import java.io.*;
import java.util.Date;
class TestSynchronized implements Runnable

      public synchronized void run(){
 
                    for(int i=0; i<10; i++)
                       {
                         Date now = new Date();
                            System.out.println(Thread.currentThread().getName() + " : " + i);
                            System.out.println(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());  //问题所在
                           try{
                                 Thread.sleep(100); 
                              } catch(InterruptedException e)
                                       { System.out.println("Interrupted"); } 
                       } 
                 }
}
public class HelloWorld 
{
       public static void main(String[] args)
             { 
                   TestSynchronized r1 = new TestSynchronized(); 
                   TestSynchronized r2 = new TestSynchronized(); 
                   Thread t1 = new Thread(r1, "t1"); 
                   Thread t2 = new Thread(r2, "t2");     
                   t1.start(); 
                   t2.start(); 
              }
}
如果没有“//问题所在”这句代码,输出是t1:0
                                      t2:0
                                      t1:1
                                      t2:1
但是加上这段显示输出时间的代码,输出是t1 : 0
                                      t2 : 0
                                      21:55:11
                                      21:55:11
                                      t2 : 1
                                      21:55:12
                                      t1 : 1
                                      21:55:12
这是什么问题啊?百思不得其解。谢谢^-^

解决方案 »

  1.   

    希望的输出是:
    t1 : 0
    21:55:11
    t2 : 0
    21:55:11
    t2 : 1
    21:55:12
    t1 : 1
    21:55:12
      

  2.   

    写错了,希望的输出是
    t1 : 0
    21:55:11
    t2 : 0
    21:55:11
    t1 : 1
    21:55:12
    t2 : 1
    21:55:12
    与实际的输出是不一样的
      

  3.   

    的确没错,是getHours的问题
    看看文档:
    getHours
    public int getHours()Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.HOUR_OF_DAY). Returns the hour represented by this Date object. The returned value is a number (0 through 23) representing the hour within the day that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone. Returns:
    the hour represented by this date.getHours()被Calendar.get(Calendar.HOUR_OF_DAY)替代了
    我将System.out.println(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());  
    改为System.out.println(new Date());
    得到的是你预期的效果
      

  4.   

    更正我的错误,
    我仍然使用System.out.println(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()); 
    但是
    在t2.start();前先t2.sleep(500);
    这样的结果和楼主的期望一样
    所以,我觉得出现这样的问题是计算机对线程的管理造成的,
    随着run中处理时间的增加,不确定性也会增加,
    感觉是在t1在处理System.out.println(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());时,t2创建完毕,并夺取了CPU造成了不期望的结果
    而System.out.println(new Date())的处理时间短,所以在输出后,t2才创建完毕夺取了CPU
      

  5.   

    谢谢mooninsun,已经能
    t1 : 0
    Sun Jul 31 23:15:00 CST 2005
    t2 : 0
    Sun Jul 31 23:15:00 CST 2005
    t2 : 1
    Sun Jul 31 23:15:00 CST 2005
    t1 : 1
    Sun Jul 31 23:15:00 CST 2005
    t2 : 2
    Sun Jul 31 23:15:00 CST 2005
    t1 : 2
    Sun Jul 31 23:15:00 CST 2005
    这样输出了,但是后面的顺序是t2 t1,而不是t1 t2,仍有问题没有解决
      

  6.   

    我想还是线程执行的不确定性
    向我前面说的,在t2.start();前先t2.sleep(500);
    使两个线程的执行时间交叉开来,
    这样输出就没问题了
      

  7.   

    我写了一个你试一试
    import java.io.*;
    import java.util.Date;
    class TestSynchronized implements Runnable
    {   
           public synchronized static void runn(int i){
     
                        
                                System.out.println(Thread.currentThread().getName() + " : " + i);
                                System.out.println(new Date());  //问题所在
                                //Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
                                 try{
                                     Thread.currentThread().sleep(100); 
                                  } catch(InterruptedException e)
                                           { System.out.println("Interrupted"); }    
                       
                           
                     }
          public void run(){for(int i=0;i<10;i++)
                           {
                       
                                runn(i); 
                            
               
                            }
                         }}
    public class HelloWorld 
    {
           public static void main(String[] args)
                 { 
                       TestSynchronized r1 = new TestSynchronized(); 
                       TestSynchronized r2 = new TestSynchronized(); 
                       Thread t1 = new Thread(r1, "t1"); 
                       Thread t2 = new Thread(r2, "t2");     
                       t1.start(); 
                       t2.start(); 
                  }
    }
    我想你应该注意SLEEP()执行的对象和时机.