黑马程序员技术交流社区

标题: 模拟火车站程序,调试过程中出现,错误异常。大神进来..... [打印本页]

作者: nine789    时间: 2015-6-25 20:44
标题: 模拟火车站程序,调试过程中出现,错误异常。大神进来.....
今天把昨天的代码补全了 ,但是总是会出现一些 异常,出现的的错误是 ,错误的行数不确定,请大神帮忙解答一下,昨天帖子地址:[url]http://bbs.itheima.com/thread-207208-1-1.html[/url]
****************************************
火车站3:窗口1:(售):票号:(20150001)余票:299
Exception in thread "火车站4:" Exception in thread "火车站1:" java.lang.IllegalThreadStateException
        at java.lang.Thread.start(Thread.java:704)
        at review.Depot.run(Depot.java:69)
        at java.lang.Thread.run(Thread.java:744)
java.lang.IllegalThreadStateException
        at java.lang.Thread.start(Thread.java:704)
        at review.Depot.run(Depot.java:69)
        at java.lang.Thread.run(Thread.java:744)
Exception in thread "火车站2:" java.lang.IllegalThreadStateException
        at java.lang.Thread.start(Thread.java:704)
        at review.Depot.run(Depot.java:69)
        at java.lang.Thread.run(Thread.java:744)
Exception in thread "火车站3:" java.lang.IllegalThreadStateException
        at java.lang.Thread.start(Thread.java:704)
        at review.Depot.run(Depot.java:69)
        at java.lang.Thread.run(Thread.java:744)
火车站3:窗口1:(售):票号:(20150002)余票:298

********************
        at java.lang.Thread.run(Thread.java:744)
火车站2:窗口1:(售):票号:(20150002)余票:298
火车站2:窗口1:(售):票号:(20150003)余票:297
火车站2:窗口1:(售):票号:(20150004)余票:296
火车站4:窗口[5]:【退】:票号:(20150004)余票:297
火车站1:窗口[6]:【退】:票号:(20150003)余票:298
火车站4:窗口[6]:【退】:票号:(20150002)余票:299
火车站4:窗口[6]:【退】:票号:(20150001)余票:300
Exception in thread "火车站4:窗口[6]:【退】:" Exception in thread "火车站1:窗口[5]:【退】:" java.lang.IllegalMonitorStateException
        at java.lang.Object.notify(Native Method)
        at review.Depot$SetTicketWindows.run(Depot.java:206)
        at java.lang.Thread.run(Thread.java:744)
java.lang.IllegalMonitorStateException
        at java.lang.Object.notify(Native Method)
        at review.Depot$SetTicketWindows.run(Depot.java:206)
        at java.lang.Thread.run(Thread.java:744)
火车站1:窗口4:(售):票号:(20150001)余票:299
火车站1:窗口4:(售):票号:(20150002)余票:298

********************
下面是今天补全的火车站类:
测试类在昨天发的帖子里:http://bbs.itheima.com/thread-207208-1-1.html
  1. package review;

  2. import java.util.LinkedList;



  3. /************火车站*************************/
  4. public class Depot implements Runnable{

  5.         //TreeSet<Integer> TICKETSS = new TreeSet<Integer>();//火车站车票池
  6.         LinkedList<Integer> TICKETSS = new LinkedList<Integer>();//未卖出车票
  7.         LinkedList<Integer> NOT_TICKETSS = new LinkedList<Integer>();//已卖出车票
  8.         int piaohao = 20150001;
  9.         int y = 300;
  10.         /*int [] TICKETSS = new int [100];*/
  11.         //private  int TICKETS = 200;// 定义票数

  12.        

  13.         Depot(){

  14.                 for(int x = 1;x<=/*TICKETSS.size()*/300;x++){//初始值为100张
  15.                         TICKETSS.add(piaohao++);
  16.                 }
  17.         }
  18.         public void run(){       
  19. /*                for(int x = 1;x<TICKETSS.size();x++){
  20.                         TICKETSS.add(x);
  21.                 }*/
  22.        
  23.                        
  24.                
  25.                 GetTicketWindows gtw = new GetTicketWindows();//售票窗口
  26.                 SetTicketWindows stw = new SetTicketWindows();//退票窗口
  27.                

  28.                 Thread t1 = new Thread(gtw);
  29.                 Thread t2 = new Thread(gtw);
  30.                 Thread t3 = new Thread(gtw);
  31.                 Thread t4 = new Thread(gtw);
  32.                
  33.                 Thread t5 = new Thread(stw);
  34.                 Thread t6 = new Thread(stw);
  35.                 //Thread t7 = new Thread(stw);
  36.                
  37.                 while(true){       
  38.                         //synchronized(Depot.class){                       
  39.                         //        if(TICKETS > 0){       
  40.                                         try {
  41.                                                 Thread.sleep(27);
  42.                                         } catch (InterruptedException e) {
  43.                                                 // TODO Auto-generated catch block
  44.                                                 e.printStackTrace();
  45.                                         }

  46.                                        

  47.                                         t1.setName(Thread.currentThread().getName()+"窗口1:(售):");
  48.                                         t2.setName(Thread.currentThread().getName()+"窗口2:(售):");
  49.                                         t3.setName(Thread.currentThread().getName()+"窗口3:(售):");
  50.                                         t4.setName(Thread.currentThread().getName()+"窗口4:(售):");
  51.                                        
  52.                                         t5.setName(Thread.currentThread().getName()+"窗口[5]:【退】:");
  53.                                         t6.setName(Thread.currentThread().getName()+"窗口[6]:【退】:");
  54.                                         //t7.setName(Thread.currentThread().getName()+"窗口[7]:【退】:");
  55.                                        
  56.                                        
  57.                                        
  58.                                         t1.start();
  59.                                         t2.start();
  60.                                         t3.start();
  61.                                         t4.start();
  62.                                        
  63.                                         t5.start();
  64.                                         t6.start();
  65.                                         //t7.start();

  66. /*                                        Ting T = new Ting();
  67.                                         Thread ting1 = new Thread();
  68.                                         Thread ting2 = new Thread();
  69.                                         ting1.setName(Thread.currentThread().getName()+"第一大厅:");
  70.                                         ting2.setName(Thread.currentThread().getName()+"第二大厅:");
  71.                                        
  72.                                         ting1.start();
  73.                                         ting2.start();*/
  74.                                 //}       
  75.                         //}               
  76.                 }

  77.         }





  78. /************售票窗口***********************/
  79. /**售票代码开始**/
  80. private  class GetTicketWindows implements Runnable{
  81.         public void run() {
  82.                 // 重写Runnable中的run方法
  83.                 //int s = 0;
  84.                 while (true) {

  85.                                
  86.                        
  87.                         //if (s % 2 == 0) {
  88.                                 /****同步代码块开始******/

  89.                                 synchronized (Depot.class) {
  90.                                         if(/*TICKETSS.isEmpty()  */ /*TICKETSS.size()< = y*/y<=0){//如果没有票,那就等待
  91.                                                
  92. /*                                                if(y==0){
  93.                                                         try {
  94.                                                                 Thread.sleep(200);
  95.                                                         } catch (InterruptedException e) {
  96.                                                                 // TODO Auto-generated catch block
  97.                                                                 e.printStackTrace();
  98.                                                         }
  99.                                                 }*/
  100.                                                 //stw.run();
  101.                                                 try {
  102.                                                         this.wait();
  103.                                                 } catch (InterruptedException e) {
  104.                                                         // TODO Auto-generated catch block
  105.                                                         e.printStackTrace();
  106.                                                 }finally{
  107.                                                         this.notify();
  108.                                                 }
  109.                                         }       
  110.                                                 if (/*TICKETSS.size()*/y > 0) {
  111.                                                         try {
  112.                                                                 Thread.sleep(27);//出票等待时间
  113.                                                         } catch (InterruptedException e) {
  114.                                                                 // TODO Auto-generated catch block
  115.                                                                 e.printStackTrace();
  116.                                                         }                                                                       
  117.                                                         synchronized(/*GetTicketWindows.class*/Depot.class){
  118.                                                                 synchronized("A"){
  119.                                                                        
  120.                                                                         y--;
  121.                                                                         NOT_TICKETSS.addFirst(TICKETSS.getFirst());//把下面这张卖出去的票增加到已卖出车票列表的第一个位置
  122.                                                                         System.out.println(Thread.currentThread().getName()
  123.                                                                                         +"票号:("+  TICKETSS.pollFirst() + ")余票:"+ (y)/*(TICKETSS.size())"-----同步代码块"*/ );
  124.                                                                 }
  125.                                                                 //this.notify();
  126.                                                                 //TICKETSS.pollFirst();
  127.                                                         }
  128.                                                        
  129.                                                        
  130.                                                        
  131.                                                 }
  132.                                 }
  133.                                 /******同步代码块结束*******/
  134.                         //}
  135.                         //else {
  136.                                 //check();
  137.                         //}
  138.                         //s++;
  139.                
  140.         }
  141.         }

  142.         /***同步方法开始**/
  143. //        private  synchronized void check() {//静态同步方法的锁是所在类的class对象,动态同步方法的锁为当前调用该方法的对象,也就是this指向的对象
  144. //                if (TICKETS > 0) {
  145. //                        try {
  146. //                                Thread.sleep(21);
  147. //                        } catch (InterruptedException e) {
  148. //                                // TODO Auto-generated catch block
  149. //                                e.printStackTrace();
  150. //                        }
  151. //                        System.out.println(Thread.currentThread().getName() + + (TICKETS--)+ "-----同步代方法" );
  152. //                }
  153. //        }
  154.         /***同步方法结束**/
  155. }
  156. /****售票代码结束****/       

  157. /*************退票窗口**********************/
  158. class SetTicketWindows implements Runnable{
  159.         public void run(){
  160.                 while(true){

  161.                                
  162.                        
  163.                                 synchronized(Depot.class){

  164.                                         if(y >= 300/* | TICKETSS == null*/){//如果没有票卖出,那就关闭退票窗口
  165.                                                
  166. /*                                                if(y==0){
  167.                                                         try {
  168.                                                                 Thread.sleep(200);
  169.                                                         } catch (InterruptedException e) {
  170.                                                                 // TODO Auto-generated catch block
  171.                                                                 e.printStackTrace();
  172.                                                         }
  173.                                                 }*/
  174.                                                
  175.                                                 try {
  176.                                                         this.wait();
  177.                                                 } catch (InterruptedException e) {
  178.                                                         // TODO Auto-generated catch block
  179.                                                         e.printStackTrace();
  180.                                                 }
  181.                                                 finally{
  182.                                                         this.notify();//告诉售票窗口,有票了
  183.                                                 }
  184.                                         }
  185.                                        
  186.                                         //if(/*(!TICKETSS.isEmpty()) | */y<299 ){//如果有票卖出,那么打开退票窗口
  187.                                                 try {
  188.                                                         Thread.sleep(27);
  189.                                                 } catch (InterruptedException e) {
  190.                                                         // TODO Auto-generated catch block
  191.                                                         e.printStackTrace();
  192.                                                 }
  193.                                                
  194. /*                                                for(int x = 1;x<=TICKETSS.size()300;x++){//初始值为100张
  195.                                                         TICKETSS.add(x);
  196.                                                 }*/
  197.                                                 synchronized(/*SetTicketWindows.class*/Depot.class){
  198.                                                         synchronized("A"){
  199.                                                                
  200.                                                                 y++;
  201.                                                                 TICKETSS.addFirst(NOT_TICKETSS.getFirst());//把下面的退票增加到未卖出车票列表中的第一个位置
  202.                                                                 System.out.println(Thread.currentThread().getName()
  203.                                                                                 +"票号:(" +  NOT_TICKETSS.pollFirst()+ ")余票:"+(y)/*(TICKETSS.size())"-----同步代码块"*/ );
  204.                                                         }
  205.                                                        
  206.                                                                         //System.out.print(  NOT_TICKETSS.pollFirst() );
  207.                                                                        
  208.                                                                         //System.out.println(")余票:"+(y)/*(TICKETSS.size())"-----同步代码块"*/ );
  209.                                                                        
  210.                                                         //NOT_TICKETSS.pollFirst();
  211.                                                 }
  212.                                 //        }
  213.                                        
  214.                                        
  215.                                 }
  216.                
  217.                 }
  218.                                
  219.         }
  220.        
  221.        
  222.        
  223. }
  224. }
复制代码




作者: (晶小羊)    时间: 2015-6-25 21:56
很尽力的看了- -请楼下作答- -
作者: (晶小羊)    时间: 2015-6-25 21:58
很尽力的看了- -请楼下作答- -
作者: 空城dream    时间: 2015-6-25 21:58
虽然看不懂 ,但顶了。。
作者: 0618-蔺飞飞    时间: 2015-6-25 21:58
膜拜一下大神
作者: nine789    时间: 2015-6-25 21:59
空城dream 发表于 2015-6-25 21:58
虽然看不懂 ,但顶了。。

非常感谢呀
作者: 青皮核桃    时间: 2015-6-25 21:59
其实我也不懂
作者: (晶小羊)    时间: 2015-6-25 22:00
很尽力的看了- -请楼下作答- -
作者: modric14    时间: 2015-6-25 22:03
感觉很厉害的样子
作者: jx5785749    时间: 2015-6-25 22:04
不错顶一个!
作者: keto    时间: 2015-6-25 22:04
表示已看晕@_@
作者: kkss834483739    时间: 2015-6-25 22:09
很尽力的看了- -请楼下作答- -
作者: nine789    时间: 2015-6-25 22:12
(晶小羊) 发表于 2015-6-25 21:56
很尽力的看了- -请楼下作答- -

有了大家的支持,楼下肯定有大神解答的
作者: 进军黑马    时间: 2015-6-25 22:12
默默的顶了
作者: tangpeng    时间: 2015-6-25 22:12
能写这莫多就不错了,不管写的怎么样!
作者: nine789    时间: 2015-6-25 22:13
(晶小羊) 发表于 2015-6-25 21:56
很尽力的看了- -请楼下作答- -

有了大家的支持,楼下肯定会出现大神出来解答的
作者: nine789    时间: 2015-6-25 22:22
0618-蔺飞飞 发表于 2015-6-25 21:58
膜拜一下大神

是姐夫吧,:lol

作者: 三樱椒    时间: 2015-6-25 23:37
用不用 其他的 编绎器?
作者: 木头人之死    时间: 2015-6-26 08:11
表示能不能写注释。。。
作者: 黑马无敌    时间: 2015-6-26 08:54
好长呀。。。。
作者: guiqulaixi    时间: 2015-6-26 12:03
楼主 看你的代码真的能哭晕在厕所里面,有一些很基础的东西你都搞错了, java.lang.IllegalThreadStateException
你知道为什么出现这种错么?? while(true){ .......  t1.start();  t2.start(); ...}你明明已经创建了一堆线程,你让它们一次又一次start()这是什么意思?线程启动只有一次start(),这种低级错误也能犯
第二个异常java.lang.IllegalMonitorStateException是因为你用的锁对象是错误的,你明明设置的锁对象是Depot.class,你却用this去notify和wait,请你好好看下文档,这又是一个低级错误
第三,你加那么多不必要的锁是何意?synchronized("A")这种同步代码块加了意义何在?你明明同步了共享资源
第四,建议你先搞清楚一个火车站多个窗口售票 退票问题,在搞多个火车站,我不太清楚你到底想怎样?是各个火车站各自卖自己的300票,还是多个火车站共享一个票源,建议你先把这些问题都想清楚,在写代码。

在你的基础上我改了些代码,是一个火车站的卖票系统,多个火车站我不打算搞
你可以参考下,如有不足,也欢迎指正:
  1. class Depot implements Runnable{
  2.   
  3.     LinkedList<Integer> TICKETSS = new LinkedList<Integer>();//未卖出车票
  4.     LinkedList<Integer> NOT_TICKETSS = new LinkedList<Integer>();//已卖出车票
  5.     int piaohao = 20150001;
  6.     int y = 300;
  7.    

  8.     Depot()
  9.     {
  10.             for(int x = 1;x<=300;x++){
  11.                     TICKETSS.add(piaohao++);               
  12.             }
  13.             
  14.             Iterator<Integer> it = TICKETSS.iterator();
  15.             while(it.hasNext())
  16.             {
  17.                     System.out.println(it.next());
  18.             }
  19.     }
  20.     public void run(){      
  21.                            
  22.             GetTicketWindows gtw = new GetTicketWindows();//售票窗口
  23.             SetTicketWindows stw = new SetTicketWindows();//退票窗口
  24.            

  25.             Thread t1 = new Thread(gtw);
  26.             Thread t2 = new Thread(gtw);
  27.             Thread t3 = new Thread(gtw);
  28.             Thread t4 = new Thread(gtw);
  29.            
  30.             Thread t5 = new Thread(stw);
  31.             Thread t6 = new Thread(stw);         
  32.                                                                              

  33.             t1.setName(Thread.currentThread().getName()+"窗口1:(售):");
  34.             t2.setName(Thread.currentThread().getName()+"窗口2:(售):");
  35.             t3.setName(Thread.currentThread().getName()+"窗口3:(售):");
  36.             t4.setName(Thread.currentThread().getName()+"窗口4:(售):");
  37.                                    
  38.             t5.setName(Thread.currentThread().getName()+"窗口[5]:【退】:");         
  39.             t6.setName(Thread.currentThread().getName()+"窗口[6]:【退】:");         
  40.                                     //t7.setName(Thread.currentThread().getName()+"窗口[7]:【退】:");
  41.                                                                               
  42.                                    
  43.             t1.start();        
  44.             t2.start();      
  45.             t3.start();        
  46.             t4.start();        
  47.                                    
  48.             t5.start();      
  49.             t6.start();      
  50.                            

  51.     }
  52.    
  53.     /************售票窗口***********************/
  54.     /**售票代码开始**/
  55.     private  class GetTicketWindows implements Runnable{
  56.             public void run() {
  57.                     // 重写Runnable中的run方法
  58.                     //int s = 0;
  59.                     while (true) {                                                                       
  60.                                     /****同步代码块开始******/

  61.                                     synchronized (Depot.class) {//error---java.lang.IllegalMonitorStateException
  62.                                             while(/*TICKETSS.isEmpty()  */ /*TICKETSS.size()< = y*/y<=0){//如果没有票,那就等待
  63.                                                    
  64.                                                     try {
  65.                                                             Depot.class.wait();
  66.                                                     } catch (InterruptedException e) {
  67.                                                             // TODO Auto-generated catch block
  68.                                                             e.printStackTrace();
  69.                                                     }finally{
  70.                                                             Depot.class.notifyAll();
  71.                                                     }
  72.                                             }      
  73.                                          
  74.                                            try {
  75.                                                     Thread.sleep(27);//出票等待时间
  76.                                                } catch (InterruptedException e) {                                                                  
  77.                                                     e.printStackTrace();
  78.                                                }                                                                                                                                 
  79.                                                                            
  80.                                                  y--;
  81.                                                  NOT_TICKETSS.addFirst(TICKETSS.poll());//把下面这张卖出去的票增加到已卖出车票列表的第一个位置
  82.                                                  System.out.println(Thread.currentThread().getName()
  83.                                                                        +"票号:("+  NOT_TICKETSS.getFirst() + ")余票:"+ (y)/*(TICKETSS.size())"-----同步代码块"*/ );
  84.                                                                                                                                                                                                 
  85.                                                            
  86.                                             }
  87.                                     }
  88.                            
  89.             }
  90.      }

  91.    
  92.     class SetTicketWindows implements Runnable{
  93.         public void run(){
  94.                 while(true){                                                   
  95.                                 synchronized(Depot.class){

  96.                                         while(y >= 300)//如果没有票卖出,那就关闭退票窗口
  97.                                         {
  98.                                                                                           
  99.                                                 try {
  100.                                                         Depot.class.wait();
  101.                                                 } catch (InterruptedException e) {
  102.                                                         // TODO Auto-generated catch block
  103.                                                         e.printStackTrace();
  104.                                                 }
  105.                                                 finally{
  106.                                                         Depot.class.notifyAll();//告诉售票窗口,有票了
  107.                                                 }
  108.                                         }
  109.                                                                                                                                                                                     
  110.                                          y++;
  111.                                          TICKETSS.addFirst(NOT_TICKETSS.poll());//把下面的退票增加到未卖出车票列表中的第一个位置
  112.                                          System.out.println(Thread.currentThread().getName()
  113.                                                              +"票号:(" +  TICKETSS.getFirst()+ ")余票:"+(y)/*(TICKETSS.size())"-----同步代码块"*/ );
  114.                                                             
  115.                                             
  116.                                                                               
  117.                                 }
  118.                
  119.                 }
  120.                               
  121.         }
  122.     }
  123.       
  124.       
  125. }
复制代码

作者: nine789    时间: 2015-6-27 00:03
guiqulaixi 发表于 2015-6-26 12:03
楼主 看你的代码真的能哭晕在厕所里面,有一些很基础的东西你都搞错了, java.lang.IllegalThreadStateExce ...

我已经优化过代码了,如果不是多个火车站,那又有什么意思呢?

这是多线程同步嵌套问题,

但还是感谢你的帮助,一会我把优化过后的代码发上去
作者: nine789    时间: 2015-6-27 00:38
guiqulaixi 发表于 2015-6-26 12:03
楼主 看你的代码真的能哭晕在厕所里面,有一些很基础的东西你都搞错了, java.lang.IllegalThreadStateExce ...

不知道 这样优化后,你感觉怎么样

但是其中还是会出现一些问题
**********测试类
        Depot dt = new Depot();

        Thread x1 = new Thread(dt);
        Thread x2 = new Thread(dt);
        Thread x3 = new Thread(dt);
        Thread x4 = new Thread(dt);
       
        x1.setName("北京站:");
        x2.setName("广州站:");
        x3.setName("携程网:");
        x4.setName("12306:");
       
        x1.start();
        x2.start();
        x3.start();
        x4.start();
*************
  1. package review;

  2. import java.util.Calendar;
  3. import java.util.LinkedList;

  4. /************ 火车站 *************************/
  5. public class Depot implements Runnable {
  6.         LinkedList<Integer> TICKETSS = new LinkedList<Integer>();// 未卖出车票
  7.         LinkedList<Integer> NOT_TICKETSS = new LinkedList<Integer>();// 已卖出车票
  8.         int piaohao = 20150001;
  9.         static int y = 300;

  10.         Depot() {
  11.                 for (int x = 1; x <= 300; x++) {// 初始值为100张
  12.                         TICKETSS.add(piaohao);
  13.                         piaohao++;
  14.                 }
  15.         }

  16.         public void run() {
  17.                 GetTicketWindows gtw = new GetTicketWindows();// 售票窗口
  18.                 SetTicketWindows stw = new SetTicketWindows();// 退票窗口
  19.                 Thread t1 = new Thread(gtw);
  20.                 Thread t2 = new Thread(gtw);
  21.                 Thread t3 = new Thread(gtw);
  22.                 Thread t4 = new Thread(gtw);
  23.                 Thread t5 = new Thread(stw);
  24.                 Thread t6 = new Thread(stw);
  25.                 synchronized ("F") {
  26.                         try {
  27.                                 Thread.sleep(220);
  28.                         } catch (InterruptedException e) {
  29.                                 // TODO Auto-generated catch block
  30.                                 e.printStackTrace();
  31.                         }
  32.                         if (Thread.currentThread().getName().equals("12306:")
  33.                                         | Thread.currentThread().getName().equals("携程网:")) {
  34.                                 t4.setName(Thread.currentThread().getName() + "(网):(售):");
  35.                                 t4.start();
  36.                                 t6.setName(Thread.currentThread().getName() + "(网):【退】:");
  37.                                 t6.start();
  38.                         }
  39.                         if (Thread.currentThread().getName().equals("北京站:")
  40.                                         | Thread.currentThread().getName().equals("广州站:")) {
  41.                                 t1.setName(Thread.currentThread().getName() + "窗口1:(售):");
  42.                                 t2.setName(Thread.currentThread().getName() + "窗口2:(售):");
  43.                                 t3.setName(Thread.currentThread().getName() + "窗口3:(售):");
  44.                                 t5.setName(Thread.currentThread().getName() + "窗口[5]:【退】:");
  45.                                 t1.start();
  46.                                 /*
  47.                                  * 多线程嵌套同步问题 火车站3:窗口[5]:【退】:票号:【+null】余票:301 Exception in thread
  48.                                  * "火车站4:" java.lang.IllegalThreadStateException at
  49.                                  * java.lang.Thread.start(Thread.java:704) at
  50.                                  * review.Depot.run(Depot.java:71) at
  51.                                  * java.lang.Thread.run(Thread.java:744)
  52.                                  * 火车站3:窗口4:(售):票号:(-null)余票:300
  53.                                  */
  54.                                 t2.start();
  55.                                 t3.start();
  56.                                 t5.start();
  57.                         }
  58.                 }
  59.         }

  60.         /************ 售票窗口 ***********************/
  61.         /** 售票代码开始 **/
  62.         private class GetTicketWindows implements Runnable {
  63.                 public void run() {
  64.                         while (true) {
  65.                                 if (y <= 0) {// 如果没有票,那就等待
  66.                                 // Thread.yield();
  67.                                 /*
  68.                                  * 多线程嵌套同步问题 火车站3:窗口[5]:【退】:票号:【+null】余票:301 Exception in thread
  69.                                  * "火车站4:" java.lang.IllegalThreadStateException at
  70.                                  * java.lang.Thread.start(Thread.java:704) at
  71.                                  * review.Depot.run(Depot.java:71) at
  72.                                  * java.lang.Thread.run(Thread.java:744)
  73.                                  * 火车站3:窗口4:(售):票号:(-null)余票:300
  74.                                  */
  75.                                         try {
  76.                                                 this.wait();
  77.                                                 /**
  78.                                                  * 暂停所有售票线程问题
  79.                                                  */
  80.                                         } catch (InterruptedException e) {
  81.                                                 // TODO Auto-generated catch block
  82.                                                 e.printStackTrace();
  83.                                         }
  84.                                         // Thread.yield(); //线程等待
  85.                                 }
  86.                                 try {
  87.                                         Thread.sleep(135);// 出票等待时间
  88.                                 } catch (InterruptedException e) {
  89.                                         // TODO Auto-generated catch block
  90.                                         e.printStackTrace();
  91.                                 }
  92.                                 if (TICKETSS != null & y > 0) { // 如果卖票池有票,并且数量大于0
  93.                                         String wuzuo = "";
  94.                                         String tiaoshi = "";
  95.                                         Calendar calendar = Calendar.getInstance();
  96.                                         int shi = calendar.get(Calendar.HOUR_OF_DAY);
  97.                                         int fen = calendar.get(Calendar.MINUTE);
  98.                                         int miao = calendar.get(Calendar.SECOND);
  99.                                         int hao = calendar.get(Calendar.MILLISECOND);
  100.                                         if (y >= 180) { // 180--300=120硬座
  101.                                                 wuzuo = "****硬座****";
  102.                                         } else if (y >= 160) {// 160--179=20软卧
  103.                                                 wuzuo = "****软卧****";
  104.                                         } else if (y >= 110) {// 110--159=50二等座
  105.                                                 wuzuo = "****二等座****";
  106.                                                 ;
  107.                                         } else if (y >= 80) {// 80--109=30硬卧
  108.                                                 wuzuo = "****硬卧****";
  109.                                         } else if (y >= 40) {// 40--79=40一等座
  110.                                                 wuzuo = "****一等座****";
  111.                                         } else if (y < 40) {
  112.                                                 wuzuo = "****无座****";
  113.                                                 if (TICKETSS == null) { // 无票
  114.                                                         tiaoshi = "------------------------系统调时";
  115.                                                 }
  116.                                         }
  117.                                         synchronized ("C") {
  118.                                                 if (y == 300) {
  119.                                                         y = 300;
  120.                                                 }
  121.                                                 --y;
  122.                                                 if (y >= 0) {
  123.                                                         System.out.println(Thread.currentThread().getName()
  124.                                                                         + "票号:(-" + TICKETSS.peek() + wuzuo
  125.                                                                         + ")余票:" + (y) + tiaoshi + ":当前时间:" + shi
  126.                                                                         + ":" + fen + ":" + miao + ":" + hao/*
  127.                                                                                                                                                  * (TICKETSS
  128.                                                                                                                                                  * .
  129.                                                                                                                                                  * size(
  130.                                                                                                                                                  * ))
  131.                                                                                                                                                  * "-----同步代码块"
  132.                                                                                                                                                  */);
  133.                                                         NOT_TICKETSS.addFirst(TICKETSS.pollFirst());// 把下面这张卖出去的票增加到已卖出车票列表的第一个位置
  134.                                                 }
  135.                                         }
  136.                                 }
  137.                         }
  138.                 }
  139.         }

  140.         /**** 售票代码结束 ****/

  141.         /************* 退票窗口 **********************/
  142.         class SetTicketWindows implements Runnable {
  143.                 public void run() {
  144.                         while (true) {
  145.                                 if (y >= 300) {// 如果没有票卖出,那就关闭退票窗口
  146.                                         // Thread.yield();
  147.                                         /*
  148.                                          * 多线程嵌套同步问题 火车站3:窗口[5]:【退】:票号:【+null】余票:301 Exception in
  149.                                          * thread "火车站4:" java.lang.IllegalThreadStateException at
  150.                                          * java.lang.Thread.start(Thread.java:704) at
  151.                                          * review.Depot.run(Depot.java:71) at
  152.                                          * java.lang.Thread.run(Thread.java:744)
  153.                                          * 火车站3:窗口4:(售):票号:(-null)余票:300
  154.                                          */

  155.                                         try {
  156.                                                 this.wait();
  157.                                                 /**
  158.                                                  * 关闭所有退票窗口
  159.                                                  */
  160.                                         } catch (InterruptedException e) {
  161.                                                 // TODO Auto-generated catch block
  162.                                                 e.printStackTrace();
  163.                                         } finally {
  164.                                                 this.notify();
  165.                                                 /**
  166.                                                  * 开启所有售票窗口
  167.                                                  */
  168.                                         }
  169.                                 }
  170.                                 try {
  171.                                         Thread.sleep(317);
  172.                                 } catch (InterruptedException e) {
  173.                                         // TODO Auto-generated catch block
  174.                                         e.printStackTrace();
  175.                                 }
  176.                                 if (NOT_TICKETSS != null & y > 0) { // 如果退票池有票,并且数量大于0
  177.                                         String tiaoshi2 = "";
  178.                                         Calendar calendar = Calendar.getInstance();
  179.                                         int shi = calendar.get(Calendar.HOUR_OF_DAY);
  180.                                         int fen = calendar.get(Calendar.MINUTE);
  181.                                         int miao = calendar.get(Calendar.SECOND);
  182.                                         int hao = calendar.get(Calendar.MILLISECOND);

  183.                                         if (y < 40) {
  184.                                                 // wuzuo2 = "****无座****";
  185.                                                 /*
  186.                                                  * if(TICKETSS == null ){ //无票 tiaoshi2 =
  187.                                                  * "------------------------系统调时"; }
  188.                                                  */
  189.                                         }
  190.                                         synchronized ("C") {
  191.                                                 if (y == 300) {
  192.                                                         y = 300;
  193.                                                 }
  194.                                                 ++y;
  195.                                                 if (y <= 300) {
  196.                                                         System.out.println(Thread.currentThread().getName()
  197.                                                                         + "票号:【+" + NOT_TICKETSS.peek() + "】余票:"
  198.                                                                         + (y) + tiaoshi2 + ":当前时间:" + shi + ":"
  199.                                                                         + fen + ":" + miao + ":" + hao/*
  200.                                                                                                                                  * (TICKETSS.size
  201.                                                                                                                                  * (
  202.                                                                                                                                  * ))"-----同步代码块"
  203.                                                                                                                                  */);
  204.                                                         TICKETSS.addFirst(NOT_TICKETSS.pollFirst());// 把下面的退票增加到未卖出车票列表中的第一个位置
  205.                                                 }
  206.                                         }
  207.                                 }
  208.                         }
  209.                 }
  210.         }
  211. }
复制代码

作者: guiqulaixi    时间: 2015-6-27 09:53
nine789 发表于 2015-6-27 00:03
我已经优化过代码了,如果不是多个火车站,那又有什么意思呢?

这是多线程同步嵌套问题,

楼主,你到现在对于一些基础的知识都没理解透 ,就想写复杂的代码,我只能说你自己好自为之吧,优化的代码还是有低级错误,不好意思,实在不想看完了。
作者: nine789    时间: 2015-6-27 21:47
guiqulaixi 发表于 2015-6-27 09:53
楼主,你到现在对于一些基础的知识都没理解透 ,就想写复杂的代码,我只能说你自己好自为之吧,优化的代 ...

我基础就是不好,算了
作者: 崔司龙    时间: 2015-6-27 22:04
看起来很厉害的样子...
作者: Wait_For    时间: 2015-6-27 22:31
求大神出现啊!
作者: nine789    时间: 2015-6-27 22:44
  1. package Thread_1;

  2. public class ThreadTicketsDemo_1 {
  3.         public static void main(String[] args) {
  4.                 Depot dt = new Depot();

  5.                 Thread x1 = new Thread(dt);
  6.                 Thread x2 = new Thread(dt);
  7.                 Thread x3 = new Thread(dt);
  8.                 Thread x4 = new Thread(dt);
  9.                
  10.                 x1.setName("北京站:");
  11.                 x2.setName("广州站:");
  12.                 x3.setName("携程网:");
  13.                 x4.setName("12306:");
  14.                
  15.                 x1.start();
  16.                 x3.start();
  17.                 x2.start();
  18.                 x4.start();
  19.         }
  20. }
复制代码

作者: nine789    时间: 2015-6-28 11:19
nine789 发表于 2015-6-27 22:44
  1. package Thread_1;

  2. import java.util.Calendar;
  3. import java.util.LinkedList;

  4. /************ 火车站 *************************/
  5. public class Depot implements Runnable {

  6.         LinkedList<Integer> TICKETSS = new LinkedList<Integer>();// 未卖出车票
  7.         LinkedList<Integer> NOT_TICKETSS = new LinkedList<Integer>();// 已卖出车票
  8.         int piaohao = 20150001;
  9.         static int y = 300;
  10.         int e = 0;
  11.         int d = 0;
  12.         Depot() {
  13.                 for (int x = 1; x <= /* TICKETSS.size() */300; x++) {// 初始值为100张
  14.                         TICKETSS.add(piaohao);
  15.                         piaohao++;
  16.                 }
  17.         }
  18.         TicketsHall_1 th1 = new TicketsHall_1();
  19.         Thread h1 = new Thread(th1);//北京站专用
  20.         Thread h2 = new Thread(th1);//广州站专用
  21.        
  22.         Thread h3 = new Thread(th1);//12306专用
  23.         //Thread h4 = new Thread(th1);//携程网专用

  24.         public void run() {
  25.                 //TicketsHall_2 th2 = new TicketsHall_2();
  26.                
  27.                 try {
  28.                         Thread.sleep((long) (Math.random()*100));
  29.                        
  30.                 } catch (InterruptedException e) {
  31.                         // TODO Auto-generated catch block
  32.                         e.printStackTrace();
  33.                 }
  34.                 //synchronized("YU"){
  35.                 if (Thread.currentThread().getName().equals("北京站:")
  36.                                 | Thread.currentThread().getName().equals("广州站:")) {
  37.                         h1.setName(Thread.currentThread().getName() + "第1售票厅:");
  38.                         h2.setName(Thread.currentThread().getName() + "第2售票厅:");
  39. /*                        try {
  40.                                 Thread.sleep(147);
  41.                         } catch (InterruptedException e) {
  42.                                 // TODO Auto-generated catch block
  43.                                 e.printStackTrace();
  44.                         }*/


  45.                                
  46.                         ++e;
  47.                         h1.start();
  48. //                                if(e%2==0){
  49. //                                        h1.start();
  50.                                         try {
  51.                                                 Thread.sleep((long) (Math.random()*100));
  52.                                         } catch (InterruptedException e1) {
  53.                                                 // TODO Auto-generated catch block
  54.                                                 e1.printStackTrace();
  55.                                         }
  56. //                                }
  57.                                 h2.start();
  58.                                
  59. /*                                if(e%2==0){
  60.                                         ++e;

  61.                                         Thread.yield();
  62.                                 }*/

  63.                 }
  64.                
  65.                 if (Thread.currentThread().getName().equals("12306:")
  66.                                 | Thread.currentThread().getName().equals("携程网:")){
  67.                         h3.setName(Thread.currentThread().getName() + "(网):");//12306专用
  68.                         //h4.setName(Thread.currentThread().getName() + "(网):");
  69.                         try {
  70.                                 Thread.sleep((long) (Math.random()*100));
  71.                         } catch (InterruptedException e) {
  72.                                 // TODO Auto-generated catch block
  73.                                 e.printStackTrace();
  74.                         }
  75. /*                        int XYZ = 0;
  76.                         if(XYZ%2==0){
  77.                                 h3.start();
  78.                                 ++XYZ;
  79.                         }*/
  80. //                        if(e%2==1){
  81. //                                ++e;
  82. //                                try {
  83. //                                        Thread.sleep(50000);
  84. //                                } catch (InterruptedException e1) {
  85. //                                        // TODO Auto-generated catch block
  86. //                                        e1.printStackTrace();
  87. //                                }
  88. //                        }
  89.                         h3.start();
  90.                         try {
  91.                                 Thread.sleep((long) (Math.random()*100));
  92.                         } catch (InterruptedException e) {
  93.                                 // TODO Auto-generated catch block
  94.                                 e.printStackTrace();
  95.                         }
  96. /*                        if(d%2==0){
  97.                                 ++d;

  98.                                 Thread.yield();
  99.                         }*/
  100.                        
  101.                        
  102.                 //        Thread.yield();
  103.                 }
  104.         //}
  105. /*                GetTicketWindows gtw = new GetTicketWindows();// 售票窗口
  106.                 SetTicketWindows stw = new SetTicketWindows();// 退票窗口

  107.                 Thread t5 = null;
  108.                 Thread t1 = new Thread(gtw);
  109.                 Thread t2 = new Thread(gtw);
  110.                 Thread t3 = new Thread(gtw);
  111.                 Thread t4 = new Thread(gtw);
  112.                 if (t5 == null) {
  113.                         t5 = new Thread(stw);
  114.                 }
  115.                 Thread t6 = new Thread(stw);

  116.                 try {
  117.                         Thread.sleep(220);
  118.                 } catch (InterruptedException e) {
  119.                         // TODO Auto-generated catch block
  120.                         e.printStackTrace();
  121.                 }

  122.                 if (Thread.currentThread().getName().equals("12306:")
  123.                                 | Thread.currentThread().getName().equals("携程网:")) {
  124.                         if (y > 0) { // 如果有票,那就打开售票窗口
  125.                                 t4.setName(Thread.currentThread().getName() + "(网):(售):");
  126.                                 t4.start();
  127.                         }
  128.                         if (y <= 300) {// 如果有票卖出,就打开退票功能
  129.                                 t6.setName(Thread.currentThread().getName() + "(网):【退】:");
  130.                                 t6.start();
  131.                         }
  132.                 }

  133.                 if (Thread.currentThread().getName().equals("北京站:")
  134.                                 | Thread.currentThread().getName().equals("广州站:")) {
  135.                         if (y > 0) {// 如果有票,我就打开售票窗口
  136.                                 t1.setName(Thread.currentThread().getName() + "窗口1:(售):");
  137.                                 t2.setName(Thread.currentThread().getName() + "窗口2:(售):");
  138.                                 t3.setName(Thread.currentThread().getName() + "窗口3:(售):");

  139.                                 t1.start();
  140.                                
  141.                                  * 多线程嵌套同步问题 火车站3:窗口[5]:【退】:票号:【+null】余票:301 Exception in thread
  142.                                  * "火车站4:" java.lang.IllegalThreadStateException at
  143.                                  * java.lang.Thread.start(Thread.java:704) at
  144.                                  * review.Depot.run(Depot.java:71) at
  145.                                  * java.lang.Thread.run(Thread.java:744)
  146.                                  * 火车站3:窗口4:(售):票号:(-null)余票:300
  147.                                  
  148.                                 t2.start();
  149.                                 t3.start();
  150.                         }
  151.                         if (y <= 300) { // 如果有票卖出,那我就打开退票窗口
  152.                                 t5.setName(Thread.currentThread().getName() + "窗口[5]:【退】:");
  153.                                 // t7.setName(Thread.currentThread().getName()+"窗口[7]:【退】:");
  154.                                 t5.start();
  155.                                 // t7.start();
  156.                         }
  157.                 }*/
  158.         }
  159.        
  160. /***********第一售票厅**************/
  161.         private class TicketsHall_1 implements Runnable{
  162.                
  163.                 GetTicketWindows gtw1 = new GetTicketWindows();// 售票窗口
  164.                 SetTicketWindows stw1 = new SetTicketWindows();// 退票窗口
  165.                
  166.                 Thread t1 = new Thread(gtw1);
  167.                 Thread t2 = new Thread(gtw1);
  168.                 Thread t3 = new Thread(gtw1);
  169.                 Thread t4 = new Thread(gtw1);
  170.                
  171.                 Thread t5 = new Thread(stw1);
  172.                 Thread t6 = new Thread(stw1);
  173.                 public void run(){
  174.                        
  175.                        

  176.                         try {
  177.                                 Thread.sleep((long) (Math.random()*100));
  178.                         } catch (InterruptedException e) {
  179.                                 // TODO Auto-generated catch block
  180.                                 e.printStackTrace();
  181.                         }
  182. //synchronized("YU"){
  183.                         if (Thread.currentThread().equals(h3)) {///////////////////////////////////////////////////////////////////
  184.                                 if (y > 0) { // 如果有票,那就打开售票窗口
  185.                                         t4.setName(Thread.currentThread().getName() + "自助:(售):");
  186.                                         t4.start();
  187.                                 }
  188.                                 if (y <= 300) {// 如果有票卖出,就打开退票功能
  189.                                         t6.setName(Thread.currentThread().getName() + "自助:【退】:");
  190.                                         t6.start();
  191.                                 }
  192.                         }

  193.                        
  194.                         if (Thread.currentThread().equals(h1)
  195.                                         | Thread.currentThread().equals(h2)) {
  196.                                
  197.                                 if (y > 0) {// 如果有票,我就打开售票窗口
  198.                                         t1.setName(Thread.currentThread().getName() + "窗口1:(售):");
  199.                                         t2.setName(Thread.currentThread().getName() + "窗口2:(售):");
  200.                                         t3.setName(Thread.currentThread().getName() + "自助1:(售):");

  201.                                         t1.start();
  202.                                         /*
  203.                                          * 多线程嵌套同步问题 火车站3:窗口[5]:【退】:票号:【+null】余票:301 Exception in thread
  204.                                          * "火车站4:" java.lang.IllegalThreadStateException at
  205.                                          * java.lang.Thread.start(Thread.java:704) at
  206.                                          * review.Depot.run(Depot.java:71) at
  207.                                          * java.lang.Thread.run(Thread.java:744)
  208.                                          * 火车站3:窗口4:(售):票号:(-null)余票:300
  209.                                          */
  210.                                         t2.start();
  211.                                         t3.start();
  212.                                 }
  213.                                 if (y <= 300) { // 如果有票卖出,那我就打开退票窗口
  214.                                         // t7.setName(Thread.currentThread().getName()+"窗口[7]:【退】:");
  215.                                         t5.setName(Thread.currentThread().getName() + "窗口[3]:【退】:");
  216.                                         t5.start();
  217.                                         // t7.start();
  218.                                 }
  219.                         }
  220.                 //}
  221.                 }
  222.         }
  223.        
  224. /***********第一售票厅*结束*************/       
复制代码

作者: nine789    时间: 2015-6-28 11:20
nine789 发表于 2015-6-28 11:19
  1. /***********第一售票厅*结束*************/       

  2.        
  3.         /************ 售票窗口 ***********************/
  4.         /** 售票代码开始 **/
  5.         private class GetTicketWindows implements Runnable {
  6.                 public void run() {
  7.                         if (y > 0) { // 如果有票就打开售票系统
  8.                                 while (true) {
  9.                                         if (/* TICKETSS.isEmpty() *//* TICKETSS.size()< = y */y <= 0) {// 如果没有票,那就等待
  10.                                         // Thread.yield();

  11.                                                 /*
  12.                                                  * 多线程嵌套同步问题 火车站3:窗口[5]:【退】:票号:【+null】余票:301 Exception
  13.                                                  * in thread "火车站4:"
  14.                                                  * java.lang.IllegalThreadStateException at
  15.                                                  * java.lang.Thread.start(Thread.java:704) at
  16.                                                  * review.Depot.run(Depot.java:71) at
  17.                                                  * java.lang.Thread.run(Thread.java:744)
  18.                                                  * 火车站3:窗口4:(售):票号:(-null)余票:300
  19.                                                  */
  20.                                         }
  21.                                         try {
  22.                                                 Thread.sleep((long) (Math.random()*100));// 出票等待时间
  23.                                         } catch (InterruptedException e) {
  24.                                                 // TODO Auto-generated catch block
  25.                                                 e.printStackTrace();
  26.                                         }
  27.                                         if (TICKETSS != null & y > 0) { // 如果卖票池有票,并且数量大于0
  28.                                                 String wuzuo = "";
  29.                                                 String tiaoshi = "";
  30.                                                 Calendar calendar = Calendar.getInstance();
  31.                                                 int shi = calendar.get(Calendar.HOUR_OF_DAY);
  32.                                                 int fen = calendar.get(Calendar.MINUTE);
  33.                                                 int miao = calendar.get(Calendar.SECOND);
  34.                                                 int hao = calendar.get(Calendar.MILLISECOND);
  35.                                                 if (y >= 180) { // 180--300=120硬座
  36.                                                         wuzuo = "****硬座****";
  37.                                                 } else if (y >= 160) {// 160--179=20软卧
  38.                                                         wuzuo = "****软卧****";
  39.                                                 } else if (y >= 110) {// 110--159=50二等座
  40.                                                         wuzuo = "****二等座****";
  41.                                                         ;
  42.                                                 } else if (y >= 80) {// 80--109=30硬卧
  43.                                                         wuzuo = "****硬卧****";
  44.                                                 } else if (y >= 40) {// 40--79=40一等座
  45.                                                         wuzuo = "****一等座****";
  46.                                                 } else if (y < 40) {
  47.                                                         wuzuo = "****无座****";
  48.                                                         if (TICKETSS == null) { // 无票
  49.                                                                 tiaoshi = "------------------------系统调时";
  50.                                                         }
  51.                                                 }

  52.                                                 /**** 同步代码块开始 ******/
  53.                                                 synchronized ("C") {
  54.                                                         if (y == 300) {
  55.                                                                 y = 300;
  56.                                                         }
  57.                                                         --y;
  58.                                                         if (y >= 0) {
  59.                                                                 System.out.println(Thread.currentThread()
  60.                                                                                 .getName()
  61.                                                                                 + "票号:(-"
  62.                                                                                 + TICKETSS.peek()
  63.                                                                                 + wuzuo
  64.                                                                                 + ")余票:"
  65.                                                                                 + (y)
  66.                                                                                 + tiaoshi
  67.                                                                                 + ":当前时间:"
  68.                                                                                 + shi
  69.                                                                                 + ":"
  70.                                                                                 + fen
  71.                                                                                 + ":"
  72.                                                                                 + miao + ":" + hao/*
  73.                                                                                                                  * (TICKETSS.size())
  74.                                                                                                                  * "-----同步代码块"
  75.                                                                                                                  */);
  76.                                                                 NOT_TICKETSS.addFirst(TICKETSS.pollFirst());// 把下面这张卖出去的票增加到已卖出车票列表的第一个位置
  77.                                                                 try {// 出票等待
  78.                                                                         Thread.sleep((long) (Math.random()*100));
  79.                                                                 } catch (InterruptedException e) {
  80.                                                                         // TODO Auto-generated catch block
  81.                                                                         e.printStackTrace();
  82.                                                                 }

  83.                                                                 if (y == 0) {
  84.                                                                         // Thread.stop();
  85.                                                                         break;
  86.                                                                 }
  87.                                                         } else {
  88.                                                                 ++y;
  89.                                                                 System.out.println("其他线程可能执行到的语句"
  90.                                                                                 + Thread.currentThread().getName()
  91.                                                                                 + "余票:" + y);
  92.                                                                 // 内测
  93.                                                         }
  94.                                                 }
  95.                                                 /****** 同步代码块结束 *******/
  96.                                         }
  97.                                 }// while结束
  98.                                 /*********** 内测开始 **************/
  99.                                 System.out.println(Thread.currentThread().getName()
  100.                                                 + "票已售完,请等待(1)");
  101.                                 {
  102.                                         System.out.println(Thread.currentThread().getName()
  103.                                                         + "票已售完,请等待(2)");
  104.                                         // stw.run();
  105.                                         // det.stw.run();
  106.                                         try {
  107.                                                 Thread.sleep((long) (Math.random()*100));
  108.                                         } catch (InterruptedException e) {
  109.                                                 // TODO Auto-generated catch block
  110.                                                 e.printStackTrace();
  111.                                         }
  112.                                         // Depot.run();
  113.                                 }
  114.                                 /*********** 内测结束 ***************/
  115.                         }// if 结束
  116.                         System.out
  117.                                         .println(Thread.currentThread().getName() + "票已售完,请等待(3)"); // 内测
  118.                 }// run结束

  119.                 /*** 同步方法开始 **/
  120.                 // private synchronized void check()
  121.                 // {//静态同步方法的锁是所在类的class对象,动态同步方法的锁为当前调用该方法的对象,也就是this指向的对象
  122.                 // if (TICKETS > 0) {
  123.                 // try {
  124.                 // Thread.sleep(21);
  125.                 // } catch (InterruptedException e) {
  126.                 // // TODO Auto-generated catch block
  127.                 // e.printStackTrace();
  128.                 // }
  129.                 // System.out.println(Thread.currentThread().getName() + + (TICKETS--)+
  130.                 // "-----同步代方法" );
  131.                 // }
  132.                 // }
  133.                 /*** 同步方法结束 **/
  134.         }

  135.         /**** 售票代码结束 ****/

  136.         /************* 退票窗口 **********************/
  137.         private class SetTicketWindows implements Runnable {
  138.                 public void run() {
  139.                         while (true) {
  140.                                 if (y >= 300/* | TICKETSS == null */) {// 如果没有票卖出,那就关闭退票窗口

  141.                                         Thread.yield();
  142.                                         /*
  143.                                          * 多线程嵌套同步问题 火车站3:窗口[5]:【退】:票号:【+null】余票:301 Exception in
  144.                                          * thread "火车站4:" java.lang.IllegalThreadStateException at
  145.                                          * java.lang.Thread.start(Thread.java:704) at
  146.                                          * review.Depot.run(Depot.java:71) at
  147.                                          * java.lang.Thread.run(Thread.java:744)
  148.                                          * 火车站3:窗口4:(售):票号:(-null)余票:300
  149.                                          */
  150.                                 }

  151.                                 try {
  152.                                         Thread.sleep((long) (Math.random()*100));
  153.                                 } catch (InterruptedException e) {
  154.                                         // TODO Auto-generated catch block
  155.                                         e.printStackTrace();
  156.                                 }

  157.                                 if (NOT_TICKETSS != null & y > 0) { // 如果退票池有票,并且数量大于0

  158.                                         String tiaoshi2 = "";
  159.                                         Calendar calendar = Calendar.getInstance();
  160.                                         int shi = calendar.get(Calendar.HOUR_OF_DAY);
  161.                                         int fen = calendar.get(Calendar.MINUTE);
  162.                                         int miao = calendar.get(Calendar.SECOND);
  163.                                         int hao = calendar.get(Calendar.MILLISECOND);

  164.                                         if (y < 40) {
  165.                                                 // wuzuo2 = "****无座****";
  166.                                                 /*
  167.                                                  * if(TICKETSS == null ){ //无票 tiaoshi2 =
  168.                                                  * "------------------------系统调时"; }
  169.                                                  */
  170.                                         }
  171.                                         synchronized ("C") {
  172.                                                 if (y >= 300) {
  173.                                                         y = 300;
  174.                                                 }
  175.                                                 ++y;
  176.                                                 if (y <= 300) {

  177.                                                         System.out.println(Thread.currentThread().getName()
  178.                                                                         + "票号:【+" + NOT_TICKETSS.peek() + "】余票:"
  179.                                                                         + (y) + tiaoshi2 + ":当前时间:" + shi + ":"
  180.                                                                         + fen + ":" + miao + ":" + hao/*
  181.                                                                                                                                  * (TICKETSS.size
  182.                                                                                                                                  * (
  183.                                                                                                                                  * ))"-----同步代码块"
  184.                                                                                                                                  */);
  185.                                                         TICKETSS.addFirst(NOT_TICKETSS.pollFirst());// 把下面的退票增加到未卖出车票列表中的第一个位置

  186.                                                         try {// 出票等待
  187.                                                                 Thread.sleep((long) (Math.random()*100));
  188.                                                         } catch (InterruptedException e) {
  189.                                                                 // TODO Auto-generated catch block
  190.                                                                 e.printStackTrace();
  191.                                                         }

  192.                                                         if (y == 0) {
  193.                                                                 // Thread.stop();
  194.                                                                 break;
  195.                                                         }
  196.                                                 } else {
  197.                                                         --y;
  198.                                                         System.out.println("其他线程可能执行到的语句"
  199.                                                                         + Thread.currentThread().getName() + "余票:"
  200.                                                                         + y);
  201.                                                 }
  202.                                         }
  203.                                 }
  204.                         }
  205.                 }
  206.         }
  207. }
复制代码





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