美文网首页
多线程模拟火车站售票出现问题

多线程模拟火车站售票出现问题

作者: 卓昂芭比 | 来源:发表于2019-03-31 18:30 被阅读0次

*模拟火车站售票

* 分析:火车票总数量,每售出一张数量减一

*      当火车票小于0张时,停止售票

*      使用多个线程模拟多个窗口进行售票

*      火车票售完,火车站门也开着

  static void sleep(long mills)在指定的毫秒数内让当前正在执行的线程休眠

public class TicketTest implements Runnable {

int num=100;

public void run() {

while(true) {

if(num>0) {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName()+":"+num--);

}

}

}

}

package RunnableTest1;

public class TicketThread {

public static void main(String[] args) {

//创建多个窗口对象

TicketTest tt=new TicketTest();

Thread t=new Thread(tt);

t.setName("窗口1");

Thread t2=new Thread(tt);

t2.setName("窗口2");

Thread t3=new Thread(tt);

t3.setName("窗口3");

//启动多线程对象

t.start();

t2.start();

t3.start();

}

}

相关文章

网友评论

      本文标题:多线程模拟火车站售票出现问题

      本文链接:https://www.haomeiwen.com/subject/ikpubqtx.html