package xianchen;
class ticks2 implements Runnable//extends Thread
{
private int ticks = 100;
public void run()
{
Object b = new Object();
while(true)
{
synchronized(b) //通过synchronized,同步代码块
{
if(ticks > 0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
}
System.out.println(Thread.currentThread().getName()+"出票"+ticks--);
}
}
}
}
}
public class test4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ticks2 t = new ticks2();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
运行后还有打印出有负数的票,这是为什么?
|
|