博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(二)线程同步_6---修改锁的竞争原则
阅读量:6707 次
发布时间:2019-06-25

本文共 3319 字,大约阅读时间需要 11 分钟。

hot3.png

修改锁的竞争原则

ReentrantLock和ReentrantReadWriteLock的构造函数都有一个Boolean型参数fairness,默认为false,该参数用来控制改变锁的竞争原则,即两种竞争模式

  • non-fair mode:当多个线程等待获取锁时,jvm随机选择其中一个线程获取锁对象,访问临界区;
  • fair-mode:在该模式下,jvm会选择等待时间最长的线程来获取锁;

然而tryLock()方法可以不时线程睡眠,所以如果是使用tryLock()那么fairness参数将无效,不会有任何影响;

下面例子中使用一个属性作为实验,看在两种模式下的不同表现;

动手实验

(1)创建PrintQueue

public class PrintQueue {    private final Lock queueLock=new ReentrantLock(true);    public void printJob(Object document) {        queueLock.lock();        try {            Long duration=(long)(Math.random()*10000);            System.out.printf("%s:PrintQueue: Printing a Job during %d seconds\n",                    Thread.currentThread().getName(),(duration/1000));            Thread.sleep(duration);        } catch (InterruptedException e) {            e.printStackTrace();        }finally {            queueLock.unlock();        }        //-------------        queueLock.lock();        try {            Long duration=(long)(Math.random()*10000);            System.out.printf("%s:PrintQueue: Printing a Job during %d seconds\n",                    Thread.currentThread().getName(),(duration/1000));            Thread.sleep(duration);        } catch (InterruptedException e) {            e.printStackTrace();        }finally {            queueLock.unlock();        }    }}
(2)创建Job

public class Job implements Runnable {    private PrintQueue printQueue;    public Job(PrintQueue printQueue) {        this.printQueue = printQueue;    }    @Override    public void run() {        System.out.printf("%s: Going to print a document\n", Thread.currentThread().getName());        printQueue.printJob(new Object());        System.out.printf("%s: The document has been printed\n",Thread.currentThread().getName());    }    public static void main(String[] args) {       PrintQueue printQueue=new PrintQueue();        Thread thread[]=new Thread[10];        for (int i=0; i<10; i++){            thread[i]=new Thread(new Job(printQueue),"Thread "+ i);        }        for (int i=0; i<10; i++){            thread[i].start();            try {                Thread.sleep(100);            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}
一次运行结果:

Thread 0: Going to print a document

Thread 0:PrintQueue: Printing a Job during 0 seconds
Thread 1: Going to print a document
Thread 2: Going to print a document
Thread 3: Going to print a document
Thread 4: Going to print a document
Thread 5: Going to print a document
Thread 6: Going to print a document
Thread 7: Going to print a document
Thread 1:PrintQueue: Printing a Job during 9 seconds
Thread 8: Going to print a document
Thread 9: Going to print a document
Thread 2:PrintQueue: Printing a Job during 8 seconds
Thread 3:PrintQueue: Printing a Job during 1 seconds
Thread 4:PrintQueue: Printing a Job during 7 seconds
Thread 5:PrintQueue: Printing a Job during 4 seconds
Thread 6:PrintQueue: Printing a Job during 9 seconds
Thread 7:PrintQueue: Printing a Job during 8 seconds
Thread 0:PrintQueue: Printing a Job during 0 seconds
Thread 8:PrintQueue: Printing a Job during 0 seconds
Thread 0: The document has been printed
当fairness设置为true,当printJob()方法执行再次获取Lock时,此时应该是Thread-1等待的时间最长,也是应该最先获取锁的线程,如上输出结果;如果将fairness设置为false,在此观察输出结果,多运行几次发现结果都不一样;

要点

了解fairness参数的作用

转载于:https://my.oschina.net/u/1387007/blog/343259

你可能感兴趣的文章
怎么把控制台输入命令之后显示的东西保存到一个记事本中
查看>>
table完美css样式,table的基本样式,table样式
查看>>
spring-boot - demo
查看>>
matlab里plot画多幅图像、设置总标题、legend无边框
查看>>
php non-thread-safe和thread-safe这两个版本有何区别?
查看>>
GetViewUrl
查看>>
好玩的注释
查看>>
一张二维码同时集成微信、支付宝支付
查看>>
【.Net Framework 体积大?】不安装.net framework 也能运行!?原理简介-2(补充)...
查看>>
Maven编译代码的相关命令
查看>>
stingray 页面布局与设计
查看>>
江南白衣整理和开发的java常用工具类
查看>>
Android.mk 文件语法详解
查看>>
android的armeabi和armeabi-v7a
查看>>
android自己定义控件系列教程-----仿新版优酷评论剧集卡片滑动控件
查看>>
URAL 2031. Overturned Numbers (枚举)
查看>>
emacs的常用配置备份
查看>>
lvs之 lvs+nginx+tomcat_1、tomcat_2+redis(lvs dr 模式)
查看>>
让js中的函数只有一次有效调用的三种常用方法
查看>>
python实现算24的算法
查看>>