Java多线程概念和join()方法

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)

在上面的代码中:

  1. 我无法理解程序的执行流程,并且在ob1创建时会调用构造函数, t.start()但是仍然run()会执行main()方法而不是方法继续执行.那为什么会这样呢?

  2. 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块内,因为它throwsInterruptedException异常,否则你将在编译时得到一个错误.所以,它应该是:

try{
    t.join();
}catch(InterruptedException e){
    // ...
}
Run Code Online (Sandbox Code Playgroud)

  • 我在网上搜索了一个简单的答案,它将以简单的方式解释这个概念!差不多一个小时.但这是我发现的唯一答案,以最简单的方式直截了当.@malwaregeek,谢谢你. (11认同)
  • 很好的解释 (2认同)

Vim*_*era 6

首先,在创建时ob1会调用构造函数并开始执行。当时t.start()还运行在单独的线程中。请记住,创建新线程时,它与主线程并行运行。这就是为什么main用next语句再次开始执行的原因。

Join()语句用于防止子线程成为孤立线程。意味着如果您没有join()在主类中调用,那么主线程将在其执行后退出,而子线程仍将在那里执行语句。Join()将等待,直到所有子线程完成其执行,然后只有main方法将退出。

通过走条,有很大帮助。