本文介绍了Java多线程概念和join()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java中Threads中使用的 join()方法感到困惑。在以下代码中:

I'm confused in join() method used in Threads in Java. In the following code:

// 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.");
    }
}

此程序的示例输出如下所示:

Sample output from this program is shown here:

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

在上面的代码中:


  1. 我不是能够理解程序的执行流程,并且当 ob1 被创建时,构造函数被调用,其中 t.start()已写入,但仍然 run()方法未执行而是 main()方法继续执行。那么为什么会发生这种情况?

  1. I'm not able to understand the flow of execution of the program, And when ob1 is created then the constructor is called where t.start() is written but still run() method is not executed rather main() method continues execution. So why is this happening?

join()方法用于等待线程所在的它被称为不终止,但在输出中我们看到线程的替代输出为什么??

join() method is used to wait until the thread on which it is called does not terminates, but here in output we see alternate outputs of the thread why??

如果使用加入这是什么用于 synchronized ??

And if the use of join is this then what is the use of synchronized??

我知道我在这里错过了一个基本概念,但我无法弄清楚所以请帮忙。

I know I'm missing a basic concept here, but I'm not able to figure it out so please help.

推荐答案

你必须明白,线程调度是由线程调度器控制的。所以,你不能保证正常情况下线程的执行顺序。

You must understand , threads scheduling is controlled by thread scheduler.So, you cannot guarantee the order of execution of threads under normal circumstances.

但是,您可以使用 join()等待线程完成其工作。

However, you can use join() to wait for a thread to complete its work.

例如,在您的情况下

ob1.t.join();

在线程 t 之前,此语句不会返回已完成运行。

This statement will not return until thread t has finished running.

试试这个,

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();
}

在上面的例子中,你的主线程正在执行。当遇到第15行时,线程调度程序可以使用线程t。一旦主线程进入第16行,它将等待线程 t 完成。

In the above example, your main thread is executing. When it encounters line 15, thread t is available at thread scheduler. As soon as main thread comes to line 16, it will wait for thread t to finish.

注意 t.join 没有做任何事情来线程 t 或线程 t1 。它只影响调用它的线程(即 main()线程)。

NOTE that t.join did not do anything to thread t or to thread t1. It only affected the thread that called it (i.e., the main() thread).

这篇关于Java多线程概念和join()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 02:54