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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 邓利军 于 2012-9-25 20:50 编辑

今天默写售票程序,不加锁时多条线程都执行了,
但当我加上锁后,每次执行都只是其中一条执行,部分运行结果如下 :
Thread-0......ticket.....420
Thread-0......ticket.....419
Thread-0......ticket.....418
Thread-0......ticket.....417
Thread-0......ticket.....416
Thread-0......ticket.....415
Thread-0......ticket.....414
Thread-0......ticket.....413
Thread-0......ticket.....412
Thread-0......ticket.....411
Thread-0......ticket.....410
Thread-0......ticket.....409
Thread-0......ticket.....408
Thread-0......ticket.....407
Thread-0......ticket.....406

我的代码如下,为什么只有一条线程执行?脑子进死胡同了,百思不得其解,求解答............
  1. class Ticket implements Runnable
  2. {
  3.         private int num=500;
  4.         private Object obj=new Object();
  5.         public  void run()
  6.         {
  7.                
  8.                 while(true)
  9.                 {
  10.                         synchronized(obj)
  11.                         {
  12.                                 show();
  13.                         }
  14.                 }

  15.         }               
  16.         public void show()
  17.                 {
  18.                                 if(num>0)
  19.                                         {
  20.                                                 try{Thread.sleep(100);}catch(InterruptedException e){}
  21.                                                 System.out.println(Thread.currentThread().getName()+"......ticket....."+num--);
  22.                                         }
  23.                 }
  24. }

  25. class TicketDemo5
  26. {
  27.         public static void main(String[] args)
  28.         {
  29.                 Ticket t=new Ticket();
  30.                 Thread t1=new Thread(t);
  31.                 Thread t2=new Thread(t);
  32.                 Thread t3=new Thread(t);
  33.                 Thread t4=new Thread(t);
  34.                 t1.start();
  35.                 t2.start();
  36.                 t3.start();
  37.                 t4.start();
  38.         }
  39. }
复制代码

评分

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

查看全部评分

5 个回复

倒序浏览

回帖奖励 +2

运行没问题啊
Thread-0......ticket.....500
Thread-0......ticket.....499
Thread-3......ticket.....498
Thread-2......ticket.....497
Thread-2......ticket.....496
Thread-2......ticket.....495
Thread-2......ticket.....494
Thread-2......ticket.....493
Thread-2......ticket.....492
Thread-2......ticket.....491
Thread-1......ticket.....490
Thread-2......ticket.....489
Thread-2......ticket.....488
Thread-2......ticket.....487
Thread-2......ticket.....486
Thread-2......ticket.....485
Thread-3......ticket.....484
Thread-0......ticket.....483
Thread-0......ticket.....482
Thread-3......ticket.....481
Thread-3......ticket.....480
Thread-2......ticket.....479

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1 参与有分

查看全部评分

回复 使用道具 举报
我运行了一下你的代码!这是我的运行结果,请查收

Thread-2......ticket.....100
Thread-3......ticket.....99
Thread-0......ticket.....98
Thread-0......ticket.....97
Thread-3......ticket.....96
Thread-3......ticket.....95
Thread-3......ticket.....94
Thread-2......ticket.....93
Thread-2......ticket.....92
Thread-1......ticket.....91
Thread-1......ticket.....90
Thread-2......ticket.....89
Thread-2......ticket.....88
Thread-2......ticket.....87
Thread-2......ticket.....86
Thread-2......ticket.....85
Thread-2......ticket.....84
Thread-3......ticket.....83
Thread-3......ticket.....82
Thread-0......ticket.....81
Thread-0......ticket.....80
Thread-0......ticket.....79
Thread-0......ticket.....78
Thread-3......ticket.....77
Thread-3......ticket.....76
Thread-3......ticket.....75
Thread-3......ticket.....74
Thread-2......ticket.....73
Thread-1......ticket.....72
Thread-1......ticket.....71
Thread-1......ticket.....70
Thread-1......ticket.....69
Thread-2......ticket.....68
Thread-3......ticket.....67
Thread-0......ticket.....66
Thread-0......ticket.....65
Thread-3......ticket.....64
Thread-2......ticket.....63
Thread-2......ticket.....62
Thread-2......ticket.....61
Thread-2......ticket.....60
Thread-2......ticket.....59
Thread-1......ticket.....58
Thread-2......ticket.....57
Thread-2......ticket.....56
Thread-2......ticket.....55
Thread-2......ticket.....54
Thread-2......ticket.....53
Thread-2......ticket.....52
Thread-2......ticket.....51
Thread-3......ticket.....50
Thread-3......ticket.....49
Thread-0......ticket.....48
Thread-0......ticket.....47
Thread-0......ticket.....46
Thread-3......ticket.....45
Thread-3......ticket.....44
Thread-2......ticket.....43
Thread-2......ticket.....42
Thread-1......ticket.....41
Thread-2......ticket.....40
Thread-2......ticket.....39
Thread-2......ticket.....38
Thread-2......ticket.....37
Thread-2......ticket.....36
Thread-2......ticket.....35
Thread-3......ticket.....34
Thread-3......ticket.....33
Thread-3......ticket.....32
Thread-3......ticket.....31
Thread-3......ticket.....30
Thread-0......ticket.....29
Thread-3......ticket.....28
Thread-2......ticket.....27
Thread-2......ticket.....26
Thread-2......ticket.....25
Thread-2......ticket.....24
Thread-2......ticket.....23
Thread-2......ticket.....22
Thread-2......ticket.....21
Thread-1......ticket.....20
Thread-1......ticket.....19
Thread-2......ticket.....18
Thread-2......ticket.....17
Thread-3......ticket.....16
Thread-3......ticket.....15
Thread-0......ticket.....14
Thread-0......ticket.....13
Thread-0......ticket.....12
Thread-0......ticket.....11
Thread-3......ticket.....10
Thread-3......ticket.....9
Thread-3......ticket.....8
Thread-3......ticket.....7
Thread-2......ticket.....6
Thread-2......ticket.....5
Thread-2......ticket.....4
Thread-2......ticket.....3
Thread-2......ticket.....2
Thread-2......ticket.....1

点评

我重复了很我多次,还是只有一条线程执行.  发表于 2012-9-25 19:36

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1 参与有分

查看全部评分

回复 使用道具 举报
本帖最后由 梁志冰 于 2012-9-25 19:38 编辑

没有任何的问题,估计是你的电脑CPU的问题,你多运行几次应该就可以看到其他线程了

点评

卖1000张票,还是一个窗口卖, 多试几次也是一样.cpu是双核的.  发表于 2012-9-25 19:40

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1 参与有分

查看全部评分

回复 使用道具 举报
梁志冰 发表于 2012-9-25 19:33
没有任何的问题,估计是你的电脑CPU的问题,你多运行几次应该就可以看到其他线程了 ...

class Ticket implements Runnable
{
private int num = 500;
private Object obj = new Object();
public void run()
{
  while(true)
  {
   synchronized(obj
   {
    if(num>0)
    {
     try{Thread.sleep(10);}catch(InterruptedException e){}
     System.out.println(Thread.currentThread().getName()+"......ticket....."+num--);
    }
   }
  }
}
}
class TicketDemo5
{
public static void main(String[] args)
{
  Ticket t = new Ticket();
  Thread t1 = new Thread(t);
  Thread t2 = new Thread(t);
  Thread t3 = new Thread(t);
  Thread t4 = new Thread(t);
  t1.start();
  t2.start();
  t3.start();
  t4.start();
}
}

那你把show()方法的代码直接放到同步中试试

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1 参与有奖

查看全部评分

回复 使用道具 举报
sleep(100),卖10万张火车票.第800多张票看到其它线程执行.
Thread-0......ticket.....99249
Thread-0......ticket.....99248
Thread-0......ticket.....99247
Thread-0......ticket.....99246
Thread-0......ticket.....99245
Thread-0......ticket.....99244
Thread-0......ticket.....99243
Thread-0......ticket.....99242
Thread-0......ticket.....99241
Thread-0......ticket.....99240
Thread-0......ticket.....99239
Thread-0......ticket.....99238
Thread-0......ticket.....99237
Thread-0......ticket.....99236
Thread-0......ticket.....99235
Thread-0......ticket.....99234
Thread-0......ticket.....99233
Thread-0......ticket.....99232
Thread-0......ticket.....99231
Thread-0......ticket.....99230
Thread-0......ticket.....99229
Thread-0......ticket.....99228
Thread-0......ticket.....99227
Thread-0......ticket.....99226
Thread-0......ticket.....99225
Thread-0......ticket.....99224
Thread-0......ticket.....99223
Thread-0......ticket.....99222
Thread-0......ticket.....99221
Thread-0......ticket.....99220
Thread-0......ticket.....99219
Thread-0......ticket.....99218
Thread-0......ticket.....99217
Thread-0......ticket.....99216
Thread-0......ticket.....99215
Thread-0......ticket.....99214
Thread-0......ticket.....99213
Thread-0......ticket.....99212
Thread-0......ticket.....99211
Thread-3......ticket.....99210
Thread-3......ticket.....99209
Thread-3......ticket.....99208
Thread-3......ticket.....99207
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马