【问题标题】:Synchronization in java-- new ReentrantLock(true) and new ReentrantLock(false) producing the same result?java 中的同步——new ReentrantLock(true) 和 new ReentrantLock(false) 产生相同的结果?
【发布时间】:2017-01-28 20:34:30
【问题描述】:

ReentrantLock 在创建 Lock 对象时提供boolean fair 标志。

  1. fair:true

    根据线程等待的时间授予线程访问临界区的权限。

  2. fair:false

    没有将临界区分配给线程的特定策略。

下面是我的代码:

public class ThreadDemo {
    private Lock lock = new ReentrantLock(false);

    public static void main(String[] args) {
        ThreadDemo td = new ThreadDemo();
        Thread[] tArr = new Thread[5];
        // Creates 5 thread and stores them in a array
        for (int i = 0; i < 5; i++) {
            tArr[i] = new Thread(() -> {
                td.enterCriticalSection(new Date().getTime());
            }, "Thread " + i);
        }
        // Iterate through the array and start it.
        for (int i = 0; i < 5; i++) {
            tArr[i].start();
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    public void enterCriticalSection(long waitTime) {
        System.out.println(Thread.currentThread().getName() + " requesting critical section at:"
                + new SimpleDateFormat("HH:mm:ss:SSS").format(new Date().getTime()));
        // Gets Lock
        lock.lock();

        try {
            /*
             * Logs the entry time in critical section and waiting period for
             * the thread
             */
            System.out.println(Thread.currentThread().getName() + " in critical section at  "
                    + new SimpleDateFormat("HH:mm:ss:SSS").format(new Date().getTime()));
            Thread.currentThread().sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // Releases lock
            lock.unlock();
        }
            
        }
}

但是对于 fair as truefair as false 我得到相同的结果,即线程等待时间最长的获取临界区

Thread 0 requesting critical section at:01:57:48:562
Thread 0 in critical section at  01:57:48:563
Thread 1 requesting critical section at:01:57:49:520
Thread 2 requesting critical section at:01:57:50:520
Thread 3 requesting critical section at:01:57:51:520
Thread 4 requesting critical section at:01:57:52:520
Thread 1 in critical section at  01:57:53:564
Thread 2 in critical section at  01:57:58:564
Thread 3 in critical section at  01:58:03:565
Thread 4 in critical section at  01:58:08:566

【问题讨论】:

    标签: java multithreading locking


    【解决方案1】:

    所有fair false 的意思是锁会让线程随心所欲地进入。对于少量线程,它可能恰好是它们一直在等待的顺序,但它不能对此做出任何保证。

    【讨论】:

    • 我一开始尝试了10-20个线程并尝试了多次,即使我认为它会根据关键部分的请求顺序分配锁
    • @DharmvirTiwari javadoc 说ReentrantLock(true) 必须创建一个公平锁,但它确实ReentrantLock(false) 必须创建一个不公平锁。如果您的 JRE 即使在 fair==false 时也似乎创建了一个公平锁,那么这并不能证明它有什么问题。
    猜你喜欢
    • 2011-12-29
    • 1970-01-01
    • 2018-04-17
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多