【问题标题】:Java Multithreading Communication does not workJava 多线程通信不起作用
【发布时间】:2013-12-28 20:31:35
【问题描述】:

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

类:ATM

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

Frank 尝试使用 ATM-01

ATM-01 不可用。请稍候...

玛丽尝试使用 ATM-01

ATM-01 不可用。请稍候...

Soy 尝试使用 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 可用。

FrankThread 正在使用 ATM-01

ATM-02 可用。

JohnThread 正在使用 ATM-02

ATM-01 可用。

ATM-02 可用。

但是,其实下面的输出从来没有出现过。

XXX 不可用。请稍候...

那么有没有人可以告诉我并向我解释? 谢谢。

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) 源代码中的一个空白行总是就足够了。 { 之后或} 之前的空行通常也是多余的。

标签: java multithreading communication


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我想你想做的就是睡在显示器外面。就代码而言:使用同步块(synchronized(this){...} 用于 useATM 的第一位。然后是 try 和 try 内的睡眠。最后是第二个同步块用于 useATM 的最后一部分。这将得到你的效果正在寻找。

    最后一件事:使用while( ! isAvailable ) 代替if( ! isAvailable )。原因是不能保证等待线程在从wait返回时会找到isAvailable true。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多