多线程实例买票
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();
}
}
//实现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();
}
}
相关文章推荐
- IOS轮播图实现
- python使用ip代理抓取网页
- 数据库优化之分库分表
- 为Mac OS X 中的MAMP安装 libmosquitto PHP扩展
- 欢迎使用CSDN-markdown编辑器
- 拼接2个数组,删除掉其中相同的元素
- android 面试专题(10)
- go中方法的接收者是值或者指针的区别
- 今日随想 7/24
- iOS-Bluetooth-MFI&BLE
- 关于JAXP, JAXB, SAX, StAX, DOM等名词解释
- UI作品
- CSVS格式文件的添加
- 上课记录
- spring mvc 中web.xml配置信息解释
- 学python的路线
- Gson解析简单介绍
- android通知公告栏,textview滚动显示上网实现
- java克隆容器后,容器内的对象相同
- Spring MVC框架简单使用