/*
* 实体类 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(getName()+"..........."+num--);
}else {
flan=false;
}
// }
}
}
public Ticket(Integer num) {
this.num = num;
}
public Ticket() {
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
}
/*
* 创建多线程
* */
public class Establish {
public static void main(String args[]){
//方式一:继承Thread类
Ticket ticket=new Ticket();
Thread thread=new Thread(ticket);
Thread thread2=new Thread(ticket);
Thread thread3=new Thread(ticket);
thread.start();
thread2.start();
thread3.start();
}
}
这是打印的线程的名字,为什么都是一样的呢?
Thread-0...........100
Thread-0...........99
Thread-0...........98
Thread-0...........97
Thread-0...........96
Thread-0...........95
Thread-0...........94
Thread-0...........93
Thread-0...........92