class Ticket implements Runnable
{
private int tick = 100000;
Object obj = new Object();
public void run()
{
while(true)
{
synchronized(obj)
{
if(tick>0)
{
try{Thread.sleep(1);}
catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"------sale:"+tick--);
}
}
}
}
}
class TicketDemo4
{
public static void main(String[] args)
{
Ticket t = new Ticket();
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();
}
}多线程(三)——多线程安全问题之同步代码块(Synchronized)
最新推荐文章于 2025-10-31 08:45:00 发布
340

被折叠的 条评论
为什么被折叠?



