当前位置: 技术问答>java相关
请教关于线程的简单问题
来源: 互联网 发布时间:2015-08-16
本文导语: 以下是源程序: public class methodtest{ static class FirstThread extends Thread { public void run(){ try{ System.out.println("First thread starts running."); sleep(10000); S...
以下是源程序:
public class methodtest{
static class FirstThread extends Thread
{
public void run(){
try{
System.out.println("First thread starts running.");
sleep(10000);
System.out.println("First thread finished running.");
}catch(InterruptedException e){}
}
}
static class SecondThread extends Thread
{
public void run()
{
System.out.println("Second thread starts running.");
System.out.println("Second thread suspends itself.");
suspend();
System.out.println("Second thread runs again and finishes.");
}
}
public static void main(String args[]){
FirstThread first=new FirstThread();
SecondThread second=new SecondThread();
first.start();
second.start();
try{
System.out.println("Waiting for first thread to finish...");
first.join();
System.out.println("It's a long wait!");
System.out.println("Waking up second thread...");
second.resume();
System.out.println("Waiting for second thread to finish...");
second.join();
}catch(InterruptedException e){
System.out.println("I'm ready to finish too.");
}
}
}
我期望的结果是在屏幕中先输出"First thread starts running",但是实际运行中先输出的却是"Waiting for first thread to finish...",这是为什么?
请各位高手多多指点。
public class methodtest{
static class FirstThread extends Thread
{
public void run(){
try{
System.out.println("First thread starts running.");
sleep(10000);
System.out.println("First thread finished running.");
}catch(InterruptedException e){}
}
}
static class SecondThread extends Thread
{
public void run()
{
System.out.println("Second thread starts running.");
System.out.println("Second thread suspends itself.");
suspend();
System.out.println("Second thread runs again and finishes.");
}
}
public static void main(String args[]){
FirstThread first=new FirstThread();
SecondThread second=new SecondThread();
first.start();
second.start();
try{
System.out.println("Waiting for first thread to finish...");
first.join();
System.out.println("It's a long wait!");
System.out.println("Waking up second thread...");
second.resume();
System.out.println("Waiting for second thread to finish...");
second.join();
}catch(InterruptedException e){
System.out.println("I'm ready to finish too.");
}
}
}
我期望的结果是在屏幕中先输出"First thread starts running",但是实际运行中先输出的却是"Waiting for first thread to finish...",这是为什么?
请各位高手多多指点。
|
下面的描述或许对你有用(摘自《Sun认证Java2程序员学习指南》):
It is in the running state that most things of interest happen to threads. They execute their run() methods concurrently with other threads, they co-operate, share resources, and they even compete with each other to get their own tasks done.
Because instructions can be performed only one at a time in a single CPU computer, threads have to take turns running. Usually the threads are switching so fast between one another that the user gets the impression of true parallelism (we will discuss this more in the scheduling section of this chapter). For the moment, remember that there is never a guarantee that a thread will execute a series of instructions in one go; it may be switched at any time and get its turn later. If there is more than one CPU available, threads may truly run in parallel.
Once a thread is started, it is not guaranteed to be running all the time. The Java virtual machine (JVM) could switch over to another thread and execute it for a while, giving it some CPU time. These various states are standard Java terms used to describe the state of a thread at any given time during execution. There are essentially five states a thread can be in: new Thread,Runnable,Running,Waiting/Blocking,Dead.
---------------------------------------------------------------
应该讲得很清楚,在second.start()之后,first和second实际上还没有达到Running状态,CPU继续被Main()方法占有,所以先输出的是Waiting for...
至于CPU的分配,则和系统有关。
It is in the running state that most things of interest happen to threads. They execute their run() methods concurrently with other threads, they co-operate, share resources, and they even compete with each other to get their own tasks done.
Because instructions can be performed only one at a time in a single CPU computer, threads have to take turns running. Usually the threads are switching so fast between one another that the user gets the impression of true parallelism (we will discuss this more in the scheduling section of this chapter). For the moment, remember that there is never a guarantee that a thread will execute a series of instructions in one go; it may be switched at any time and get its turn later. If there is more than one CPU available, threads may truly run in parallel.
Once a thread is started, it is not guaranteed to be running all the time. The Java virtual machine (JVM) could switch over to another thread and execute it for a while, giving it some CPU time. These various states are standard Java terms used to describe the state of a thread at any given time during execution. There are essentially five states a thread can be in: new Thread,Runnable,Running,Waiting/Blocking,Dead.
---------------------------------------------------------------
应该讲得很清楚,在second.start()之后,first和second实际上还没有达到Running状态,CPU继续被Main()方法占有,所以先输出的是Waiting for...
至于CPU的分配,则和系统有关。