简体   繁体   English

使用AbstractMonitor在Java中进行并行编程,如果满足条件,如何使线程离开监视器

[英]concurrent programming in java using AbstractMonitor, how can i make a thread leave the monitor if a condition is met

i'm trying to solve the sleeping barber problem in java i'm having a problem when trying to make a thread leave the monitor when a signle condition is met here's the code: 我正在尝试解决Java中的理发师睡眠问题,在满足信号条件的情况下尝试使线程离开监视器时遇到问题,代码如下:

public void getHairCut(String threadName) {
    enter();
    if(!barberSleeping) {
        if(freeSeats == 0) {
        System.out.println(threadName + " waiting...");
            freeSeats--;;           
            hairCut.await();
        }
        else {
            System.out.println("waiting room is full " + threadName + " is leaving...");
            return;   // i'm having a problem here,i want the thread to return     from the method and release the monitor's lock to let other threads enter
        }
    }
    // some code

    leave();
}

thanks in advance. 提前致谢。

In very brief the situation is as follows: 简而言之,情况如下:

In order to be capable of waiting for a condition to be met a thread (say T0) must first acquire a lock for itself. 为了能够等待条件被满足,线程(例如T0)必须首先为其自身获取一个锁。 After that it might (or might not) be suspended because some condition is not yet met by calling a wait-function. 之后,它可能会(或可能不会)被挂起,因为尚未通过调用等待函数满足某些条件。 If this condition will be met at a later point (by another thread eg T1, because T0 is suspended), T1 might signal the suspended Thread T0 to reclaim its work. 如果在以后的某个时间(由于另一个线程,例如T1,因为T0被挂起)将满足此条件,则T1可能会向挂起的线程T0发信号,以收回其工作。 In order to signal the condition which was responsible for suspending T0, T1 also needs to acquire the one lock associated with the condition. 为了用信号通知负责挂起T0的条件,T1还需要获取与该条件关联的一个锁。

A corresponding code would look like: 相应的代码如下所示:

// relevant prior assumptions

Lock myLock = new ReentrantLock();
Condition myCondition = myLock.newCondition();

// code that might suspend thread T0
try {

    myLock.lock();

    while (someCondition == false) {

        myCondition.await();

    }

} finally {

    myLock.unlock();

}

// code that signals T0 to wake up and continue with its wrkr where it stopped (which is exactly aone line after the call to myCondition.awayit())

try {

    myLock.lock();

    myCondition.signal();
} finally {

    myLock.unlock();
}

Can't you wrap the critical section in a try-finally block? 您能否将关键部分包装在try-finally块中?

public void getHairCut(String threadName) {
    enter();
    try {
        if(!barberSleeping) {
            if(freeSeats == 0) {
            System.out.println(threadName + " waiting...");
                freeSeats--;;           
                hairCut.await();
            }
            else {
                System.out.println("waiting room is full " + threadName + " is leaving...");
                return;   // i'm having a problem here,i want the thread to return     from the method and release the monitor's lock to let other threads enter
            }
        }
        // some code
    }
    finally {
        leave(); // this would be called in any case
    }
}

The finally block will be executed in any case: 在任何情况下都将执行finally块:

  • when execution reaches the bottom 当执行到达最低点
  • when an exception is thrown 当引发异常时
  • when the code block is quit because of a return statement 当代码块由于返回语句而退出时

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

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