class Ticket1 implements Runnable{
private String name;
private int ticket = 50;
public Ticket1(String name){
this.name = name;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i<this.ticket; i++ ){
synchronized(this){
System.out.println(this.name+Thread.currentThread().getName()+"--->"+i);
}
}
}
}
public class test {
public static void main(String[] args) {
Ticket1 t1 = new Ticket1("实现方式");
new Thread(t1).start();
new Thread(t1).start();
new Thread(t1).start();
}
}
这是一个很简单的例子啊,我和我没有达到共享车票的目的 |
|