- 浏览: 981668 次
- 性别:
- 来自: 上海
-
文章分类
最新评论
-
Mr.Cheney:
去掉 UUID字符串中的“-” 直接replaceAll(&q ...
JAVA生成全局唯一ID 使用 java.util.UUID -
呜哩喵:
楼主nice
java中的时间操作 -
zxs6587:
Thinking inJava我读着好像说要建立基类对象啊!请 ...
创建子类的对象时也要创建其所有父类的对象? -
just_Word:
getFullYear
date.getyear -
JamesQian:
我觉得楼上的synchronized(this),notify ...
notify() wait()
原创 浅析 Java Thread.join() 收藏
一、在研究join的用法之前,先明确两件事情。
1.join方法定义在Thread类中,则调用者必须是一个线程,
例如:
Thread t = new CustomThread();//这里一般是自定义的线程类
t.start();//线程起动
t.join();//此处会抛出InterruptedException异常
2.上面的两行代码也是在一个线程里面执行的。
以上出现了两个线程,一个是我们自定义的线程类,我们实现了run方法,做一些我们需要的工作;另外一个线程,生成我们自定义线程类的对象,然后执行
customThread.start();
customThread.join();
在这种情况下,两个线程的关系是一个线程由另外一个线程生成并起动,所以我们暂且认为第一个线程叫做“子线程”,另外一个线程叫做“主线程”。
二、为什么要用join()方法
主线程生成并起动了子线程,而子线程里要进行大量的耗时的运算(这里可以借鉴下线程的作用),当主线程处理完其他的事务后,需要用到子线程的处理结果,这个时候就要用到join();方法了。
三、join方法的作用
在网上看到有人说“将两个线程合并”。这样解释我觉得理解起来还更麻烦。不如就借鉴下API里的说法:
“等待该线程终止。”
解释一下,是主线程(我在 “一”里已经命名过了)等待子线程的终止。也就是在子线程调用了join()方法后面的代码,只有等到子线程结束了才能执行。(Waits for this thread to die.)
四、用实例来理解
写一个简单的例子来看一下join()的用法,一共三个类:
1.CustomThread 类
2. CustomThread1类
3. JoinTestDemo 类,main方法所在的类。
代码1:
view plaincopy to clipboardprint?
1. package wxhx.csdn2;
2. /**
3. *
4. * @author bzwm
5. *
6. */
7. class CustomThread1 extends Thread {
8. public CustomThread1() {
9. super("[CustomThread1] Thread");
10. };
11. public void run() {
12. String threadName = Thread.currentThread().getName();
13. System.out.println(threadName + " start.");
14. try {
15. for (int i = 0; i < 5; i++) {
16. System.out.println(threadName + " loop at " + i);
17. Thread.sleep(1000);
18. }
19. System.out.println(threadName + " end.");
20. } catch (Exception e) {
21. System.out.println("Exception from " + threadName + ".run");
22. }
23. }
24. }
25. class CustomThread extends Thread {
26. CustomThread1 t1;
27. public CustomThread(CustomThread1 t1) {
28. super("[CustomThread] Thread");
29. this.t1 = t1;
30. }
31. public void run() {
32. String threadName = Thread.currentThread().getName();
33. System.out.println(threadName + " start.");
34. try {
35. t1.join();
36. System.out.println(threadName + " end.");
37. } catch (Exception e) {
38. System.out.println("Exception from " + threadName + ".run");
39. }
40. }
41. }
42. public class JoinTestDemo {
43. public static void main(String[] args) {
44. String threadName = Thread.currentThread().getName();
45. System.out.println(threadName + " start.");
46. CustomThread1 t1 = new CustomThread1();
47. CustomThread t = new CustomThread(t1);
48. try {
49. t1.start();
50. Thread.sleep(2000);
51. t.start();
52. t.join();//在代碼2里,將此處注釋掉
53. } catch (Exception e) {
54. System.out.println("Exception from main");
55. }
56. System.out.println(threadName + " end!");
57. }
58. }
package wxhx.csdn2; /** * * @author bzwm * */ class CustomThread1 extends Thread { public CustomThread1() { super("[CustomThread1] Thread"); }; public void run() { String threadName = Thread.currentThread().getName(); System.out.println(threadName + " start."); try { for (int i = 0; i < 5; i++) { System.out.println(threadName + " loop at " + i); Thread.sleep(1000); } System.out.println(threadName + " end."); } catch (Exception e) { System.out.println("Exception from " + threadName + ".run"); } } } class CustomThread extends Thread { CustomThread1 t1; public CustomThread(CustomThread1 t1) { super("[CustomThread] Thread"); this.t1 = t1; } public void run() { String threadName = Thread.currentThread().getName(); System.out.println(threadName + " start."); try { t1.join(); System.out.println(threadName + " end."); } catch (Exception e) { System.out.println("Exception from " + threadName + ".run"); } } } public class JoinTestDemo { public static void main(String[] args) { String threadName = Thread.currentThread().getName(); System.out.println(threadName + " start."); CustomThread1 t1 = new CustomThread1(); CustomThread t = new CustomThread(t1); try { t1.start(); Thread.sleep(2000); t.start(); t.join();//在代碼2里,將此處注釋掉 } catch (Exception e) { System.out.println("Exception from main"); } System.out.println(threadName + " end!"); } }
打印结果:
main start.//main方法所在的线程起动,但没有马上结束,因为调用t.join();,所以要等到t结束了,此线程才能向下执行。
[CustomThread1] Thread start.//线程CustomThread1起动
[CustomThread1] Thread loop at 0//线程CustomThread1执行
[CustomThread1] Thread loop at 1//线程CustomThread1执行
[CustomThread] Thread start.//线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。
[CustomThread1] Thread loop at 2//线程CustomThread1继续执行
[CustomThread1] Thread loop at 3//线程CustomThread1继续执行
[CustomThread1] Thread loop at 4//线程CustomThread1继续执行
[CustomThread1] Thread end. //线程CustomThread1结束了
[CustomThread] Thread end.// 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果
main end!//线程CustomThread结束,此线程在t.join();阻塞处起动,向下继续执行的结果。
修改一下代码,得到代码2:(这里只写出修改的部分)
view plaincopy to clipboardprint?
1. public class JoinTestDemo {
2. public static void main(String[] args) {
3. String threadName = Thread.currentThread().getName();
4. System.out.println(threadName + " start.");
5. CustomThread1 t1 = new CustomThread1();
6. CustomThread t = new CustomThread(t1);
7. try {
8. t1.start();
9. Thread.sleep(2000);
10. t.start();
11. // t.join();//在代碼2里,將此處注釋掉
12. } catch (Exception e) {
13. System.out.println("Exception from main");
14. }
15. System.out.println(threadName + " end!");
16. }
17. }
public class JoinTestDemo { public static void main(String[] args) { String threadName = Thread.currentThread().getName(); System.out.println(threadName + " start."); CustomThread1 t1 = new CustomThread1(); CustomThread t = new CustomThread(t1); try { t1.start(); Thread.sleep(2000); t.start(); // t.join();//在代碼2里,將此處注釋掉 } catch (Exception e) { System.out.println("Exception from main"); } System.out.println(threadName + " end!"); } }
打印结果:
main start. // main方法所在的线程起动,但没有马上结束,这里并不是因为join方法,而是因为Thread.sleep(2000);
[CustomThread1] Thread start. //线程CustomThread1起动
[CustomThread1] Thread loop at 0//线程CustomThread1执行
[CustomThread1] Thread loop at 1//线程CustomThread1执行
main end!// Thread.sleep(2000);结束,虽然在线程CustomThread执行了t1.join();,但这并不会影响到其他线程(这里main方法所在的线程)。
[CustomThread] Thread start. //线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。
[CustomThread1] Thread loop at 2//线程CustomThread1继续执行
[CustomThread1] Thread loop at 3//线程CustomThread1继续执行
[CustomThread1] Thread loop at 4//线程CustomThread1继续执行
[CustomThread1] Thread end. //线程CustomThread1结束了
[CustomThread] Thread end. // 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果
五、从源码看join()方法
在CustomThread的run方法里,执行了t1.join();,进入看一下它的JDK源码:
view plaincopy to clipboardprint?
1. public final void join() throws InterruptedException {
2. n(0);
3. }
public final void join() throws InterruptedException { join(0); }
然后进入join(0)方法:
view plaincopy to clipboardprint?
1. /**
2. * Waits at most <code>millis</code> milliseconds for this thread to
3. * die. A timeout of <code>0</code> means to wait forever. // 注意这句
4. *
5. * @param millis the time to wait in milliseconds.
6. * @exception InterruptedException if another thread has interrupted
7. * the current thread. The <i>interrupted status</i> of the
8. * current thread is cleared when this exception is thrown.
9. */
10. public final synchronized void join(long millis) //参数millis为0.
11. throws InterruptedException {
12. long base = System.currentTimeMillis();
13. long now = 0;
14. if (millis < 0) {
15. throw new IllegalArgumentException("timeout value is negative");
16. }
17. if (millis == 0) {//进入这个分支
18. while (isAlive()) {//判断本线程是否为活动的。这里的本线程就是t1.
19. wait(0);//阻塞
20. }
21. } else {
22. while (isAlive()) {
23. long delay = millis - now;
24. if (delay <= 0) {
25. break;
26. }
27. wait(delay);
28. now = System.currentTimeMillis() - base;
29. }
30. }
31. }
/** * Waits at most <code>millis</code> milliseconds for this thread to * die. A timeout of <code>0</code> means to wait forever. //注意这句 * * @param millis the time to wait in milliseconds. * @exception InterruptedException if another thread has interrupted * the current thread. The <i>interrupted status</i> of the * current thread is cleared when this exception is thrown. */ public final synchronized void join(long millis) //参数millis为0. throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) {//进入这个分支 while (isAlive()) {//判断本线程是否为活动的。这里的本线程就是t1. wait(0);//阻塞 } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } }
单纯从代码上看,如果线程被生成了,但还未被起动,调用它的join()方法是没有作用的。将直接继续向下执行,这里就不写代码验证了。
一、在研究join的用法之前,先明确两件事情。
1.join方法定义在Thread类中,则调用者必须是一个线程,
例如:
Thread t = new CustomThread();//这里一般是自定义的线程类
t.start();//线程起动
t.join();//此处会抛出InterruptedException异常
2.上面的两行代码也是在一个线程里面执行的。
以上出现了两个线程,一个是我们自定义的线程类,我们实现了run方法,做一些我们需要的工作;另外一个线程,生成我们自定义线程类的对象,然后执行
customThread.start();
customThread.join();
在这种情况下,两个线程的关系是一个线程由另外一个线程生成并起动,所以我们暂且认为第一个线程叫做“子线程”,另外一个线程叫做“主线程”。
二、为什么要用join()方法
主线程生成并起动了子线程,而子线程里要进行大量的耗时的运算(这里可以借鉴下线程的作用),当主线程处理完其他的事务后,需要用到子线程的处理结果,这个时候就要用到join();方法了。
三、join方法的作用
在网上看到有人说“将两个线程合并”。这样解释我觉得理解起来还更麻烦。不如就借鉴下API里的说法:
“等待该线程终止。”
解释一下,是主线程(我在 “一”里已经命名过了)等待子线程的终止。也就是在子线程调用了join()方法后面的代码,只有等到子线程结束了才能执行。(Waits for this thread to die.)
四、用实例来理解
写一个简单的例子来看一下join()的用法,一共三个类:
1.CustomThread 类
2. CustomThread1类
3. JoinTestDemo 类,main方法所在的类。
代码1:
view plaincopy to clipboardprint?
1. package wxhx.csdn2;
2. /**
3. *
4. * @author bzwm
5. *
6. */
7. class CustomThread1 extends Thread {
8. public CustomThread1() {
9. super("[CustomThread1] Thread");
10. };
11. public void run() {
12. String threadName = Thread.currentThread().getName();
13. System.out.println(threadName + " start.");
14. try {
15. for (int i = 0; i < 5; i++) {
16. System.out.println(threadName + " loop at " + i);
17. Thread.sleep(1000);
18. }
19. System.out.println(threadName + " end.");
20. } catch (Exception e) {
21. System.out.println("Exception from " + threadName + ".run");
22. }
23. }
24. }
25. class CustomThread extends Thread {
26. CustomThread1 t1;
27. public CustomThread(CustomThread1 t1) {
28. super("[CustomThread] Thread");
29. this.t1 = t1;
30. }
31. public void run() {
32. String threadName = Thread.currentThread().getName();
33. System.out.println(threadName + " start.");
34. try {
35. t1.join();
36. System.out.println(threadName + " end.");
37. } catch (Exception e) {
38. System.out.println("Exception from " + threadName + ".run");
39. }
40. }
41. }
42. public class JoinTestDemo {
43. public static void main(String[] args) {
44. String threadName = Thread.currentThread().getName();
45. System.out.println(threadName + " start.");
46. CustomThread1 t1 = new CustomThread1();
47. CustomThread t = new CustomThread(t1);
48. try {
49. t1.start();
50. Thread.sleep(2000);
51. t.start();
52. t.join();//在代碼2里,將此處注釋掉
53. } catch (Exception e) {
54. System.out.println("Exception from main");
55. }
56. System.out.println(threadName + " end!");
57. }
58. }
package wxhx.csdn2; /** * * @author bzwm * */ class CustomThread1 extends Thread { public CustomThread1() { super("[CustomThread1] Thread"); }; public void run() { String threadName = Thread.currentThread().getName(); System.out.println(threadName + " start."); try { for (int i = 0; i < 5; i++) { System.out.println(threadName + " loop at " + i); Thread.sleep(1000); } System.out.println(threadName + " end."); } catch (Exception e) { System.out.println("Exception from " + threadName + ".run"); } } } class CustomThread extends Thread { CustomThread1 t1; public CustomThread(CustomThread1 t1) { super("[CustomThread] Thread"); this.t1 = t1; } public void run() { String threadName = Thread.currentThread().getName(); System.out.println(threadName + " start."); try { t1.join(); System.out.println(threadName + " end."); } catch (Exception e) { System.out.println("Exception from " + threadName + ".run"); } } } public class JoinTestDemo { public static void main(String[] args) { String threadName = Thread.currentThread().getName(); System.out.println(threadName + " start."); CustomThread1 t1 = new CustomThread1(); CustomThread t = new CustomThread(t1); try { t1.start(); Thread.sleep(2000); t.start(); t.join();//在代碼2里,將此處注釋掉 } catch (Exception e) { System.out.println("Exception from main"); } System.out.println(threadName + " end!"); } }
打印结果:
main start.//main方法所在的线程起动,但没有马上结束,因为调用t.join();,所以要等到t结束了,此线程才能向下执行。
[CustomThread1] Thread start.//线程CustomThread1起动
[CustomThread1] Thread loop at 0//线程CustomThread1执行
[CustomThread1] Thread loop at 1//线程CustomThread1执行
[CustomThread] Thread start.//线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。
[CustomThread1] Thread loop at 2//线程CustomThread1继续执行
[CustomThread1] Thread loop at 3//线程CustomThread1继续执行
[CustomThread1] Thread loop at 4//线程CustomThread1继续执行
[CustomThread1] Thread end. //线程CustomThread1结束了
[CustomThread] Thread end.// 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果
main end!//线程CustomThread结束,此线程在t.join();阻塞处起动,向下继续执行的结果。
修改一下代码,得到代码2:(这里只写出修改的部分)
view plaincopy to clipboardprint?
1. public class JoinTestDemo {
2. public static void main(String[] args) {
3. String threadName = Thread.currentThread().getName();
4. System.out.println(threadName + " start.");
5. CustomThread1 t1 = new CustomThread1();
6. CustomThread t = new CustomThread(t1);
7. try {
8. t1.start();
9. Thread.sleep(2000);
10. t.start();
11. // t.join();//在代碼2里,將此處注釋掉
12. } catch (Exception e) {
13. System.out.println("Exception from main");
14. }
15. System.out.println(threadName + " end!");
16. }
17. }
public class JoinTestDemo { public static void main(String[] args) { String threadName = Thread.currentThread().getName(); System.out.println(threadName + " start."); CustomThread1 t1 = new CustomThread1(); CustomThread t = new CustomThread(t1); try { t1.start(); Thread.sleep(2000); t.start(); // t.join();//在代碼2里,將此處注釋掉 } catch (Exception e) { System.out.println("Exception from main"); } System.out.println(threadName + " end!"); } }
打印结果:
main start. // main方法所在的线程起动,但没有马上结束,这里并不是因为join方法,而是因为Thread.sleep(2000);
[CustomThread1] Thread start. //线程CustomThread1起动
[CustomThread1] Thread loop at 0//线程CustomThread1执行
[CustomThread1] Thread loop at 1//线程CustomThread1执行
main end!// Thread.sleep(2000);结束,虽然在线程CustomThread执行了t1.join();,但这并不会影响到其他线程(这里main方法所在的线程)。
[CustomThread] Thread start. //线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。
[CustomThread1] Thread loop at 2//线程CustomThread1继续执行
[CustomThread1] Thread loop at 3//线程CustomThread1继续执行
[CustomThread1] Thread loop at 4//线程CustomThread1继续执行
[CustomThread1] Thread end. //线程CustomThread1结束了
[CustomThread] Thread end. // 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果
五、从源码看join()方法
在CustomThread的run方法里,执行了t1.join();,进入看一下它的JDK源码:
view plaincopy to clipboardprint?
1. public final void join() throws InterruptedException {
2. n(0);
3. }
public final void join() throws InterruptedException { join(0); }
然后进入join(0)方法:
view plaincopy to clipboardprint?
1. /**
2. * Waits at most <code>millis</code> milliseconds for this thread to
3. * die. A timeout of <code>0</code> means to wait forever. // 注意这句
4. *
5. * @param millis the time to wait in milliseconds.
6. * @exception InterruptedException if another thread has interrupted
7. * the current thread. The <i>interrupted status</i> of the
8. * current thread is cleared when this exception is thrown.
9. */
10. public final synchronized void join(long millis) //参数millis为0.
11. throws InterruptedException {
12. long base = System.currentTimeMillis();
13. long now = 0;
14. if (millis < 0) {
15. throw new IllegalArgumentException("timeout value is negative");
16. }
17. if (millis == 0) {//进入这个分支
18. while (isAlive()) {//判断本线程是否为活动的。这里的本线程就是t1.
19. wait(0);//阻塞
20. }
21. } else {
22. while (isAlive()) {
23. long delay = millis - now;
24. if (delay <= 0) {
25. break;
26. }
27. wait(delay);
28. now = System.currentTimeMillis() - base;
29. }
30. }
31. }
/** * Waits at most <code>millis</code> milliseconds for this thread to * die. A timeout of <code>0</code> means to wait forever. //注意这句 * * @param millis the time to wait in milliseconds. * @exception InterruptedException if another thread has interrupted * the current thread. The <i>interrupted status</i> of the * current thread is cleared when this exception is thrown. */ public final synchronized void join(long millis) //参数millis为0. throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (millis == 0) {//进入这个分支 while (isAlive()) {//判断本线程是否为活动的。这里的本线程就是t1. wait(0);//阻塞 } } else { while (isAlive()) { long delay = millis - now; if (delay <= 0) { break; } wait(delay); now = System.currentTimeMillis() - base; } } }
单纯从代码上看,如果线程被生成了,但还未被起动,调用它的join()方法是没有作用的。将直接继续向下执行,这里就不写代码验证了。
发表评论
-
关于数组和List之间相互转换的方法
2011-04-14 21:04 13901.List转换成为数组。( ... -
java的几种对象(PO,VO,DAO,BO,POJO)解释
2011-03-24 10:13 1350java的几种对象(PO,VO,DAO,BO,POJO)解释 ... -
switch
2010-12-02 19:02 11501 public class Switch { 2 ... -
优化的冒泡排序
2010-09-25 14:18 1372public static void bubble_Sort( ... -
java变量命名规则
2010-08-13 23:15 23761. 大小写有别,例如 a 和 A是两个变量 2. 长度任意 ... -
String.getBytes()的问题
2010-08-13 22:46 1654转载 http://java.chinaitlab.c ... -
tomcat 修改端口
2010-08-09 22:41 2009Tomcat端口修改: 在Tomcat安装目录下的conf目 ... -
tomcat 中增加用户名和密码
2010-08-09 22:41 1924原来的tomcat-user.xml是 <?xml ... -
Eclipse is running in a JRE, but a JDK is required
2010-07-28 09:30 15351 安装了maven插件,使用的时候老是有这样的提示: 08- ... -
安装Eclipse的maven插件
2010-07-27 11:01 1831Installing m2eclipse Core To i ... -
Attach Library Sources and Javadocs
2010-07-26 13:41 1935Attach Library Sources and Java ... -
maven 安装jaxb插件
2010-07-18 15:10 65781. Put your schemas ( ... -
java接受控制台输入
2010-07-16 13:45 2719import java.io.*; public c ... -
将xsd文件转化为java类
2010-07-10 15:31 2519最近有一个需求是把xsd文件中定义的数据类型转化为java类 ... -
jconsole attache sun glassfish
2010-06-13 17:04 1358To Set Up JConsole Connectivity ... -
suse下lamp的安装
2010-05-31 16:45 1568首先卸载suse缺省安装的apache2 主要是在网上看到人家 ... -
java的property配置文件的用法
2010-05-30 15:04 1149在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我 ... -
让ubuntu下的eclipse支持GBK编码
2010-05-30 14:38 1528今天,把windows下的工程导入到了Linux下eclips ... -
java路径中/的问题
2010-05-18 17:23 1373windows支持两种文件分隔符“/”和“\” 且使用“/”时 ... -
java中serializable是可以继承的
2010-05-16 21:58 5514import java.io.FileInputStream; ...
相关推荐
在Java中,ForkJoinPool使用ForkJoinTask作为任务的具体实现。ForkJoinTask是一个抽象类,它有两个主要的子类:RecursiveAction和RecursiveTask。RecursiveAction用于没有返回结果的任务,而RecursiveTask可以返回...
join()方法是Thread类中的一个实例方法,当一个线程A执行了threadB.join()操作时,线程A会等待线程B执行完成后才继续执行。这通常用于需要等待子线程完成之后才继续执行的情况。 以上就是对Java线程中wait、await、...
在Java编程语言中,"左关联"和"右关联"是数据库查询操作中的概念,通常在SQL中使用JOIN语句实现。在这个场景下,我们讨论的是如何使用Java代码来模拟这些数据库操作,以达到高效、便捷地处理数据关联的目的。 首先...
在Java多线程编程中,理解并正确使用`yield`和`join`方法是至关重要的。这两个方法都属于线程控制策略的一部分,但它们的作用和使用场景有所不同。 首先,我们来详细探讨`Thread.yield()`方法。这个方法的目的是让...
本文介绍了Java中的线程join方法,包括使用Thread类的join方法和使用Thread类的wait和notify方法两种方式,并且讨论了join方法的应用场景。通过join方法,我们可以实现线程同步,确保线程的安全执行。
在Java中,`join()`、`daemon`线程以及同步机制是多线程编程中的重要概念,对于理解和编写高效的并发代码至关重要。 首先,我们来讨论`join()`方法。在多线程环境中,有时候我们需要确保一个线程在执行完它的任务后...
现在需要根据一个输入的字符"list1.column1=list2.column2,list1.column3=list3.column4"(不是固定的)来实现inner join关系的控制,即list1中的map和list2中map通过key值column1和column2关联,同时list1中的map和...
通过上述介绍和示例代码,你应该对Java中的Fork/Join框架有了更深入的理解。这种框架特别适合于需要并行处理大量子任务的场景,如大数据处理、图像处理等领域。希望本文能够帮助你在实际项目中更好地应用Fork/Join...
标题和描述中提到的"Java使用join方法暂停当前线程"其实是指,通过调用`join()`方法让当前线程等待目标线程完成其执行。在这个过程中,当前线程会被阻塞,直到被`join()`的线程执行完毕,然后当前线程才会继续执行。...
主要介绍了java 线程方法join简单用法,结合实例形式总结分析了Java线程join方法的功能、原理及使用技巧,需要的朋友可以参考下
Java线程中的`join()`方法是一个非常重要的同步工具,它允许一个线程(通常为主线程)等待另一个线程(子线程)执行完成后再继续执行。`join()`方法定义在`java.lang.Thread`类中,它使得多线程间的协作更加有序。 ...
而`Java.jpg`可能是用来辅助理解多线程概念的图片,例如展示线程的生命周期或join()方法在程序流程中的位置。 值得注意的是,join()方法可以指定等待的时间,即`thread.join(long millis)`,如果被join的线程在这段...
Java并发Fork-Join框架原理是Java7中提供的一种并行执行任务的框架,旨在提高程序的执行效率和性能。该框架的核心思想是将大任务分割成若干个小任务,并将其分配给不同的线程执行,以充分利用多核CPU的计算能力。 ...
Fork/Join框架是Java并发库中的一部分,自Java 7开始引入,它为开发者提供了一种高效的处理大规模计算任务的方法。这个框架基于分治策略,将大任务分解成若干小任务,然后并行执行这些小任务,最后再将结果合并。...
在Java中,API包含了类、接口、枚举和注解等,为开发者提供了丰富的功能和工具。 首先,Java 8中的主要新特性之一是Lambda表达式。Lambda表达式简化了函数式编程,允许开发者以更简洁的方式编写匿名函数。例如,...
fork/join框架是ExecutorService接口的一个实现,可以帮助开发人员充分利用多核处理器的优势,编写出并行执行的程序,提高应用程序的性能;设计的目的是为了处理那些可以被递归拆分的任务。
在Java中,避免UTF-8的csv文件打开中文出现乱码的方法是非常重要的。csv文件是 comma separated values 的缩写,常用于数据交换和导入导出操作。然而,在Java中读取和写入csv文件时,中文字符如果不正确地处理,可能...
本文将简要回顾Java中的并发编程基础知识,介绍java.util.concurrent包提供的高级并发原语,并深入探讨Fork/Join框架及其在Java SE 7中的应用。 首先,让我们回顾一下Java中基本的并发机制。自Java早期版本起,线程...
在Java中,join方法是Thread类中的一个非静态方法,用于让一个线程等待另一个线程的执行完毕。例如,Thread t = new MyThread(); t.start(); t.join();在这里,主线程会等待MyThread线程的执行完毕,然后继续执行。 ...
Java中join线程操作实例分析提供了一个详细的实例,展示了join()方法的使用和实现原理。通过join()方法,可以实现线程之间的同步操作,提高程序的稳定性和效率。 相关知识点: * Java多线程编程 * Join线程操作 * ...