Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确同步此模型?_Java_Multithreading - Fatal编程技术网

Java 如何正确同步此模型?

Java 如何正确同步此模型?,java,multithreading,Java,Multithreading,想象一下,有一个按摩师,他有自己的沙龙。他睡了一整天,直到一位顾客走进沙龙把他叫醒 顾客在按摩时正在睡觉。当按摩师完成后,他叫醒顾客,并获得服务报酬 顾客离开了沙龙 按摩师进入等候室寻找另一位正在等候(睡觉)的顾客。如果没有,按摩师就再去睡觉 在处理线程时,这是一个有趣的情况 public class Aufg1{ public static void main(String args[]){ MassageSalon ms = new MassageSalon();

想象一下,有一个按摩师,他有自己的沙龙。他睡了一整天,直到一位顾客走进沙龙把他叫醒

顾客在按摩时正在睡觉。当按摩师完成后,他叫醒顾客,并获得服务报酬

顾客离开了沙龙

按摩师进入等候室寻找另一位正在等候(睡觉)的顾客。如果没有,按摩师就再去睡觉

在处理线程时,这是一个有趣的情况

public class Aufg1{
    public static void main(String args[]){
        MassageSalon ms = new MassageSalon();
        Customer c = new Customer(ms);
        Masseur m = new Masseur(ms);
        m.start(); c.start();
    }
}
Masseur.java

public class Masseur extends Thread{

    final MassageSalon salon;

    public Masseur(MassageSalon pSalon){
        salon = pSalon;
    }

    public void run(){
        while(true){
            salon.getNextCustomer();
            salon.finishedMassage();
        }
    }
}
Customer.java

public class Customer extends Thread{

    final MassageSalon salon;

    public Customer(MassageSalon pSalon){
        salon = pSalon;
    }

    public void run(){
        while(true){
            salon.getMassage();
        }
    }
}
我有一堂按摩课。代码描述与我刚才提到的几乎相同

现在我想使用
wait()、notify()、notifyAll()
来确保一切都像我提到的那样工作。我已经编辑了MassageSalon类并添加了wait()、notify()方法

您认为wait()和notify()的位置正确吗?运行此代码时,不会调用finishedMassage方法。为什么?

 public class MassageSalon {
    private int customerOnCouch = 0;
    private int customerPaid = 0;
    private int masseurAvailable = 0;
    private int masseurBusy = 0;
    private int masseurDone = 0;
    private int masseurClose = 0;


    public synchronized void getNextCustomer() {

        while(masseurAvailable != masseurClose){
            try{
                System.out.println("waiting for masseur...");
                wait();
            }catch(InterruptedException e){
                System.out.println(e);
            }
        }

        //masseur is available to handle a new customer

        System.out.println("masseur is available to handle a new customer");

        masseurAvailable++;

        while(customerOnCouch == customerPaid){
            try{
                System.out.println("waiting for customer...");
                wait();
            }catch(InterruptedException e){
                System.out.println(e);
            }
        }

        //and is busy as soon as a new customers takes his couch

        System.out.println("and is busy as soon as a new customers takes his couch");

        masseurBusy++;
    }

    public synchronized void finishedMassage() {
        //eventually the masseur finishes the massage

        System.out.println("eventually the masseur finishes the massage");

        masseurDone++;

        notify();

        //and closes the deal as soon as the customer paid

        System.out.println("and closes the deal as soon as the customer paid");

        masseurClose++;
    }

    public synchronized void getMassage() {
        //customer takes a couch

        System.out.println("customer takes a couch");

        customerOnCouch++;  

        notify();

        while(masseurBusy != masseurDone){
            try{
                System.out.println("waiting to finish massage");
                wait();
            }catch(InterruptedException e){
                System.out.println(e);
            }
        }

        //and pays for the massage after it

        System.out.println("and pays for the massage after it");

        customerPaid++;
    }
}

你描述的是迪克斯特拉的睡眠理发问题,但不是理发店,而是按摩院。不过,解决方案是一样的,可以在这里找到:

您可以使用一个名为NotSleeping的fair

只要顾客不进入酒馆,它就保持不睡觉。当顾客进来时,它会释放信号灯,这会唤醒按摩师试图抓住的线索。释放后,客户尝试在按摩过程中保持不睡觉


完成后,按摩师释放NotSleep,顾客再次抓住它。按摩师,试图保持不睡觉,直到顾客进来。等等等等。

@Rom1:是的,是的。嗯,不完全一样。我只是想知道上面代码中的wait、notfiy和notifyAll应该放在哪里,以便相互排斥。@ArtWorkAD。很多东西都不见了。您需要创建线程和某种类型的客户队列。