本帖最后由 henry2004 于 2009-11-19 00:36:01 编辑

解决方案 »

  1.   

    我也Win7 运行没有问题
    可能是10太小了 你试着改成1000看看
      

  2.   


    class ThreadDemo implements Runnable
     {
         private int ticket=10;
         public synchronized void fun()
         {
             if(this.ticket>0)
             {
     System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
                try
                {
                    //延迟操作,产生不同步的后果
                    Thread.sleep(100);
                }
                catch (Exception e)
                {
                }
          //  System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
             }
         }
         public void run()
         {
             while(ticket>0)
             {
                this.fun();
             }
         }
     }
    搞定了,将注释地方的代码放到红色字体位置,就OK了,但是不知道什么原因。
      

  3.   


    class ThreadDemo implements Runnable
     {
         private int ticket=10;
         public synchronized void fun()
         {
             if(this.ticket>0)
             {
     System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);

                try
                {
                    //延迟操作,产生不同步的后果
                    Thread.sleep(100);
                }
                catch (Exception e)
                {
                }
          //  System.out.println(Thread.currentThread().getName()+"卖票:----->"+this.ticket--);
             }
         }
         public void run()
         {
             while(ticket>0)
             {
                this.fun();
             }
         }
     }
    搞定了,将注释地方的代码放到红色字体位置,就OK了,但是不知道什么原因
      

  4.   

    公司只有xp,回去用win 7试试
    sleep不会释放锁,去掉那行代码应该也没关系吧
      

  5.   

    先不谈论线程的安全问题,sleep(100000)也是一样,但是将输出语句放到sleep之前就OK了
      

  6.   

     /**
         * Causes the currently executing thread to sleep (temporarily cease 
         * execution) for the specified number of milliseconds. The thread 
         * does not lose ownership of any monitors.
         *
         * @param      millis   the length of time to sleep in milliseconds.
         * @exception  InterruptedException if another thread has interrupted
         *             the current thread.  The <i>interrupted status</i> of the
         *             current thread is cleared when this exception is thrown.
         * @see        java.lang.Object#notify()
         */
        public static native void sleep(long millis) throws InterruptedException;不知道8楼用的是哪个版本的java啊?
      

  7.   


    微秒?Are you sure?....
      

  8.   

    时间设的太小了,加之thread调度的不确定性导致问题的发生
      

  9.   

    是毫秒阿。难道这也有问题?翻翻API去