卖火车票---java线程

来源:互联网 发布:vs2017写c语言 编辑:程序博客网 时间:2024/04/30 16:27
package com.pcs;public class TicketDemo{public static void main(String[] args){Demo d=new Demo();for(int i=0;i<5;i++)new Thread(d,"售票口 "+(i+1)+"  :").start();}}class Demo implements Runnable{private volatile int num=100;public void run(){while(num>0){synchronized(this){try{Thread.sleep(100);}catch(Exception e){}if(num>0){System.out.println(Thread.currentThread().getName()+num);num--;}}}}}














0 0
原创粉丝点击