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
这是什么问题啊?百思不得其解。谢谢^-^
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
这是什么问题啊?百思不得其解。谢谢^-^
解决方案 »
- 帮忙,谢谢!
- Java 图片问题,请高手指教,在线等!
- jsp表单中如何动态的获取select的值
- java 中的String[]问题
- 一个Method split(java.lang.String) 的问题?
- 请教一个简单问题:重载是不是一定要在同一class中? 重写是不是要求一定跟基类参数一致?
- 问个问!
- 请问:我怎么用java程序如何取出.doc或.excel文件里的内容啊?急!!!(100分)
- win2000下如何设置tomcat4.0的运行环境:(请高手不吝赐教,thx
- 使用迭代器时出现了错误:不兼容的类型:Object无法转换为Employee。求大神点播为什么?
- 我实在是不明白,打死我也不相信的问题
- 如何改变java application 的风格
t1 : 0
21:55:11
t2 : 0
21:55:11
t2 : 1
21:55:12
t1 : 1
21:55:12
t1 : 0
21:55:11
t2 : 0
21:55:11
t1 : 1
21:55:12
t2 : 1
21:55:12
与实际的输出是不一样的
看看文档:
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());
得到的是你预期的效果
我仍然使用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
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,仍有问题没有解决
向我前面说的,在t2.start();前先t2.sleep(500);
使两个线程的执行时间交叉开来,
这样输出就没问题了
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()执行的对象和时机.