Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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_Communication - Fatal编程技术网

Java多线程通信不起作用

Java多线程通信不起作用,java,multithreading,communication,Java,Multithreading,Communication,伙计们, 我正在学习一些关于Java多线程的知识。我的代码如下: 类别:自动取款机 package com.frank.threadlearning; public class ATM { private String atmNo; private boolean isAvailable = true; public ATM(){ this("ATM-00"); } public ATM(String s){ this.atmNo = s; } public String

伙计们, 我正在学习一些关于Java多线程的知识。我的代码如下:

类别:自动取款机

package com.frank.threadlearning;

public class ATM {
private String atmNo;
private boolean isAvailable = true;

public ATM(){
    this("ATM-00");
}

public ATM(String s){
    this.atmNo = s;
}

public String getATMNo(){
    return this.atmNo;
}

public synchronized void useATM(){
    try{
        if(!isAvailable){
            System.out.println(this.atmNo + " is unavailable. Please wait...");
            this.wait();
        }
        isAvailable = false;
        System.out.println(Thread.currentThread().getName() + " is using " + this.atmNo);
        Thread.sleep(5000);
        System.out.println(this.atmNo + " is available.");
        isAvailable = true;
        this.notifyAll();

    }catch(InterruptedException ie){
        ie.printStackTrace();
    }
}

public String getStatus(){
    return this.atmNo + " is available? " + this.isAvailable;
}


}
类别:ATMUser

package com.frank.threadlearning;

public class ATMUser implements Runnable{
private ATM atm;
private String name;

public ATMUser(ATM atm, String s){
    this.atm = atm;
    this.name = s;
}

public String getName(){
    return this.name;
}

public void run(){
    System.out.println(this.name + " tries to use the " + this.atm.getATMNo());
    this.atm.useATM();
}

}
班级:ATMRoom

package com.frank.threadlearning;

public class ATMRoom {

public static void main(String[] args){
    //Define two ATM objects.
    ATM atm1 = new ATM("ATM-01");
    ATM atm2 = new ATM("ATM-02");
    //Define six ATMUser objects.
    ATMUser user11 = new ATMUser(atm1,"Frank");
    ATMUser user12 = new ATMUser(atm1,"Kate");
    ATMUser user13 = new ATMUser(atm1,"Mary");
    ATMUser user21 = new ATMUser(atm2,"John");
    ATMUser user22 = new ATMUser(atm2,"Soy");
    ATMUser user23 = new ATMUser(atm2,"Claire");

    Thread thread11 = new Thread(user11,user11.getName()+"Thread");
    Thread thread12 = new Thread(user12,user12.getName()+"Thread");
    Thread thread13 = new Thread(user13,user13.getName()+"Thread");
    Thread thread21 = new Thread(user21,user21.getName()+"Thread");
    Thread thread22 = new Thread(user22,user22.getName()+"Thread");
    Thread thread23 = new Thread(user23,user23.getName()+"Thread");

    thread11.start();
    thread12.start();
    thread13.start();
    thread21.start();
    thread22.start();
    thread23.start();




}


 }
我希望结果是这样的:

凯特试图使用ATM-01

KateThread正在使用ATM-01

弗兰克试图使用ATM-01

ATM-01不可用。请稍候

玛丽试图使用ATM-01

ATM-01不可用。请稍候

大豆试图使用ATM-02

SoyThread正在使用ATM-02

约翰试图使用ATM-02

ATM-02不可用。请稍候

克莱尔试图使用ATM-02

ATM-02不可用。请稍候

ATM-01可用

MaryThread正在使用ATM-01

ATM-02可用

ClaireThread正在使用ATM-02

ATM-01可用

FranksThread正在使用ATM-01

ATM-02可用

约翰里德正在使用ATM-02

ATM-01可用

ATM-02可用

然而,事实上,以下输出从未出现过

XXX不可用。请稍候

有没有人能告诉我并向我解释?
谢谢。

因为useATM方法是同步的,所以一次只能有一个线程可以进入给定ATM的useATM方法。

我想你要做的是在监视器外面睡觉。在代码方面:对useATM的第一位使用一个同步块(
synchronized(this){…}
),然后是try和try中的sleep。最后对useATM的最后一部分使用第二个同步块。这将得到您想要的效果

不过,最后一件事是:如果(!isAvailable)使用
而不是
而(!isAvailable)
。原因是不能保证等待线程在从
等待
返回时会发现
isAvailable
为真。要更快地获得更好的帮助,请发布一条消息。2) 源代码中只有一行空白就足够了。
{
之后或
}
之前的空行通常也是多余的。