当年话下
2018-10-13 15:17:15浏览 1996
1.1线程的创建
[代码]java代码:
?
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package byetickets;
public class MyThread implements Runnable{
private int tickets=1;// 票数
@Override
public void run() {
while(true){
synchronized (this) {
if (tickets > 100) {// 共有100张票
break;
}
System.out.println(Thread.currentThread().getName() + " 出售了第" + tickets + " 张票!");
tickets++;
}
}
}
}
1.2 售票点的模拟
package byetickets;
public class Test01 {
public static void main(String[] args) {
MyThread my=new MyThread();
Thread t1=new Thread(my," 售票点1");
Thread t2=new Thread(my," 售票点2");
Thread t3=new Thread(my," 售票点3");
Thread t4=new Thread(my," 售票点4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
|
原文链接:http://www.apkbus.com/blog-813041-60899.html