确保多线程的安全性,能不能不用同步代码块synchronized,用join()方法可以不
例如:class TicketRunnable implements Runnable{
private int tickets = 100;
public void run(){
while(true){
Thread.currentThread().join();
if(tickets>0){
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"出售第"+tickets--);
}
}
}
}
public class ThreadDemo8 {
public static void main(String[] args) {
TicketRunnable t = new TicketRunnable();
Thread t0 = new Thread(t);
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t0.start();
t1.start();
t2.start();
}
}
这个样子行不
|
|