A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 谭荣强 高级黑马   /  2014-5-21 18:33  /  1855 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public static void main(String[] args) {  
  2. 13.        //产生4个普通窗口  
  3. 14.        for(int i=1;i<5;i++){  
  4. 15.            ServiceWindow window =  new ServiceWindow();  
  5. 16.            window.setNumber(i);  
  6. 17.            window.start();  
  7. 18.        }  
  8. 19.  问题:    但是,start()方法里面是有个while(true)循环。下面的代码怎么会执行到呢??        ??
  9. 20.        //产生1个快速窗口  
  10. 21.        ServiceWindow expressWindow =  new ServiceWindow();  
  11. 22.        expressWindow.setType(CustomerType.EXPRESS);  
  12. 23.        expressWindow.start();  
  13. 24.         
  14. 25.        //产生1个VIP窗口      
  15. 26.        ServiceWindow vipWindow =  new ServiceWindow();  
  16. 27.        vipWindow.setType(CustomerType.VIP);  
  17. 28.        vipWindow.start();        


  18. public void start(){  
  19. 28.        Executors.newSingleThreadExecutor().execute(  
  20. 29.                new Runnable(){  
  21. 30.                    public void run(){  
  22. 31.                        //下面这种写法的运行效率低,最好是把while放在case下面  
  23. 32.                        while(true){  
  24. 33.                            switch(type){  
  25. 34.                                case COMMON:  
  26. 35.                                    commonService();  
  27. 36.                                    break;  
  28. 37.                                case EXPRESS:  
  29. 38.                                    expressService();  
  30. 39.                                    break;  
  31. 40.                                case VIP:  
  32. 41.                                    vipService();  
  33. 42.                                    break;  
  34. 43.                            }  
  35. 44.                        }  
  36. 45.                    }  
  37. 46.                }  
  38. 47.        );  
  39. 48.    }  
复制代码

start()方法里面是有个while(true)循环。下面的代码怎么会执行到呢??        ??

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1 赞一个!学习态度值得赞扬!

查看全部评分

9 个回复

倒序浏览
start开启了一个线程,在线程中用while没有问题的吧,和主线程并行执行的,为什么会不运行下面的呢
回复 使用道具 举报
月光海 发表于 2014-5-21 19:19
start开启了一个线程,在线程中用while没有问题的吧,和主线程并行执行的,为什么会不运行下面的呢 ...

vipWindow.start()是开启线程吗,感觉不像 VipWindow有不是Thread的子类。  
回复 使用道具 举报
break............
回复 使用道具 举报

break,只是跳出switch语句,while还是会循环的。
回复 使用道具 举报
谭荣强 发表于 2014-5-21 19:43
vipWindow.start()是开启线程吗,感觉不像 VipWindow有不是Thread的子类。

请看清楚,start里面是有创建单个线程池然后运行里面的命令
  1. execute(Runnable command)在未来某个时间执行给定的命令。该命令可能在新的线程、已入池的线程或者正调用的线程中执行,这由 Executor 实现决定。

  2. 参数:
  3. command - 可运行的任务
复制代码

这样就开启了一个线程,然后主线程也在运行,主线程就将start方法执行完就继续执行下面的代码,刚刚启动的线程也在运行,我说的够清楚吗?
回复 使用道具 举报
月光海 发表于 2014-5-21 20:22
请看清楚,start里面是有创建单个线程池然后运行里面的命令

这样就开启了一个线程,然后主线程也在运行 ...

为什么下面的这段代码输出全是1

  1. import java.util.concurrent.Executors;
  2. public class Demo {
  3.         public static void main(String[] args) {                 
  4.                        
  5.                 new test1().show1();
  6.                 new test2().show2();
  7.                 new test3().show3();         
  8.        
  9.         }       
  10. }
  11. class test1{         
  12.         public  void show1(){
  13.                 while (true) {
  14.                         Executors.newSingleThreadExecutor().execute(new Runnable() {
  15.                                 public void run() {
  16.                                         System.out.println("1");
  17.                                 }
  18.                         });
  19.                 }         
  20.         }
  21. }
  22. class test2{
  23.         public  void show2(){
  24.                 while (true) {
  25.                         Executors.newSingleThreadExecutor().execute(new Runnable() {
  26.                                 public void run() {
  27.                                         System.out.println("2");
  28.                                 }
  29.                         });
  30.                 }         
  31.         }
  32. }
  33. class test3{
  34.         public  void show3(){
  35.                 while (true) {
  36.                         Executors.newSingleThreadExecutor().execute(new Runnable() {
  37.                                 public void run() {
  38.                                         System.out.println("3");
  39.                                 }
  40.                         });
  41.                 }         
  42.         }
  43. }
  44.        
复制代码
回复 使用道具 举报
谭荣强 发表于 2014-5-21 20:55
为什么下面的这段代码输出全是1

你看清楚好吗?你写的while(true)是在线程池的外围啊,张老师写的是在线程里面,会一样吗?看看我按张老师的写法改的你的代码,再看不懂我就没话说了
  1. package demo2;

  2. import java.util.concurrent.Executors;
  3. public class Demo {
  4.         public static void main(String[] args) {                 
  5.                         
  6.                 new test1().show1();
  7.                 new test2().show2();
  8.                 new test3().show3();         
  9.         
  10.         }        
  11. }
  12. class test1{         
  13.         public  void show1(){
  14.               
  15.                         Executors.newSingleThreadExecutor().execute(new Runnable() {
  16.                                 public void run() {
  17.                                           while (true) {
  18.                                         System.out.println("1");
  19.                                           }  
  20.                                 }
  21.                         });
  22.                      
  23.         }
  24. }
  25. class test2{
  26.         public  void show2(){
  27.                
  28.                         Executors.newSingleThreadExecutor().execute(new Runnable() {
  29.                                 public void run() {
  30.                                          while (true) {
  31.                                         System.out.println("2");
  32.                                           }
  33.                                 }
  34.                         });
  35.                      
  36.         }
  37. }
  38. class test3{
  39.         public  void show3(){
  40.               
  41.                         Executors.newSingleThreadExecutor().execute(new Runnable() {
  42.                                 public void run() {
  43.                                           while (true) {
  44.                                         System.out.println("3");
  45.                                            }
  46.                                 }
  47.                         });
  48.                      
  49.         }
  50. }
  51.         
复制代码

评分

参与人数 1技术分 +2 收起 理由
李小然 + 2 回答的很棒!下次注意下语气哦~.

查看全部评分

回复 使用道具 举报
学习了.
回复 使用道具 举报
月光海 发表于 2014-5-21 21:09
你看清楚好吗?你写的while(true)是在线程池的外围啊,张老师写的是在线程里面,会一样吗?看看我按张老 ...

明白多谢多谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马