Java 为什么我们应该在线程中使用Join?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6594388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:03:54  来源:igfitidea点击:

Why we should use Join in threads?

javamultithreading

提问by BOSS

I have 2 threads T1 and T2 ,both have different jobs so usually we prefer to accomplish this task by thread Joins.

我有 2 个线程 T1 和 T2,它们都有不同的工作,所以通常我们更喜欢通过线程连接来完成这个任务。

But we can do this with out using join(). We can add T2 thread's code inside T1 thread. What difference does this make ?

但是我们可以不使用 join() 来做到这一点。我们可以在 T1 线程中添加 T2 线程的代码。这有什么区别?

采纳答案by BOSS

The main difference is when we join T2 thread with T1 ,the time T2 is executing the job can be utilised by T1 also ,that means they will do different job parllely.But this cann't happen when you include the T2 thread code inside T1 thread.

主要区别在于当我们将 T2 线程与 T1 连接时,T2 执行作业的时间也可以被 T1 使用,这意味着它们将并行执行不同的工作。但是当您在 T1 中包含 T2 线程代码时,这不会发生线。

回答by Leonard Brünings

If you call T1.join();from T2 it will wait for T1 to die (finish). It is a form of thread synchronization, but from what you describe you can simply fire of two thread and simply do not use join. If you use two threads then the work will be done in parallel, if you put the code only in one thread then the work will be done sequentially.

如果您T1.join();从 T2调用,它将等待 T1 死亡(完成)。它是线程同步的一种形式,但根据您的描述,您可以简单地触发两个线程,并且根本不使用连接。如果您使用两个线程,那么工作将并行完成,如果您只将代码放在一个线程中,那么工作将依次完成。

回答by Thomas

Joining a thread means that one waits for the other to end, so that you can safely access its result or continue after both have finished their jobs.

加入一个线程意味着一个线程等待另一个线程结束,这样您就可以安全地访问其结果或在两个线程完成工作后继续。

Example: if you start a new thread in the main thread and both do some work, you'd join the main thread on the newly created one, causing the main thread to wait for the second thread to finish. Thus you can do some work in parallel until you reach the join.

示例:如果您在主线程中启动一个新线程并且两者都做了一些工作,您将在新创建的线程上加入主线程,导致主线程等待第二个线程完成。因此,您可以并行执行一些工作,直到达到联接为止。

If you split a job into two parts which are executed by different threads you may get a performance improvement, if

如果您将作业拆分为由不同线程执行的两个部分,您可能会获得性能提升,如果

  • the threads can run independently, i.e. if they don't rely on each other's data, otherwise you'd have to synchronize which costs performance
  • the JVM is able to execute multiple threads in parallel, i.e. you have a hyperthreading/multicore machine and the JVM utilizes that
  • 线程可以独立运行,即如果它们不依赖彼此的数据,否则您必须进行同步,这会降低性能
  • JVM 能够并行执行多个线程,即您有一台超线程/多核机器,而 JVM 利用它

回答by Bohemian

You could use something like a java.util.concurrent.CountDownLatch, eg:

你可以使用类似 a 的东西java.util.concurrent.CountDownLatch,例如:

CountDownLatch doneSignal = new CountDownLatch(2);

and have each thread countDown()when they're done, so a main thread knows when both threads have completed.

countDown()在完成后让每个线程都完成,因此主线程知道两个线程何时完成。

回答by SJuan76

Two things.

两件事情。

Join is used only when one thread must wait for the open to finish (lets say thread A prepares a file and thread B cannot continue until the file is ready). There are instance where threads are independent and no join is needed (for example most of daemon threads).

仅当一个线程必须等待打开完成时才使用加入(假设线程 A 准备一个文件,而线程 B 在文件准备好之前无法继续)。有些情况下线程是独立的并且不需要连接(例如大多数守护线程)。

With threading you get several things: - mainly, independence in the order of execution. Lets say that you have a program that when you push a button does some heavy processing. If you do that processing in the main thread, you GUI will freeze until the task is finished. If you do the processing in another thread, then the GUI thread is "freed" and the GUI keeps working. - in some (most) of modern computers, creating several threads could allow the OS to use the different cores to serve different threads, improving performance.

使用线程,您可以获得以下几点: - 主要是执行顺序的独立性。假设您有一个程序,当您按下按钮时,它会进行一些繁重的处理。如果您在主线程中进行该处理,您的 GUI 将冻结,直到任务完成。如果您在另一个线程中进行处理,那么 GUI 线程将被“释放”并且 GUI 继续工作。- 在某些(大多数)现代计算机中,创建多个线程可以让操作系统使用不同的内核来为不同的线程提供服务,从而提高性能。

The drawback is bigger complexity, as you need information of other threads execution state.

缺点是更复杂,因为您需要其他线程执行状态的信息。

回答by user207421

usually we prefer to accomplish this task by thread Joins.

通常我们更喜欢通过线程Joins来完成这个任务。

No we don't. We accomplish this task by startingtwo threads. There is no obligation to use join()so there is no 'should' about it. If you want to pause the current thread while another thread completes, do so. If you don't, don't.

不,我们没有。我们通过启动两个线程来完成这个任务。没有使用的义务,join()所以没有关于它的“应该”。如果要在另一个线程完成时暂停当前线程,请执行此操作。如果你不这样做,不要。

回答by Deepak Bala

using Join also like we can add the T2 thread's code inside T1 thread

使用Join也像我们可以在T1线程中添加T2线程的代码

join() like the method name implies waits for the thread to die and joins it at the end of execution. You can add one thread's code inside another but that would destroy the purpose of using 2 separate threads to run your jobs concurrently. Placing one code after the other would run your statements in sequence. There is no concurrency.

join() 就像方法名称一样暗示等待线程死亡并在执行结束时加入它。您可以将一个线程的代码添加到另一个线程中,但这会破坏使用 2 个独立线程并发运行作业的目的。一个接一个地放置代码将按顺序运行您的语句。没有并发。

When in doubt, consult the javadocs - http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join%28%29

如有疑问,请查阅 javadocs - http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join%28%29

回答by Real Red.

If T1 and T2 do different tasks which do not depend on state changes caused by each other - you should not join them to reap advantages of parallel execution. In case there are state dependenices you should synchronize both threads using mechanisms like wait/notify or even .Join() depending on your use case.

如果 T1 和 T2 执行不依赖于彼此引起的状态更改的不同任务 - 您不应该加入它们以获得并行执行的优势。如果存在状态依赖,您应该根据您的用例使用等待/通知甚至 .Join() 等机制同步两个线程。

And as for, combining the run() methods of both threads, it's entirely left to you. I mean, you should understand why both threads are of different "types" (as they have different run() body) in the first place . It's a design aspect and not a performance aspect.

至于,结合两个线程的 run() 方法,完全留给你。我的意思是,您首先应该理解为什么两个线程具有不同的“类型”(因为它们具有不同的 run() 主体)。这是一个设计方面而不是性能方面。

回答by Alexander Razmakhnin

Here is the reason to use join: You use it when final result depends on result of two tasks which could run at the same time.

这是使用 join 的原因:当最终结果取决于可以同时运行的两个任务的结果时,您可以使用它。

Example1: After user clicks submit button the program has to call two external webservices to update their respective systems. It can be done at the same time that is why we would create a separate thread for one of webservices.

Example1:用户点击提交按钮后,程序必须调用两个外部网络服务来更新各自的系统。它可以同时完成,这就是为什么我们要为其中一个 web 服务创建一个单独的线程。

The user will sit before the screen and wait for a notification: Your submission is OK! The screen should say OK only after both threads finished.

用户将坐在屏幕前等待通知:您提交成功!只有在两个线程完成后,屏幕才会显示 OK。