首页 > 其他分享 >简单多线程卖票代码

简单多线程卖票代码

时间:2023-02-19 23:35:59浏览次数:28  
标签:count Thread 卖票 代码 class ShareData new 多线程 public


 

public class Thread2 {
public static void main(String[] args) {
ShareData d = new ShareData();
//两个窗口卖票
new Thread(d).start();
new Thread(d).start();
}
}


class ShareData implements Runnable {

private int count = 100;

@Override
public void run() {
synchronized (ShareData.class) {
while (count > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println(Thread.currentThread().getName() + "余票: " + count);
}
}
}



}

 

 


标签:count,Thread,卖票,代码,class,ShareData,new,多线程,public
From: https://blog.51cto.com/u_21817/6066951

相关文章