class Ticket extends Thread //implements Runnable
{
private int tick = 100;
Object obj = new Object();
public void run()
{
while (true)
{
synchronized(this)
{
if(tick>0)
{
try{Thread.sleep(5);}catch (Exception e){}
System.out.println(Thread.currentThread().getName()+"sale:"+tick--);
}
}
}
}
}
class TicketDemo2
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Ticket t1 = new Ticket();
t.start();
t1.start();//这样实现不可以 ,该怎么实现同步呢 ,最好举个例子
//new Thread(t).start();
//new Thread(t).start();
//new Thread(t).start();
//new Thread(t).start();
//new Thread(t).start();
}
}
实现Runnable的同步我会,可是继承Thread怎么实现同步呢? |