java多線程模擬火車站窗口賣票的問題


package com.cal;

public class TicketsThread {
public static void main(String[] args) {

T tt=new T();

Thread thread=new Thread(tt,"窗口1");
Thread thread2=new Thread(tt,"窗口2");
Thread thread3=new Thread(tt,"窗口3");

thread.start();
thread2.start();
thread3.start();
}
}

class T implements Runnable{
//總的票數
private int count=5;

@Override
public void run() {
synchronized (this) {
while(count>0){
count--;
System.out.println(Thread.currentThread().getName()+"賣了一張票,還剩下"+count+"張票");
}
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM