1.模拟销售类:
class ThreadTest implements Runnable { private int tickets=100; public void run() { while(true) { if(tickets>0) System.out.println(Thread.currentThread().getName() + " is saling ticket " + tickets--); } } <p>}</p>
2.执行类:
public static void main(String [] args) { ThreadTest t=new ThreadTest(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); }
时间: 2024-10-10 00:07:10