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 37 38 39 40 41 42 43 | package byetickets;
public class MyThread2 extends Thread{
private static int tickets=1;
@Override
public void run() {
while(true){
synchronized (MyThread2.class) {
if (tickets > 100) {
break;
}
System.out.println(Thread.currentThread().getName() + " 出售了第" + tickets + " 张票!");
tickets++;
}
}
}
}
1.2 售票点的模拟
package byetickets;
public class Test02 {
public static void main(String[] args) {
Thread t1=new MyThread2();
Thread t2=new MyThread2();
Thread t3=new MyThread2();
Thread t4=new MyThread2();
t1.setName(" 售票点1");
t2.setName(" 售票点2");
t3.setName(" 售票点3");
t4.setName(" 售票点4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
|
原文链接:http://www.apkbus.com/blog-813041-60900.html