黑马程序员技术交流社区

标题: while-switch [打印本页]

作者: zl78365336    时间: 2013-12-9 14:09
标题: while-switch
本帖最后由 zl78365336 于 2013-12-9 17:50 编辑

ServiceWindow类中,老师讲说,while语句放在switch外面效率低,while语句放在switch里面更高效,要怎么放呢?



  1.         public void start(){
  2.                 Executors.newSingleThreadExecutor().execute(new Runnable(){
  3.                         public void run(){
  4.                                 while(true){
  5.                                         switch(type){
  6.                                         case COMMON:
  7.                                                 commonService();
  8.                                                 break;
  9.                                         case EXPRESS:
  10.                                                 expressService();
  11.                                                 break;
  12.                                         case VIP:
  13.                                                 vipService();
  14.                                                 break;
  15.                                         }
  16.                                 }
  17.                         }
  18.                 });
  19.         }



复制代码


作者: 王新年    时间: 2013-12-9 16:17
只需要将while语句嵌套在case中,如下代码:
public stati void  start(){
     Executors.newSingleThreadExecutor().execute(new Runnable(){
          public void run(){
              switch(type){
               case COMMON: while(true){commonService();} break;
               case EXPRESS: while(true){expressService();} break;
               case VIP: while(true){vipService();} break;


                }
              }
     } )
}




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