黑马程序员技术交流社区

标题: 线程小程序 [打印本页]

作者: yuyucoo    时间: 2015-10-12 15:05
标题: 线程小程序

public class TextThread2{
        public static void main(String[] args){
                Thread t1=new Thread(new Text21());
                t1.setName("售票窗口1"); t1.start();
                Thread t2=new Thread(new Text21());
                t2.setName("售票窗口2"); t2.start();
                Thread t3=new Thread(new Text21());
                t3.setName("售票窗口3"); t3.start();
        }
}
class Text21 implements Runnable{
        public static int nums1=100;
        static String ss=new String("11");
       
        public void run() {
                while(true){
                        synchronized(ss){
                                if(nums1>0){
                                        System.out.println(Thread.currentThread().getName()+"卖出了第:"+(101-nums1)+"票。");
                                        --nums1 ;
                                }else{
                                        break;
                                }
                        }
                       
                }
       
               
        }
       
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2