package sixth;
public class ThreadDemo1 implements Runnable{
public static int tickets = 100;
private Object obj = new Object();
public void run()
{
while (tickets>0)
{
synchronized (obj)
{
if (tickets > 0)
{
System.out.println(Thread.currentThread().getName()+"_________"+tickets);
--tickets;
}
else
{
break;
}
}
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
package sixt
public class ThreadTest1 {
public static void main(String[] args) {
ThreadDemo1 a=new ThreadDemo1();
Thread t1=new Thread(a,"1");
Thread t2=new Thread(a,"2");
Thread t3=new Thread(a,"3");
Thread t4=new Thread(a,"4");
Thread t5=new Thread(a,"5");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
实例代码一(多线程买火车票)
最新推荐文章于 2023-08-31 15:32:46 发布