多线程实现四个窗口共同售10张票(java)"/>
多线程实现四个窗口共同售10张票(java)
//实现四个窗口共同售10张票(多线程)
public class demo2 {public static void main(String []args){ThreadTest2 tt = new ThreadTest2();new Thread(tt).start();//第一个线程new Thread(tt).start();//第二个线程new Thread(tt).start();//第三个线程new Thread(tt).start();//第四个线程}
}
class ThreadTest2 implements Runnable{int ticket = 10;String str = new String("");public void run() {synchronized (str) {while(ticket>0){try{Thread.sleep(3000);}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"saling the "+ticket- -);}}}}
int ticket = 10;String str = new String();都不能放在run方法中,否则将不能实现同步
更多推荐
多线程实现四个窗口共同售10张票(java)
发布评论