线程通讯出现问题(synchronized)代码中wait()和notifyAll()方法感觉没启用
程序代码:
找不到问题,就是自己认为notifyAll没有起到作用,当退票后,没有将票继续分给其他人,希望解答。新手,分数不高,也可以QQ:2832799597,求教,发你一个小红包~~~ package communicationThread; public class TrainTicket { private String seatNO; private String trainNo; private String date; public TrainTicket(String seatNO, String trainNo, String date) { super(); this.seatNO = seatNO; this.trainNo = trainNo; this.date = date; } public String getSeatNO() { return seatNO; } public void setSeatNO(String seatNO) {package communicationThread; public class Passenger { private String name; public Passenger(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void buyTicket(TrainTicket ticket){ System.out.println("乘客:"+name+"打算买的票为:"+ticket); Thread t = new Thread(new BuyTicketThread(ticket,this)); t.start(); } public void returnTicket(TrainTicket ticket){ System.out.println("乘客:"+name+"打算退得票:"+ticket); Thread t = new Thread(new ReturnTicketThread(this,ticket)); t.start(); } public String toString(){ return "name:"+name; } }
程序代码:
package communicationThread; public class BuyTicketThread implements Runnable { private TrainTicket ticket; private Passenger passenger; public BuyTicketThread(TrainTicket ticket, Passenger passenger) { super(); this.ticket = ticket; this.passenger = passenger; } public void run() { synchronized(ticket){ TrainTicket t = TicketSeller.buyTicket(ticket); while(t==null){ System.out.println(passenger+"你买的票已售出!"); try { ticket.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(t!=null){ System.out.println("恭喜"+passenger+" 买到了"+ticket); } } } }
程序代码:
package communicationThread; public class ReturnTicketThread implements Runnable { private Passenger passenger; private TrainTicket ticket; public ReturnTicketThread(Passenger passenger, TrainTicket ticket) { super(); this.passenger = passenger; this.ticket = ticket; } public void run() { synchronized(ticket){ TicketSeller.returnTicket(ticket); System.out.println(passenger+"正在退票"); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ticket.notifyAll(); System.out.println(passenger+"退票成功,票单为"+ticket); } } }
this.seatNO = seatNO;
}
public String getTrainNo() {
return trainNo;
}
public void setTrainNo(String trainNo) {
this.trainNo = trainNo;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + ((seatNO == null) ? 0 : seatNO.hashCode());
result = prime * result + ((trainNo == null) ? 0 : trainNo.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final TrainTicket other = (TrainTicket) obj;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (seatNO == null) {
if (other.seatNO != null)
return false;
} else if (!seatNO.equals(other.seatNO))
return false;
if (trainNo == null) {
if (other.trainNo != null)
return false;
} else if (!trainNo.equals(other.trainNo))
return false;
return true;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "trainNO:"+trainNo+" "+"seatNO:"+seatNO+" "+"date:"+date;
}
}
程序代码:
package communicationThread; import java.util.ArrayList; public class TicketSeller { private static ArrayList pool = new ArrayList(); static{ pool.add(new TrainTicket("SK123","123","20170222")); pool.add(new TrainTicket("SK123","124","20170222")); pool.add(new TrainTicket("SK123","125","20170222")); pool.add(new TrainTicket("SK123","126","20170222")); pool.add(new TrainTicket("SK123","127","20170222")); pool.add(new TrainTicket("SK123","128","20170222")); } public static TrainTicket buyTicket(TrainTicket ticket){ TrainTicket t = null; for(TrainTicket t1:pool){ if(t1.equals(ticket)){ t=t1; System.out.println("正在出票: "+t); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } pool.remove(t1); break; } } return t; } public static void returnTicket(TrainTicket ticket){ synchronized(ticket){ pool.add(ticket); } } }package communicationThread; public class TestSecond { /** * @param args */ public static void main(String[] args) { Passenger p1 = new Passenger("张三"); TrainTicket t1 = new TrainTicket("SK123","123","20170222"); Passenger p2 = new Passenger("李四"); TrainTicket t2 = new TrainTicket("SK123","124","20170222"); Passenger p3 = new Passenger("王五"); Passenger p4 = new Passenger("孙六"); Passenger p5 = new Passenger("盛七"); p1.buyTicket(t1); p2.buyTicket(t2); p3.buyTicket(t1); p4.buyTicket(t1); p5.buyTicket(t2); p1.returnTicket(t1); p2.returnTicket(t2); } }