| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 955 人关注过本帖
标题:线程通讯出现问题(synchronized)代码中wait()和notifyAll()方法感觉没启用 ...
只看楼主 加入收藏
s人圭日月
Rank: 2
等 级:论坛游民
帖 子:36
专家分:27
注 册:2016-6-22
结帖率:50%
收藏
 问题点数:0 回复次数:2 
线程通讯出现问题(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);

    }

}

2017-02-22 17:14
HolyOrder
Rank: 3Rank: 3
等 级:论坛游侠
威 望:6
帖 子:47
专家分:140
注 册:2017-2-6
收藏
得分:0 
应该是同步代码块锁的对象不一致吧,字太小看得头晕,没仔细看
2017-02-22 17:56
s人圭日月
Rank: 2
等 级:论坛游民
帖 子:36
专家分:27
注 册:2016-6-22
收藏
得分:0 
回复 2楼 HolyOrder
是票取出来后,退票后无法回到集合里面。
2017-02-25 09:27
快速回复:线程通讯出现问题(synchronized)代码中wait()和notifyAll()方法感觉 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045982 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved