/*
* 实体类 Thread
* */
public class Ticket extends Thread{
private Integer num=100;
private Object obj=new Object();
boolean flan=true;
//重写Thread类的run方法供线程调用
public void run(){
while (flan){
synchronized(obj){
try {Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); }
if (num>0){
System.out.println(Thread.currentThread().getName()+"..........."+num--);
}else {
flan=false;
}
}
}
}
}
/*
- 创建多线程
-
*/
public class Establish {
public static void main(String args[]){//方式一:继承Thread类 Ticket ticket=new Ticket(); Ticket ticket2=ticket; Ticket ticket3=ticket;
// Ticket2 ticket2=new Ticket2();
Thread thread=new Thread(ticket);
Thread thread2=new Thread(ticket2);
Thread thread3=new Thread(ticket3);thread.start(); thread2.start(); ticket3.start();
}
}
打印结果
Thread-2...........35
Thread-2...........34
Thread-3...........33
Thread-3...........32
Thread-3...........31
Thread-3...........30