简体   繁体   English

如何使用可重入锁实现三个线程的同步?

[英]How to achieve synchronization of three threads using reentrant lock?

Below specified code snippet prints numbers in sequence with synchronizing three threads using wait() and notify() methods. 在指定的代码段下方,使用wait()和notify()方法同步打印三个线程,依次打印数字。 But the requirement is to achieve the same using reentrant locking mechanism. 但是要求是使用可重入锁定机制来实现相同的目的。

class JoinTask {

    private int currentRank = 1;

    public void doJob(int rank, int printNo) {
        synchronized (this) {
            while (rank != currentRank) {
                try {
                    System.out.println("going to wait by thread:" + printNo);
                    wait();
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            System.out.println("Job:" + printNo + " : " + currentRank);
            currentRank++;
            notifyAll();
        }
    }
}

public class ThreeThreadsPlay {

    public static void main(String[] args) {
        final JoinTask task = new JoinTask();

        Thread A = new Thread() {
            public void run() {
                int k = 1;
                for (int i = 1; i < 30; i++) {
                    task.doJob(k, 1);
                    k = k + 3;
                }}};

        Thread B = new Thread() {
            public void run() {
                int k = 2;
                for (int i = 1; i < 30; i++) {
                    task.doJob(k, 2);
                    k = k + 3;
                }}};

        Thread C = new Thread() {
            public void run() {
                int k = 3;
                for (int i = 1; i < 30; i++) {
                    task.doJob(k, 3);
                    k = k + 3;
                }}};
        C.start();
        B.start();
        A.start();
    }}

How can I achieve the same using reentrant locking? 如何使用可重入锁定实现相同目的?

Any other example using reentrant locking to provide such mechanism will also help. 使用重入锁定提供这种机制的任何其他示例也将有所帮助。 Furthermore, any information provided in this context will be highly appreciated. 此外,将高度赞赏在此上下文中提供的任何信息。

Here's a proper implementation with ReentrantLock/Conditional . 这是使用ReentrantLock/Conditional的正确实现。 Note carefully the differences between this and what you attempted. 请仔细注意此操作与尝试操作之间的区别。 The lock acquisition and release should really be handled in a try-finally block to avoid a lock being kept indefinitely, but you can find examples of that in other questions. 锁的获取和释放实际上应该在try-finally块中进行处理,以避免无限期地保留锁,但是您可以在其他问题中找到该锁的示例。

class JoinTask {

    private int currentRank = 1;
    final ReentrantLock l = new ReentrantLock();
    final Condition c = l.newCondition();

    public void doJob(int rank, int threadNumber) {
        l.lock();
        while(rank != currentRank) {
            c.await();
        }
        System.out.println("Job:" + threadNumber + " : " + currentRank);
        currentRank++;
        c.signalAll();
        l.unlock();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM