class ThreadDemo {
public static void main(String[] args)
{ TestThread t = new TestThread();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}
class TestThread implements Runnable {
int tickets = 100;
public void run() {
String str = "";
while(true)
{
synchronized(str)
{
if(tickets<=0)
break;
System.out.println(Thread.currentThread().getName()
+ " is saling " + tickets--);
}
}
}
}
这么做不对吗,为什么不能实现各自的同步 |