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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 吴亨 黑马帝   /  2011-12-13 11:58  /  3669 人查看  /  16 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 stephen8341 于 2011-12-13 13:32 编辑

class TestThread
{
        public static void main(String[]args)
        {
          Runnable tt = new Thread1();
          new Thread(tt).start();
          new Thread(tt).start();
          new Thread(tt).start();
          new Thread(tt).start();
        }
       
}


class Thread1 implements Runnable
{
            int tickets = 100;
            String str = "";
                public  void run()
                {
                               
                        while(true)
                       {
                               sale();
                       }
                }
                       
public synchronized void sale()
  {
                               
         if(tickets>0)
         {
                try{Thread.sleep(10);} catch(Exception ex){}
                System.out.println(Thread.currentThread().getName()+" ticket"+tickets--);
         }
  }
                       
}

为何只有一个线程在运行?

评分

参与人数 1技术分 +1 收起 理由
王德云 + 1

查看全部评分

16 个回复

倒序浏览
周胜 黑马帝 2011-12-13 12:23:03
沙发
本帖最后由 周胜 于 2011-12-13 13:38 编辑

Synchronized是在执行这个方法的过程中当前对象被锁定!!~//锁定机制,锁定当前方法,。
刚没理解你的意思。。。你调成Thread.sleep(500)就能看出来多线程了。。。

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
并不是lz所说的那样,在run方法中
public  void run()
                {
                                
                        while(true)
                       {
                               sale(); //同步的只是sale方法,当每次的sale方法调用运行结束,返回后,在这边4个线程会抢夺CPU执行权,具体给哪个线程我们说了不算
                       }
                }


运行结果如下:
  1. D:\>java TestThread
  2. Thread-0 ticket100
  3. Thread-0 ticket99
  4. Thread-0 ticket98
  5. Thread-0 ticket97
  6. Thread-0 ticket96
  7. Thread-0 ticket95
  8. Thread-0 ticket94
  9. Thread-3 ticket93
  10. Thread-3 ticket92
  11. Thread-3 ticket91
  12. Thread-3 ticket90
  13. Thread-3 ticket89
  14. Thread-3 ticket88
  15. Thread-3 ticket87
  16. Thread-3 ticket86
  17. Thread-3 ticket85
  18. Thread-3 ticket84
  19. Thread-3 ticket83
  20. Thread-3 ticket82
  21. Thread-3 ticket81
  22. Thread-3 ticket80
  23. Thread-3 ticket79
  24. Thread-3 ticket78
  25. Thread-3 ticket77
  26. Thread-3 ticket76
  27. Thread-3 ticket75
  28. Thread-3 ticket74
  29. Thread-3 ticket73
  30. Thread-3 ticket72
  31. Thread-3 ticket71
  32. Thread-3 ticket70
  33. Thread-3 ticket69
  34. Thread-3 ticket68
  35. Thread-3 ticket67
  36. Thread-3 ticket66
  37. Thread-3 ticket65
  38. Thread-3 ticket64
  39. Thread-3 ticket63
  40. Thread-3 ticket62
  41. Thread-3 ticket61
  42. Thread-3 ticket60
  43. Thread-3 ticket59
  44. Thread-3 ticket58
  45. Thread-3 ticket57
  46. Thread-3 ticket56
  47. Thread-3 ticket55
  48. Thread-3 ticket54
  49. Thread-3 ticket53
  50. Thread-3 ticket52
  51. Thread-3 ticket51
  52. Thread-3 ticket50
  53. Thread-3 ticket49
  54. Thread-3 ticket48
  55. Thread-3 ticket47
  56. Thread-3 ticket46
  57. Thread-3 ticket45
  58. Thread-3 ticket44
  59. Thread-3 ticket43
  60. Thread-3 ticket42
  61. Thread-3 ticket41
  62. Thread-3 ticket40
  63. Thread-3 ticket39
  64. Thread-3 ticket38
  65. Thread-3 ticket37
  66. Thread-3 ticket36
  67. Thread-3 ticket35
  68. Thread-3 ticket34
  69. Thread-3 ticket33
  70. Thread-3 ticket32
  71. Thread-3 ticket31
  72. Thread-3 ticket30
  73. Thread-3 ticket29
  74. Thread-3 ticket28
  75. Thread-3 ticket27
  76. Thread-3 ticket26
  77. Thread-3 ticket25
  78. Thread-3 ticket24
  79. Thread-3 ticket23
  80. Thread-3 ticket22
  81. Thread-3 ticket21
  82. Thread-3 ticket20
  83. Thread-3 ticket19
  84. Thread-3 ticket18
  85. Thread-3 ticket17
  86. Thread-3 ticket16
  87. Thread-3 ticket15
  88. Thread-3 ticket14
  89. Thread-3 ticket13
  90. Thread-3 ticket12
  91. Thread-3 ticket11
  92. Thread-3 ticket10
  93. Thread-3 ticket9
  94. Thread-3 ticket8
  95. Thread-3 ticket7
  96. Thread-3 ticket6
  97. Thread-3 ticket5
  98. Thread-3 ticket4
  99. Thread-3 ticket3
  100. Thread-3 ticket2
  101. Thread-3 ticket1
复制代码
回复 使用道具 举报
是不是只显示一个线程,不显示其他的,
这也是有可能的,线程的随机性,
即使有执行权,但被同步以后,只要上一个线程没结束,也不执行;
你可以多运行几次试试;
snchronized 方法被同步

对象如同锁,持有锁的线程可以在同步中执行
没有持有锁的线程即使获得cpu的执行权,也进不去,因为没有获得锁

对了,你的结果给下;

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
怎么回复要审核》》》
回复 使用道具 举报
lz你说的不正确,
public  void run()
                {
                                
                        while(true)
                       {
                               sale(); //同步的只是sale方法,当某个线程第一次拿到了锁,调用了sale(),结束返回后,在这边,4个线程会抢夺cpu的执行权(每次sale()方法的调用前都会这么做),至于执行权给谁,我们决定不了,
                       }
                }


运行结果:
  1. D:\>java TestThread
  2. Thread-0 ticket100
  3. Thread-0 ticket99
  4. Thread-0 ticket98
  5. Thread-0 ticket97
  6. Thread-0 ticket96
  7. Thread-0 ticket95
  8. Thread-0 ticket94
  9. Thread-3 ticket93
  10. Thread-3 ticket92
  11. Thread-3 ticket91
  12. Thread-3 ticket90
  13. Thread-3 ticket89
  14. Thread-3 ticket88
  15. Thread-3 ticket87
  16. Thread-3 ticket86
  17. Thread-3 ticket85
  18. Thread-3 ticket84
  19. Thread-3 ticket83
  20. Thread-3 ticket82
  21. Thread-3 ticket81
  22. Thread-3 ticket80
  23. Thread-3 ticket79
  24. Thread-3 ticket78
  25. Thread-3 ticket77
  26. Thread-3 ticket76
  27. Thread-3 ticket75
  28. Thread-3 ticket74
  29. Thread-3 ticket73
  30. Thread-3 ticket72
  31. Thread-3 ticket71
  32. Thread-3 ticket70
  33. Thread-3 ticket69
  34. Thread-3 ticket68
  35. Thread-3 ticket67
  36. Thread-3 ticket66
  37. Thread-3 ticket65
  38. Thread-3 ticket64
  39. Thread-3 ticket63
  40. Thread-3 ticket62
  41. Thread-3 ticket61
  42. Thread-3 ticket60
  43. Thread-3 ticket59
  44. Thread-3 ticket58
  45. Thread-3 ticket57
  46. Thread-3 ticket56
  47. Thread-3 ticket55
  48. Thread-3 ticket54
  49. Thread-3 ticket53
  50. Thread-3 ticket52
  51. Thread-3 ticket51
  52. Thread-3 ticket50
  53. Thread-3 ticket49
  54. Thread-3 ticket48
  55. Thread-3 ticket47
  56. Thread-3 ticket46
  57. Thread-3 ticket45
  58. Thread-3 ticket44
  59. Thread-3 ticket43
  60. Thread-3 ticket42
  61. Thread-3 ticket41
  62. Thread-3 ticket40
  63. Thread-3 ticket39
  64. Thread-3 ticket38
  65. Thread-3 ticket37
  66. Thread-3 ticket36
  67. Thread-3 ticket35
  68. Thread-3 ticket34
  69. Thread-3 ticket33
  70. Thread-3 ticket32
  71. Thread-3 ticket31
  72. Thread-3 ticket30
  73. Thread-3 ticket29
  74. Thread-3 ticket28
  75. Thread-3 ticket27
  76. Thread-3 ticket26
  77. Thread-3 ticket25
  78. Thread-3 ticket24
  79. Thread-3 ticket23
  80. Thread-3 ticket22
  81. Thread-3 ticket21
  82. Thread-3 ticket20
  83. Thread-3 ticket19
  84. Thread-3 ticket18
  85. Thread-3 ticket17
  86. Thread-3 ticket16
  87. Thread-3 ticket15
  88. Thread-3 ticket14
  89. Thread-3 ticket13
  90. Thread-3 ticket12
  91. Thread-3 ticket11
  92. Thread-3 ticket10
  93. Thread-3 ticket9
  94. Thread-3 ticket8
  95. Thread-3 ticket7
  96. Thread-3 ticket6
  97. Thread-3 ticket5
  98. Thread-3 ticket4
  99. Thread-3 ticket3
  100. Thread-3 ticket2
  101. Thread-3 ticket1
复制代码
回复 使用道具 举报
吴亨 黑马帝 2011-12-13 12:52:12
7#
这是截图

QQ截图20111123125111.png (12.7 KB, 下载次数: 225)

QQ截图20111123125111.png
回复 使用道具 举报
吴亨 黑马帝 2011-12-13 12:56:11
8#
胡遇潮 发表于 2011-12-13 12:36
是不是只显示一个线程,不显示其他的,
这也是有可能的,线程的随机性,
即使有执行权,但被同步以后,只要 ...

  我又试了好几次,偶尔会出现其它线程,但没有出现过线程交互出现的情况,请问怎么解决啊?必须用wait,notify吗

点评

把synchronized去掉试试  发表于 2011-12-13 13:11
回复 使用道具 举报
zoufan7410 黑马帝 2011-12-13 13:00:06
9#
为什么我用eclipse 运行时,是多线程啊,和你不一样啊,!~,
回复 使用道具 举报
李建锋 黑马帝 2011-12-13 13:02:13
10#
public static void main(String[]args)
    {
      Runnable tt = new Thread1();
      new Thread(tt).start();
      new Thread(tt).start();
      new Thread(tt).start();
      new Thread(tt).start();
    }
   
}


class Thread1 implements Runnable
{
        int tickets = 100;
        String str = "";
            public  void run()
            {
                           
                   while(true)
                   {
                           try {
                            sale();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                   }
            }
                    
            public synchronized void sale() throws IOException
{
                           
    if(tickets>0)
     {
            try{Thread.sleep(60);}
            
            
            
            catch(Exception ex){}
                OutputStream os=new FileOutputStream("c:\\info.txt",true);
                OutputStreamWriter osw=new OutputStreamWriter(os);
                BufferedWriter bufw=new BufferedWriter(osw);
                osw.write((Thread.currentThread().getName()+" ticket"+tickets--).toCharArray()
                        );
                        bufw.newLine();
                        bufw.flush();
                        bufw.close();
     }
}




Thread-0 ticket100
Thread-0 ticket99
Thread-0 ticket98
Thread-0 ticket97
Thread-0 ticket96
Thread-0 ticket95
Thread-3 ticket94
Thread-1 ticket93
Thread-1 ticket92
Thread-1 ticket91
Thread-1 ticket90
Thread-2 ticket89
Thread-1 ticket88
Thread-3 ticket87
Thread-0 ticket86
Thread-3 ticket85
Thread-1 ticket84
Thread-1 ticket83
Thread-1 ticket82
Thread-1 ticket81
Thread-1 ticket80
Thread-1 ticket79
Thread-1 ticket78
Thread-1 ticket77
Thread-1 ticket76
Thread-1 ticket75
Thread-1 ticket74
Thread-1 ticket73
Thread-1 ticket72
Thread-2 ticket71
Thread-2 ticket70
Thread-2 ticket69
Thread-2 ticket68
Thread-2 ticket67
Thread-2 ticket66
Thread-2 ticket65
Thread-2 ticket64
Thread-2 ticket63
Thread-2 ticket62
Thread-2 ticket61
Thread-2 ticket60
Thread-2 ticket59
Thread-1 ticket58
Thread-3 ticket57
Thread-3 ticket56
Thread-3 ticket55
Thread-3 ticket54
Thread-3 ticket53
Thread-3 ticket52
Thread-0 ticket51
Thread-0 ticket50
Thread-0 ticket49
Thread-0 ticket48
Thread-0 ticket47
Thread-0 ticket46
Thread-3 ticket45
Thread-1 ticket44
Thread-1 ticket43
Thread-1 ticket42
Thread-1 ticket41
Thread-1 ticket40
Thread-1 ticket39
Thread-1 ticket38
Thread-1 ticket37
Thread-1 ticket36
Thread-1 ticket35
Thread-1 ticket34
Thread-1 ticket33
Thread-1 ticket32
Thread-1 ticket31
Thread-1 ticket30
Thread-1 ticket29
Thread-1 ticket28
Thread-1 ticket27
Thread-1 ticket26
Thread-1 ticket25
Thread-1 ticket24
Thread-1 ticket23
Thread-1 ticket22
Thread-1 ticket21
Thread-1 ticket20
Thread-1 ticket19
Thread-1 ticket18
Thread-1 ticket17
Thread-1 ticket16
Thread-1 ticket15
Thread-1 ticket14
Thread-1 ticket13
Thread-1 ticket12
Thread-1 ticket11
Thread-1 ticket10
Thread-1 ticket9
Thread-2 ticket8
Thread-2 ticket7
Thread-2 ticket6
Thread-2 ticket5
Thread-2 ticket4
Thread-2 ticket3
Thread-2 ticket2
Thread-2 ticket1
本人完美运行!
回复 使用道具 举报
吴亨 黑马帝 2011-12-13 13:03:58
11#
zoufan7410 发表于 2011-12-13 13:00
为什么我用eclipse 运行时,是多线程啊,和你不一样啊,!~,

我还在用cmd,是这个原因吗
回复 使用道具 举报
杨旭 黑马帝 2011-12-13 13:04:19
12#
把 int tickets = 100  的100  改大一点  就出来效果了
回复 使用道具 举报
刘基军 黑马帝 2011-12-13 13:09:52
13#
stephen8341 发表于 2011-12-13 12:56
我又试了好几次,偶尔会出现其它线程,但没有出现过线程交互出现的情况,请问怎么解决啊?必须用wait,n ...

lz你同步的只是sale()方法,而在run()方法中的while循环里: 在每次调用sale()方法前,4个线程都会进行cpu执行权的抢夺,至于最后是给了哪个线程,是由操作系统或cpu决定的,你决定不了。除非你改动代码,人为的加上条件进行控制。
回复 使用道具 举报
吴亨 黑马帝 2011-12-13 13:15:43
14#
monghuan 发表于 2011-12-13 13:09
lz你同步的只是sale()方法,而在run()方法中的while循环里: 在每次调用sale()方法前,4个线程都会进行cpu ...

对啊,所以四个线程应该都有可能出现吧。为什么只有一个线程?
回复 使用道具 举报
刘基军 黑马帝 2011-12-13 13:19:02
15#
stephen8341 发表于 2011-12-13 13:15
对啊,所以四个线程应该都有可能出现吧。为什么只有一个线程?

对呀,所以运行结果应该是随机的,多运行几次,就不一样了
回复 使用道具 举报
刘基军 黑马帝 2011-12-13 13:21:53
16#
恩,我刚才又试了十来次,几乎大多和lz的一样,多试就会不一样了
回复 使用道具 举报
吴亨 黑马帝 2011-12-13 13:31:24
17#
谢谢大家了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马