//Create a second thread.
class NewThread2 implements Runnable {
Thread t;
NewThread2() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo2 {
public static void main(String args[]) {
new NewThread2(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
这个程序的执行结果是:
Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.
我有一个问题,当子线程建立以后,调用start()方法,再调用run方法,怎么不先执行了run中的第一个for循环,然后sleep(00),再切换到主线程当中来,执行主线程中的第一次for循环, 而是还没有运行子进程的第一次for循环,然后就跳转到主循环中,就开始执行了。这是怎么回事情?哪位能给解释一下。
class NewThread2 implements Runnable {
Thread t;
NewThread2() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo2 {
public static void main(String args[]) {
new NewThread2(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
这个程序的执行结果是:
Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.
我有一个问题,当子线程建立以后,调用start()方法,再调用run方法,怎么不先执行了run中的第一个for循环,然后sleep(00),再切换到主线程当中来,执行主线程中的第一次for循环, 而是还没有运行子进程的第一次for循环,然后就跳转到主循环中,就开始执行了。这是怎么回事情?哪位能给解释一下。
解决方案 »
- 为什么用加号不能分割呢?请大家给个解释
- 求SCJP复习资料 电子版...最好是chm,能拷贝
- 一个调试的问题,在main方法中调用formSubmit(IRequestCycle cycle)
- 刚开始学的菜鸟,几个概念问题请教兄弟们指点一下。谢谢。
- 如何停掉多线程(来者有分)
- 多重继承
- 请教 ,如何做JBUILDER项目的安装程序,如何在其中加入所用到的包,120分献上。
- 学习java时间超过两个月时间的兄弟们请留下你的oicq号码!大家一起切磋进步了!!
- 我想访问局域网中服务器中的一个文件,但告诉我连接被拒绝!
- 请问在java多线程中,是只有run(){}内的代码运行在一个新线程下呢?还是这个类中的代码都运行在一个新线程下?
- 使用URL类出现异常的问题
- 帮我来看下这道题,关于垃圾回收的
Chindthread...
仍然是在主线程中。
如果你在
new NewThread2(); // create a new thread
语句后加一句
Thread.Sleep(0); ,
让主线程主动交出自己时间片,运行的结果可能就会是和你想像的一样了。