新用户登入 登录
萌子岛 返回首页

yh0603126的个人空间 https://www.dommdo.com/?1128695 [收藏] [复制] [RSS]

日志

2012-11-22

已有 200 次阅读2012-11-22 17:30

[代码] [Java]代码
view sourceprint?
001
/**
002
 * 模拟售票,一个窗口售票,多个买票者购票,票数设为50 主要是利用线程间的同步于互斥
003
 *
004
 * 同步:线程2必须在线程1完成后才能进行
005
 * 互斥:线程2和线程3访问同一变量,一次只能有一个线程访问
006
 *
007
 * @author
008
 *
009
 */
010
public class SaleTicket {
011
 
012
    public static void main(String[] args) {
013
        Ticket t = new Ticket(50);
014
        setTicket set = new setTicket(t);// 售票线程
015
        set.start();
016
        // 买票线程
017
        for (int i = 1; i <= 52; i++) {
018
            getTicket get = new getTicket(t, "买票者  " + i + " ");
019
            get.start();
020
        }
021
    }
022
}
023
//票对象
024
class Ticket {
025
    private int ticketNum;// 票数
026
    boolean write = false;// 售票与买票间的同步信号量,同一时间只有一张票卖出
027
    boolean read = true;// 买票者 买票间的信号量,一张票只能卖给一个人
028
 
029
    public Ticket(int ticketNum) {
030
        this.ticketNum = ticketNum;
031
    }
032
 
033
    // 取票,票数减一
034
    public synchronized void getTicket(String name) throws InterruptedException {
035
        // 售票员正忙,买票者进行等待
036
        while (!write && read) {
037
            System.out.println("*****************************" + name
038
                    + " 买票等待。。。");
039
            this.wait();
040
        }
041
        write = false;// 某个买票者要买票,售票员办理即进行等待
042
        if (ticketNum > 0) {
043
            read = false;// 让其他买票者等待
044
            System.out.println("*****************************" + name
045
                    + " 买票成功,号码=" + ticketNum);
046
            this.subTicketNum();// 销售一张票
047
            read = true;// 让其他买票者进行买票
048
            this.notifyAll();
049
        } else {// 票数为0
050
            read = false;
051
            System.out.println("*****************************票已经售完, " + name
052
                    + " 没有买到票");
053
        }
054
    }
055
 
056
    // 售票
057
    public synchronized void sellTicket(String name)
058
            throws InterruptedException {
059
        // 只要票数大于0,售票员就一直进行售票
060
        while (ticketNum > 0) {
061
            while (write) {
062
                // 进行售票
063
                System.out.println(name + " 处理业务,售票等待。。。");
064
                this.wait();
065
            }
066
            if (ticketNum > 0) {
067
                System.out.println(name + " 开始售票,号码=" + ticketNum);
068
            }
069
            write = true;
070
            this.notifyAll();
071
        }
072
        System.out.println(name + " 对不起,售票已经售完!");
073
    }
074
 
075
    public void subTicketNum() {
076
        if (this.ticketNum > 0)
077
            this.ticketNum = this.ticketNum - 1;
078
    }
079
 
080
    public int getTicketNum() {
081
        return ticketNum;
082
    }
083
}
084
 
085
// 购票者
086
class getTicket extends Thread {
087
    Ticket d;// 包含剩余票数
088
    String name;// 购票者姓名
089
 
090
    public getTicket(Ticket d, String name) {
091
        this.d = d;
092
        this.name = name;
093
    }
094
 
095
    public void run() {
096
        try {
097
            d.getTicket(name);
098
        } catch (InterruptedException e) {
099
            e.printStackTrace();
100
        }
101
    }
102
}
103
 
104
// 售票员
105
class setTicket extends Thread {
106
    Ticket d;
107
 
108
    public setTicket(Ticket d) {
109
        this.d = d;
110
    }
111
 
112
    public void run() {
113
        try {
114
            d.sellTicket("售票员 1 ");
115
        } catch (InterruptedException e) {
116
            e.printStackTrace();
117
        }
118
    }
119
}


路过

雷人

握手

鲜花

鸡蛋

全部作者的其他最新日志

评论 (0 个评论)

小黑屋|萌子岛

GMT+8, 2025-5-5 06:34 , Processed in 0.048264 second(s), 19 queries , Gzip On, MemCache On.

Copyright 2020  atollmoe©.a2.1.0 All rights reserved. 9+

Copyright 2009  supported by zhixuan© oeo© oko© All rights reserved.Thank you!

返回顶部