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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© java小兵 中级黑马   /  2014-12-9 11:04  /  1304 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 java小兵 于 2015-1-26 21:35 编辑
  1. class Ticket implements Runnable
  2. {
  3.         private int tick = 100;

  4.         Object o = new Object();

  5.         public void run()
  6.         {
  7.                 while(true)
  8.                 {
  9.                         synchronized(o)
  10.                         {
  11.                                 if (tick>0)
  12.                                 {
  13.                                         try
  14.                                         {
  15.                                                 Thread.sleep(10);
  16.                                         }
  17.                                         catch (Exception e)
  18.                                         {
  19.                                                 
  20.                                         }
  21.                                         System.out.println(Thread.currentThread().getName()+"....sale"+tick--);
  22.                                 }
  23.                         }
  24.                         
  25.                 }
  26.         }
  27. }
  28. class  TicketDemo
  29. {
  30.         public static void main(String[] args)
  31.         {
  32.                 Ticket t = new Ticket();

  33.                 Thread t1 = new Thread(t);
  34.                 Thread t2 = new Thread(t);
  35.                 Thread t3 = new Thread(t);
  36.                 Thread t4 = new Thread(t);
  37.                 t1.start();
  38.                 t2.start();
  39.                 t3.start();
  40.                 t4.start();

  41.                 //System.out.println("Hello World!");
  42.         }
  43. }
复制代码


运行完只有0线程。
H:\>java TicketDemo
Thread-0....sale100
Thread-0....sale99
Thread-0....sale98
Thread-0....sale97
Thread-0....sale96
Thread-0....sale95
Thread-0....sale94
Thread-0....sale93
Thread-0....sale92
Thread-0....sale91
Thread-0....sale90
Thread-0....sale89
Thread-0....sale88
Thread-0....sale87
Thread-0....sale86
Thread-0....sale85
Thread-0....sale84
Thread-0....sale83
Thread-0....sale82
Thread-0....sale81
Thread-0....sale80
Thread-0....sale79
Thread-0....sale78
Thread-0....sale77
Thread-0....sale76
Thread-0....sale75
Thread-0....sale74
Thread-0....sale73
Thread-0....sale72
Thread-0....sale71
Thread-0....sale70
Thread-0....sale69
Thread-0....sale68
Thread-0....sale67
Thread-0....sale66
Thread-0....sale65
Thread-0....sale64
Thread-0....sale63
Thread-0....sale62
Thread-0....sale61
Thread-0....sale60
Thread-0....sale59
Thread-0....sale58
Thread-0....sale57
Thread-0....sale56
Thread-0....sale55
Thread-0....sale54
Thread-0....sale53
Thread-0....sale52
Thread-0....sale51
Thread-0....sale50
Thread-0....sale49
Thread-0....sale48
Thread-0....sale47
Thread-0....sale46
Thread-0....sale45
Thread-0....sale44
Thread-0....sale43
Thread-0....sale42
Thread-0....sale41
Thread-0....sale40
Thread-0....sale39
Thread-0....sale38
Thread-0....sale37
Thread-0....sale36
Thread-0....sale35
Thread-0....sale34
Thread-0....sale33
Thread-0....sale32
Thread-0....sale31
Thread-0....sale30
Thread-0....sale29
Thread-0....sale28
Thread-0....sale27
Thread-0....sale26
Thread-0....sale25
Thread-0....sale24
Thread-0....sale23
Thread-0....sale22
Thread-0....sale21
Thread-0....sale20
Thread-0....sale19
Thread-0....sale18
Thread-0....sale17
Thread-0....sale16
Thread-0....sale15
Thread-0....sale14
Thread-0....sale13
Thread-0....sale12
Thread-0....sale11
Thread-0....sale10
Thread-0....sale9
Thread-0....sale8
Thread-0....sale7
Thread-0....sale6
Thread-0....sale5
Thread-0....sale4
Thread-0....sale3
Thread-0....sale2
Thread-0....sale1

这是怎么回事呢?求大家帮忙啊!

11 个回复

倒序浏览
在我电脑上运行的时候,四个线程都运行了,你多测试几次看看

回复 使用道具 举报
月老~牵红线 发表于 2014-12-9 11:58
在我电脑上运行的时候,四个线程都运行了,你多测试几次看看

试了好多次了。都一样啊、难道是电脑问题?
回复 使用道具 举报
又出问题了
  1. class Ticket implements Runnable
  2. {
  3.         private int tick = 100;
  4.         boolean flag = true;
  5.         public void run()
  6.         {
  7.                 if (flag)
  8.                 {
  9.                         while (true)
  10.                         {
  11.                                 synchronized(this)
  12.                                 {
  13.                                         if (tick>0)
  14.                                         {
  15.                                                 try
  16.                                                 {
  17.                                                         Thread.sleep(10);
  18.                                                 }
  19.                                                 catch (Exception e)
  20.                                                 {
  21.                                                 }
  22.                                                 System.out.println(Thread.currentThread().getName()+"--代码块执行--"+tick--);
  23.                                         }
  24.                                         else
  25.                                                 while (true)
  26.                                                 {
  27.                                                         show();
  28.                                                 }
  29.                                 }
  30.                         }
  31.                 }
  32.         }
  33.         public synchronized void show()
  34.         {
  35.                 if (tick>0)
  36.                 {
  37.                         try
  38.                         {
  39.                                 Thread.sleep(10);
  40.                         }
  41.                         catch (Exception e)
  42.                         {
  43.                         }
  44.                         System.out.println(Thread.currentThread().getName()+"--方法执行--"+tick--);
  45.                 }
  46.         }
  47. }
  48. class  Demo2_2
  49. {
  50.         public static void main(String[] args)
  51.         {
  52.                 Ticket t = new Ticket();

  53.                 Thread t1 = new  Thread(t);
  54.                
  55.                 Thread t2 = new  Thread(t);
  56.                 t1.start();
  57.                 try{Thread.sleep(10);}catch(Exception e){}
  58.                 t.flag = false;
  59.                 t2.start();
  60.                 System.out.println("Hello World!");
  61.         }
  62. }
复制代码


代码是按毕老师的照抄来的。但结果错了。

昨天发了一个跟这个问题一样。结果也一样。问题不知道出在哪里。是不是我电脑的 问题呢?请兄弟们给看看。。。
回复 使用道具 举报
接楼上啊!

QQ图片20141210122631.jpg (114.61 KB, 下载次数: 4)

QQ图片20141210122631.jpg

QQ图片20141210122704.jpg (111.49 KB, 下载次数: 7)

QQ图片20141210122704.jpg
回复 使用道具 举报

24行else块的内容放错了,放在32行的位置
回复 使用道具 举报
Lscreat 中级黑马 2014-12-10 15:23:01
7#
不行你就吧ticket的数量弄大一点嘛。
回复 使用道具 举报
多试几次就有了  或者ticket搞大点
回复 使用道具 举报
hengxing0079 发表于 2014-12-10 13:16
24行else块的内容放错了,放在32行的位置

谢谢。。改过来了。还是一样。100换成1000才出现两个线程。这是怎么回事呢?
回复 使用道具 举报
Lscreat 发表于 2014-12-10 15:23
不行你就吧ticket的数量弄大一点嘛。

搞大的确出来了。但是为什么呢?
回复 使用道具 举报
毕老师视频里有说的,数字太小可能导致有的线程没有获得执行权就执行完了所以看上去就只有一个线程在运行。没理解到回去看看视频吧。
回复 使用道具 举报
帅,很好!

评分

参与人数 1黑马币 +6 收起 理由
java小兵 + 6

查看全部评分

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