在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java多线程中关于join方法的使用实例解析

2017-3-7 12:50| 发布者: zhangjf| 查看: 1066| 评论: 0

摘要: 先上代码 新建一个Thread,代码如下: package com.thread.test;public class MyThread extends Thread { private String name; public MyThread(String name) { this.name = name; } @Override public void r ...

先上代码

新建一个Thread,代码如下:

  1. package com.thread.test;
  2. public class MyThread extends Thread {
  3. private String name;
  4. public MyThread(String name) {
  5. this.name = name;
  6. }
  7. @Override
  8. public void run() {
  9. for (int i = 0; i < 100; i++) {
  10. System.out.println(name+"["+i+"]");
  11. }
  12. super.run();
  13. }
  14. }
复制代码

之后新建测试类,代码如下:

  1. package com.thread.test;
  2. /*
  3. * 0-50执行的是主线程,50-100执行的是A线程,并且将A线程完全执行完后才继续执行主线程
  4. */
  5. public class ThreadDemo{
  6. public static void main(String[] args) {
  7. MyThread t = new MyThread("A");
  8. t.start();
  9. for (int i = 0; i < 100; i++) {
  10. if (i>50) {
  11. try {
  12. t.join();
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. System.out.println("主线程"+"["+i+"]");
  18. }
  19. }
  20. }
复制代码

下面是Java Platform SE8 API中对Thread中Join方法的解释:

  1. public final void join(long millis)
  2. throws InterruptedExceptionWaits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
  3. This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
  4. Parameters:
  5. millis - the time to wait in milliseconds
  6. Throws:
  7. IllegalArgumentException - if the value of millis is negative
  8. InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
复制代码

先上代码

新建一个Thread,代码如下:

  1. package com.thread.test;
  2. public class MyThread extends Thread {
  3. private String name;
  4. public MyThread(String name) {
  5. this.name = name;
  6. }
  7. @Override
  8. public void run() {
  9. for (int i = 0; i < 100; i++) {
  10. System.out.println(name+"["+i+"]");
  11. }
  12. super.run();
  13. }
  14. }
复制代码

之后新建测试类,代码如下:

  1. package com.thread.test;
  2. /*
  3. * 0-50执行的是主线程,50-100执行的是A线程,并且将A线程完全执行完后才继续执行主线程
  4. */
  5. public class ThreadDemo{
  6. public static void main(String[] args) {
  7. MyThread t = new MyThread("A");
  8. t.start();
  9. for (int i = 0; i < 100; i++) {
  10. if (i>50) {
  11. try {
  12. t.join();
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. System.out.println("主线程"+"["+i+"]");
  18. }
  19. }
  20. }
复制代码

下面是Java Platform SE8 API中对Thread中Join方法的解释:

  1. public final void join(long millis)
  2. throws InterruptedExceptionWaits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
  3. This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
  4. Parameters:
  5. millis - the time to wait in milliseconds
  6. Throws:
  7. IllegalArgumentException - if the value of millis is negative
  8. InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
复制代码

我自己的理解就是会强行进入使用join方法的线程,其他线程等待该线程完全执行完后才会进来。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-4 02:20

Copyright 2015-2025 djqfx

返回顶部