线程的实现(通过Runnable接口实现资源的共享:卖票)
2016-04-06 16:43
585 查看
/*通过Runnable实现资源的共享*/
class MyThread2 implements Runnable{
private int ticket = 100000 ;
public void run(){
for(int i=0;i<100000;i++){
if(ticket>0){
System.out.println("剩余票数:"+ticket--);
}
}
}
}
class ThreadTool{
public static void ThreadStart(int i){
MyThread2 my = new MyThread2();
for(int j=0; j<i; j++){
new Thread(my).start();
}
}
}
public class ThreadRunnableDemo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
long startTime=System.currentTimeMillis();
ThreadTool.ThreadStart(3);
long endTime=System.currentTimeMillis();
System.out.println("本次执行共用时:"+(endTime-startTime)+"毫秒");
}
}
class MyThread2 implements Runnable{
private int ticket = 100000 ;
public void run(){
for(int i=0;i<100000;i++){
if(ticket>0){
System.out.println("剩余票数:"+ticket--);
}
}
}
}
class ThreadTool{
public static void ThreadStart(int i){
MyThread2 my = new MyThread2();
for(int j=0; j<i; j++){
new Thread(my).start();
}
}
}
public class ThreadRunnableDemo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
long startTime=System.currentTimeMillis();
ThreadTool.ThreadStart(3);
long endTime=System.currentTimeMillis();
System.out.println("本次执行共用时:"+(endTime-startTime)+"毫秒");
}
}
相关文章推荐
- 关于引用.so(在使用第三方的SDK时,使用库跟.so)
- 公理系统与有限几何(4)
- pe如何安装ios系统
- react、redux什么的都用起来 【4】生产部署和优化
- mac 共享文件和mount挂载数据
- GO int64转int32
- 3.30作业
- 笔试笔记3 HTTPS HTTP
- 关于湖北美术学院花坛长出娃娃
- Exception in thread "http-bio-8080-exec-2" java.lang.OutOfMemoryError: PermGen space
- Android Studio项目目录结构介绍
- 线程间操作
- 适配iOS9问题汇总
- iOS做新浪微博sso授权登录遇到的一些坑
- zabbix 监控 php
- Android下常用的图像处理程序(灰度化、线性灰度变化、二值化)
- yeoman官网新手教程(LET'S SCAFFOLD A WEB APP)
- 获取唯一UUID/UDID的方案
- c++编程思想---第二章练习题
- Visual Studio 2013英文版 设置大括号不换行 代码长自动换行 显示行号