Java多线程的join方法
2017-03-15 15:29
337 查看
join方法:
例如我们有耗时操作需要在子线程完成的话,然后需要早主线程获取结果的话,我们可以如下实现:
package com.daxin;
class JoinThread extends Thread {
private int count = 0;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public void run() {
try {
Thread.sleep(5000);
for (int i = 0; i < 6; i++) {
count++;
}
System.out.println("子线程count运行完毕,count =" + count);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class JoinDemo {
public static void main(String[] args) throws Exception {
JoinThread jt = new JoinThread();
jt.start();
System.out.println(jt.getCount());
}
}
但是实际上输出的是:0,这是因为子线程没有执行完毕,所以导致主线程获取的结果不正确。那么如何实现这个呢 ?
其实可以使用Callable接口实现异步获取结果,但是我们也可以使用Thread实现,那么就需要使用join方法:
package com.daxin;
class JoinThread extends Thread {
private int count = 0;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public void run() {
try {
Thread.sleep(5000);
for (int i = 0; i < 6; i++) {
count++;
}
System.out.println("子线程count运行完毕,count =" + count);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class JoinDemo {
public static void main(String[] args) throws Exception {
JoinThread jt = new JoinThread();
jt.start();
jt.join();
System.out.println(jt.getCount());
}
}
这次输出:6 正确。join的方法作用就是等待子线程完毕之后,加入主线程一起向下执行。也正符合join的意思!
例如我们有耗时操作需要在子线程完成的话,然后需要早主线程获取结果的话,我们可以如下实现:
package com.daxin;
class JoinThread extends Thread {
private int count = 0;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public void run() {
try {
Thread.sleep(5000);
for (int i = 0; i < 6; i++) {
count++;
}
System.out.println("子线程count运行完毕,count =" + count);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class JoinDemo {
public static void main(String[] args) throws Exception {
JoinThread jt = new JoinThread();
jt.start();
System.out.println(jt.getCount());
}
}
但是实际上输出的是:0,这是因为子线程没有执行完毕,所以导致主线程获取的结果不正确。那么如何实现这个呢 ?
其实可以使用Callable接口实现异步获取结果,但是我们也可以使用Thread实现,那么就需要使用join方法:
package com.daxin;
class JoinThread extends Thread {
private int count = 0;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public void run() {
try {
Thread.sleep(5000);
for (int i = 0; i < 6; i++) {
count++;
}
System.out.println("子线程count运行完毕,count =" + count);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class JoinDemo {
public static void main(String[] args) throws Exception {
JoinThread jt = new JoinThread();
jt.start();
jt.join();
System.out.println(jt.getCount());
}
}
这次输出:6 正确。join的方法作用就是等待子线程完毕之后,加入主线程一起向下执行。也正符合join的意思!
相关文章推荐
- 初学Java多线程:join方法的使用
- java例程练习(多线程[join()方法])
- Java多线程之interrupt()方法与sleep(),join(),wait()的关系
- Java—Java多线程中join方法的理解
- Java多线程中join方法的理解
- java多线程中的join方法详解
- 转载:Java多线程中join方法的理解
- java 多线程并行求和 之 join() 方法应用
- java多线程中join()方法
- Java多线程中join方法的理解
- java例程练习(多线程——join()方法)
- java基本教程之join方法详解 java多线程教程
- java基础学习__多线程(停止线程,守护线程,join方法)
- java 多线程并行求数值积分(πPI) 之 join() 方法应用
- 5. 初学Java多线程:join方法的使用
- java例程练习(多线程[join()方法])
- Java多线程中join方法的理解
- 五、初学Java多线程:join方法的使用
- 黑马程序员:Java基础——多线程的停止与守护以及Join,优先级和yield方法
- Java多线程编程方法join -深入浅出Java