use*_*258 57 java multithreading synchronized pthread-join
我对join()
Java中的Threads中使用的方法感到困惑.在以下代码中:
// Using join() to wait for threads to finish.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class DemoJoin {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: "
+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "
+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "
+ ob3.t.isAlive());
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "
+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "
+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "
+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}
Run Code Online (Sandbox Code Playgroud)
此程序的示例输出如下所示:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Two: 1
Three: 1
Two exiting.
Three exiting.
One exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread Exiting
Run Code Online (Sandbox Code Playgroud)
在上面的代码中:
我无法理解程序的执行流程,并且在ob1
创建时会调用构造函数, t.start()
但是仍然run()
会执行main()
方法而不是方法继续执行.那为什么会这样呢?
join()
方法用于等待它被调用的线程没有终止,但在输出中我们看到线程的备用输出为什么?
如果使用的join
是这个,那么有什么用synchronized
?
我知道我在这里错过了一个基本概念,但我无法弄清楚所以请帮忙.
Mal*_*eek 160
您必须了解,线程调度由线程调度程序控制.因此,您无法保证正常情况下线程的执行顺序.
但是,您可以使用join()
等待线程完成其工作.
例如,在你的情况下
ob1.t.join();
Run Code Online (Sandbox Code Playgroud)
在线程t
完成运行之前,此语句不会返回.
试试这个,
class Demo {
Thread t = new Thread(
new Runnable() {
public void run () {
//do something
}
}
);
Thread t1 = new Thread(
new Runnable() {
public void run () {
//do something
}
}
);
t.start(); // Line 15
t.join(); // Line 16
t1.start();
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,您的主线程正在执行.遇到第15行时,线程调度程序可以使用线程t.一旦主线程进入第16行,它将等待线程t
完成.
注意,t.join
没有对线程t
或线程做任何事情t1
.它只影响调用它的main()
线程(即线程).
编辑:
t.join();
需要在try
块内,因为它throws
是InterruptedException
异常,否则你将在编译时得到一个错误.所以,它应该是:
try{
t.join();
}catch(InterruptedException e){
// ...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
109737 次 |
最近记录: |