线程学习之--6案例:售票demo

来源:互联网 发布:淘宝官方加盟可靠吗 编辑:程序博客网 时间:2024/05/16 14:34
package com;
/**
 * 需求:买票系统。一共有一百张火车票,同时有四个窗口卖着100张票。使用多线程的技术(也可以使用static方法)。
 * @author 勇
 *(该demo存在线程安全的问题)
 */

class Piao implements Runnable{//火车票类

    private int num = 100;//定义一百张票
    public void run() {
        while(true){
            if(num>0){
                System.out.println(Thread.currentThread().getName()+">>"+ num--);
            }            
        }
    }    
}

public class Ticket {
    public static void main(String[] args) {
        Piao p=new Piao();//同时操作一个对象
        Thread t1=new Thread(p);//窗口1
        Thread t2=new Thread(p);//窗口2
        Thread t3=new Thread(p);//窗口3
        Thread t4=new Thread(p);//窗口4
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

}


原创粉丝点击