public class One extends Thread {
private boolean running;
public void setRunning(boolean running) {
this.running = running;
} public void run()
{
while(running)
{
System.out.println("Thread one running.....");
}
}}
public class TestJiaoCuo { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
One one = new One(); one.setRunning(true);
one.start();
try {
one.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
one.setRunning(false); }}请问这个程序在main方法中,one.start();和one.sleep(100)是不是同时在运行啊,我实在弄不懂,我的程序运行结果是这个线程运行了一段时间后就停止了,那我的理解是,线程start以后不是马上就sleep了吗?为什么还有一段时间的输出呢?反正脑子里很乱,麻烦哪位高手解释下下
private boolean running;
public void setRunning(boolean running) {
this.running = running;
} public void run()
{
while(running)
{
System.out.println("Thread one running.....");
}
}}
public class TestJiaoCuo { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
One one = new One(); one.setRunning(true);
one.start();
try {
one.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
one.setRunning(false); }}请问这个程序在main方法中,one.start();和one.sleep(100)是不是同时在运行啊,我实在弄不懂,我的程序运行结果是这个线程运行了一段时间后就停止了,那我的理解是,线程start以后不是马上就sleep了吗?为什么还有一段时间的输出呢?反正脑子里很乱,麻烦哪位高手解释下下
try {
one.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
start()的线程和sleep()的线程不是同一个线程吧.
[时间片] = JVm分配给线程的一小段处理时间One one = new One(); // 构造线程
one.setRunning(true); // 设置线程可运行
one.start(); // 启动线程, 注意,启动线程,只是标志线程处于可运行状态,到底啥时候运行,那得看JVM什么时候分配给他[时间片]了。try {
one.sleep(1000); // 这里休眠1秒,注意,等同于 Thread.sleep(1000); 主线程休眠,就是为了把JVM时间片交还,好让One线程有时间运行。
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} one.setRunning(false); // 退出运行结果分析1 One 线程,运行的最大时间应该在1秒钟左右。
2 如果你去掉了sleep(1000);, 那么主线程刚启动One线程,主线程停止了,JVM也就自动停止了,One线程还没有拿到运行[时间片]啊!建议:
看一下线程状态的说明。以及多线程的JVm处理时间分配方面的资料
one.sleep == Thread.sleep ???
不一样吧?Thread.sleep 是指让当前线程睡眠 这里也就是主线程
public static void sleep(long millis)
throws InterruptedException
在指定的毫秒数内让当前正在执行的线程休眠(暂停执行)。该线程不丢失任何监视器的所属权。 这是一个static方法,作用于当前正在执行的线程
你的理解有问题。
public class TestJiaoCuo {
public static void main(String[] args) {
try {
Thread thr = new Thread(){
public void run() {
while (true) {
try {
System.out.println("thr线程......");
this.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
thr.start();
while (true) {
try {
Thread.sleep(1000);
System.out.println("main线程...");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
上面的代码 输出的
thr线程......
明显要比"main线程......"多的多
你怎么解释?
去掉 ,你就会发现结果是相反的
main 每次休眠1000,
当然 thr 得到的时间片多一些,当然 thr 多了,应该在10倍左右。
while (true) {
try {
thr.sleep(1000); // 你把这里改成 thr, 看一下结果吧!
System.out.println("main线程...");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}这个能证明,thr.sleep 和 Thread.sleep 是没有区别的,他都是让当前(main)线程休眠1秒。 而不是让 thr 线程休眠。sleep 是 static 方法,请再次注意这个问题,它是不依赖某个具体的线程的。
你说Thread.sleep(1000) 等于 one.sleep(1000);可你在12楼又说:
thr线程 每次休眠100 //这里没错
main 每次休眠1000, ///可是这里 你又说是main线程睡眠1000 这里你为什么不认为Thread.sleep 等于 thr.sleep呢? 而又是main线程了?难道就因为换了个名字?
他只会影响当前执行sleep的线程
受教了 谢谢!!!!
当前是thr ,则 Thread.slee 和 thr.sleep 都是让 thr休眠。 当然,在thr里面,应该可以用 this.sleep();