黑马程序员技术交流社区

标题: 关于线程的小程序 [打印本页]

作者: 周金帅    时间: 2012-5-31 17:27
标题: 关于线程的小程序
  1. class Tickit implements Runnable
  2. {
  3.         private int tickit=100;
  4.         Object o=new Object();
  5.         public void run()
  6.         {   
  7.                 synchronized(o)
  8.                 {
  9.                         while(tickit>0)
  10.                         {
  11.                         try{Thread.sleep(1);}catch(Exception e){}
  12.                         System.out.println(Thread.currentThread().getName()+"卖票"+tickit--);
  13.                         }
  14.                 }
  15.         }
  16. }

  17. class ThreadDemo1
  18. {
  19.         public static void main(String[] args)
  20.         {
  21.                 Tickit t=new Tickit();
  22.                 Thread t1=new Thread(t);
  23.                 Thread t2=new Thread(t);
  24.                 Thread t3=new Thread(t);
  25.                 Thread t4=new Thread(t);
  26.                
  27.                 t1.start();
  28.                 t2.start();
  29.                 t3.start();
  30.                 t4.start();
  31.         }
  32. }
复制代码
运行结果,只有0线程运行,哪里出错了啊
作者: 吴小铁你好    时间: 2012-5-31 17:35
呵呵,你把while的位置放错了。应该放在synchronized(o)
外面。

像你那样t1进入后循环玩才能出来啊,所以就只有t1线程啊。
作者: 周金帅    时间: 2012-5-31 17:42
吴小铁你好 发表于 2012-5-31 17:35
呵呵,你把while的位置放错了。应该放在synchronized(o)
外面。

额。。。知道了
作者: 麦田守望者0812    时间: 2012-5-31 17:59
class  TicketDemo
{
         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();
         }
}

class Ticket implements Runnable
{
         private int ticket = 200;
         Object  obj = null;
                 //new String();

        public void run() {
                 obj = new Object();
                 synchronized(obj) {
                                 
                         //while(true) {
                                 while(ticket>0) {
                                         try{
                                                 Thread.sleep(5);
                                         } catch(Exception e) {
                                                 e.printStackTrace();
                                         }
                                         System.out.println(Thread.currentThread().getName()+
                                        " sale:"+ ticket--);
                                 }
                        // }
                        
                }
         }


Thread-3 sale:200
Thread-1 sale:199
Thread-2 sale:197
Thread-0 sale:198
Thread-3 sale:196
Thread-1 sale:195
Thread-0 sale:193
Thread-2 sale:194
Thread-3 sale:192
Thread-1 sale:190
Thread-0 sale:191
Thread-2 sale:189
Thread-3 sale:188
Thread-0 sale:186
Thread-1 sale:187
Thread-2 sale:185
Thread-3 sale:184
Thread-0 sale:182
作者: 右眼会动的人    时间: 2012-5-31 20:47
小手一抖 金币到手




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