1,多线程模拟售票系统
class Threadscale implements Runnable
{
int tickets=100;
public void run()
{
while((tickets--)>0)
{
System.out.println(Thread.currentThread().getName()+"售票机出售第"+(tickets+1)+"张票");
try
{ Thread.sleep(200);}
catch(InterruptedException e){
System.out.println(e);
}
}
}
}
public class MyThread {
public static void main(String args[])
{
Threadscale t=new Threadscale();
Thread th1=new Thread(t,"线程1");
Thread th2=new Thread(t,"线程2");
Thread th3=new Thread(t,"线程3");
Thread th4=new Thread(t,"线程4");
th1.start();
th2.start();
th3.start();
th4.start();
}
}