您的位置:首页 > 其它

多线程实例买票

2016-07-24 22:26 211 查看
package com.seven.线程;
//实现Runnable接口
public class 买票 implements Runnable {
// 定义一个变量票为1000
private  int tick = 1000;
// 覆盖里面的run方法

public void run (){
while(true){
// 加锁实现单线程买票
synchronized (this) {
if(tick>0){
System.out.println(Thread.currentThread().getName()+"...........卖票"+tick--);
}
}
}
}

}

package com.seven.线程;

public class 买票2 {
public static void main(String[] args) {
买票 t = new 买票();

// 创建4个线程制定所实现的对象t
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);

// 开启线程
t1.start();
t2.start();
t3.start();
t4.start();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: