Java多线程概念和join()方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18479771/
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
Java Multithreading concept and join() method
提问by user2696258
I'm confused in join()
method used in Threads in Java. In the following code:
我对join()
Java 线程中使用的方法感到困惑。在以下代码中:
// 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
In the above code :
在上面的代码中:
I'm not able to understand the flow of execution of the program, And when
ob1
is created then the constructor is called wheret.start()
is written but stillrun()
method is not executed rathermain()
method continues execution. So why is this happening?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??
我无法理解程序的执行流程,并且在
ob1
创建时调用构造函数,t.start()
但仍然run()
没有执行main()
方法,而是继续执行方法。那么为什么会发生这种情况呢?join()
方法用于等待直到调用它的线程不终止,但在输出中我们看到线程的替代输出为什么?
And if the use of join
is this then what is the use of synchronized
??
如果使用的join
是 this 那么有什么用synchronized
?
I know I'm missing a basic concept here, but I'm not able to figure it out so please help.
我知道我在这里遗漏了一个基本概念,但我无法弄清楚,所以请帮忙。
采纳答案by Malwaregeek
You must understand , threads scheduling is controlled by thread scheduler.So, you cannot guarantee the order of execution of threads under normal circumstances.
你必须明白,线程调度是由线程调度器控制的,所以在正常情况下不能保证线程的执行顺序。
However, you can use join()
to wait for a thread to complete its work.
但是,您可以使用join()
等待线程完成其工作。
For example, in your case
例如,在你的情况下
ob1.t.join();
This statement will not return until thread t
has finished running.
直到线程t
完成运行,此语句才会返回。
Try this,
尝试这个,
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();
}
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.
在上面的例子中,你的主线程正在执行。当它遇到第 15 行时,线程 t 在线程调度程序中可用。只要主线程到达第 16 行,它就会等待线程t
完成。
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).
请注意,t.join
对 threadt
或 to thread没有做任何事情t1
。它只影响调用它的main()
线程(即线程)。
Edited:
编辑:
t.join();
needs to be inside the try
block because it throws
the InterruptedException
exception, otherwise you will get an error at compile time. So, it should be:
t.join();
需要在try
块内,因为它throws
是InterruptedException
异常,否则在编译时会出错。所以,应该是:
try{
t.join();
}catch(InterruptedException e){
// ...
}
回答by Vimal Bera
First of all, when you create ob1
then constructor is called and it starts execution. At that time t.start()
also runs in separate thread. Remember when a new thread is created, it runs parallely to main thread. And thats why main start execution again with next statement.
首先,当你创建ob1
然后构造函数被调用并开始执行。当时t.start()
也在单独的线程中运行。请记住,当创建新线程时,它与主线程并行运行。这就是为什么 main 用 next 语句再次开始执行。
And Join()
statement is used to prevent the child thread from becoming orphan.Means if you did'nt call join()
in your main class, then main thread will exit after its execution and child thread will be still there executing the statements. Join()
will wait until all child thread complete its execution and then only main method will exit.
andJoin()
语句用于防止子线程成为孤儿。意味着如果你没有join()
在你的主类中调用,那么主线程将在执行后退出,子线程将仍然在那里执行语句。Join()
将等待所有子线程完成其执行,然后只有 main 方法将退出。
Go through thisarticle, helps a lot.
通读这篇文章,很有帮助。
回答by MadProgrammer
First rule of threading - "Threading is fun"...
线程的第一条规则——“线程很有趣”...
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 stillrun()
method is not executed rathermain()
method continues execution. So why is this happening?
我无法理解程序的执行流程,当创建 ob1 时,构造函数被调用,
t.start()
但仍然run()
没有执行main()
方法,而是方法继续执行。那么为什么会发生这种情况呢?
This is exactly what should happen. When you call Thread#start
, the thread is created and schedule for execution, it might happen immediately (or close enough to it), it might not. It comes down to the thread scheduler.
这正是应该发生的事情。当您调用 时Thread#start
,线程被创建并安排执行,它可能会立即发生(或足够接近它),也可能不会。它归结为线程调度程序。
This comes down to how the thread execution is scheduled and what else is going on in the system. Typically, each thread will be given a small amount of time to execute before it is put back to "sleep" and another thread is allowed to execute (obviously in multiple processor environments, more than one thread can be running at time, but let's try and keep it simple ;))
这归结为线程执行的调度方式以及系统中正在发生的其他事情。通常,每个线程在回到“睡眠”状态之前都会有一小段时间来执行,并允许另一个线程执行(显然在多处理器环境中,可以同时运行多个线程,但让我们尝试并保持简单;))
Threads may also yield
execution, allow other threads in the system to have chance to execute.
线程也可以yield
执行,让系统中的其他线程有机会执行。
You could try
你可以试试
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
// Yield here
Thread.yield();
}
And it might make a difference to the way the threads run...equally, you could sleep
for a small period of time, but this could cause your thread to be overlooked for execution for a period of cycles (sometimes you want this, sometimes you don't)...
它可能会对线程的运行方式产生影响......同样,您可以sleep
在一小段时间内,但这可能会导致您的线程在执行一段时间内被忽略(有时您想要这个,有时您别)...
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??
join()
方法用于等待直到调用它的线程不终止,但在输出中我们看到线程的替代输出为什么?
The way you've stated the question is wrong...join
will wait for the Thread
it is called on to die before returning. For example, if you depending on the result of a Thread
, you could use join
to know when the Thread
has ended before trying to retrieve it's result.
你陈述问题的方式是错误的......join
会等待Thread
它被召唤死后再回来。例如,如果您依赖于 a 的结果Thread
,则可以在尝试检索其结果之前使用它join
来知道 何时Thread
结束。
Equally, you could poll the thread, but this will eat CPU cycles that could be better used by the Thread
instead...
同样,您可以轮询线程,但这会占用 CPU 周期,而这些 CPU 周期本可以由Thread
...
回答by codingjourney
The JVM and the underlying OS have considerable freedom when scheduling things. The fact that you get all the way to "Waiting for threads to finish" before you see the output from individual threads may simply mean that thread start-up takes a bit longer (i.e. it takes some time between the moment when a thread becomes "alive" and when the run()method actually starts executing). You could conceivably see thread output sooner but it's not guaranteed either way.
JVM 和底层操作系统在调度事物时具有相当大的自由度。在您看到单个线程的输出之前,您一直到“等待线程完成”这一事实可能只是意味着线程启动需要更长的时间(即在线程变为“活着”以及run()方法实际开始执行时)。可以想象,您可以更快地看到线程输出,但无论如何都不能保证。
As for join(), it only guarantees that whatever is after it will only happen once the thread you are joining is done. So when you have three join()calls in a row it doesn't mean the threads should end in a particular order. It simply means that you will wait for ob1first. Once ob1finishes, ob2and ob3may be still running or they may already be finished. If they are finished, your other join()calls will return immediately.
至于join(),它只保证它之后的任何内容只会在您加入的线程完成后发生。因此,当您连续进行三个join()调用时,并不意味着线程应该以特定顺序结束。这只是意味着您将首先等待ob1。一旦ob1完成,ob2和ob3可能仍在运行,或者它们可能已经完成。如果它们完成,您的其他join()调用将立即返回。
synchronizedis used specifically when multiple threads access the same object and make changes to it. A synchronized block is guaranteed never to be executed by two threads simultaneously - i.e. the thread that executes it has the synchronized object all to itself.
当多个线程访问同一个对象并对其进行更改时,专门使用同步。一个同步块保证永远不会被两个线程同时执行——即执行它的线程拥有全部的同步对象。
回答by Prabhaker A
when ob1 is created then the constructor is called where "t.start()" is written but still run() method is not executed rather main() method is executed further. So why is this happening?
当 ob1 被创建时,构造函数被调用,其中“t.start()”被写入,但仍然没有执行 run() 方法,而是进一步执行 main() 方法。那么为什么会发生这种情况呢?
here your threads and main thread has equal priority.Execution of equal priority thread totally depends on the Thread schedular
.You can't expect which to execute first.
这里你的线程和主线程具有相同的优先级。同等优先级线程的执行完全取决于 。Thread schedular
你不能指望先执行哪个。
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??
join() 方法用于等待直到调用它的线程不终止,但在输出中我们看到线程的替代输出为什么?
Here your calling below statements from main thread.
在这里,您从主线程调用以下语句。
ob1.t.join();
ob2.t.join();
ob3.t.join();
So main thread waits for ob1.t
,ob2.t
,ob3.t
threads to die(look into Thread#join doc).So all three threads executes successfully and main thread completes after that
所以主线程等待ob1.t
, ob2.t
,ob3.t
线程死亡(查看Thread#join doc)。所以所有三个线程都成功执行,然后主线程完成
回答by Prabhaker A
My Comments:
我的评论:
When I see the output, the output is mixed with One, Two, Three which are the thread names and they run simultaneously. I am not sure when you say thread is not running by main method.
当我看到输出时,输出与线程名称一、二、三混合在一起,它们同时运行。我不确定你什么时候说线程没有通过 main 方法运行。
Not sure if I understood your question or not. But I m putting my answer what I could understand, hope it can help you.
不知道我是否理解你的问题。但我把我的答案放在我能理解的地方,希望它可以帮助你。
1) Then you created the object, it called the constructor, in construct it has start method which started the thread and executed the contents written inside run() method.
1) 然后你创建了对象,它调用了构造函数,在构造它有 start 方法,它启动线程并执行 run() 方法中写入的内容。
So as you created 3 objects (3 threads - one, two, three), all 3 threads started executing simultaneously.
因此,当您创建 3 个对象(3 个线程 - 一、二、三)时,所有 3 个线程开始同时执行。
2) Join and Synchronization They are 2 different things, Synchronization is when there are multiple threads sharing a common resource and one thread should use that resource at a time. E.g. Threads such as DepositThread, WithdrawThread etc. do share a common object as BankObject. So while DepositThread is running, the WithdrawThread will wait if they are synchronized. wait(), notify(), notifyAll() are used for inter-thread communication. Plz google to know more.
2)加入和同步它们是两件不同的事情,同步是当有多个线程共享一个公共资源并且一个线程应该一次使用该资源时。例如,诸如DepositThread、WithdrawThread 等线程确实共享一个公共对象作为BankObject。因此,当 DepositThread 运行时,WithdrawThread 将等待它们是否同步。wait()、notify()、notifyAll() 用于线程间通信。请谷歌谷歌了解更多。
about Join(), it is when multiple threads are running, but you join. e.g. if there are two thread t1 and t2 and in multi-thread env they run, the output would be: t1-0 t2-0 t1-1 t2-1 t1-2 t2-2
关于Join(),它是当多个线程正在运行时,但您加入了。例如,如果有两个线程 t1 和 t2 并且在它们运行的多线程环境中,输出将是: t1-0 t2-0 t1-1 t2-1 t1-2 t2-2
and we use t1.join(), it would be: t1-0 t1-1 t1-2 t2-0 t2-1 t2-2
我们使用 t1.join(),它将是: t1-0 t1-1 t1-2 t2-0 t2-1 t2-2
This is used in realtime when sometimes you don't mix up the thread in certain conditions and one depends another to be completed (not in shared resource), so you can call the join() method.
当有时您在某些条件下不混淆线程并且一个依赖另一个要完成(不在共享资源中)时,这是实时使用的,因此您可以调用 join() 方法。
回答by rmishra
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?
我无法理解程序的执行流程,当创建 ob1 时,会在编写 t.start() 的地方调用构造函数,但仍然不执行 run() 方法,而是继续执行 main() 方法。那么为什么会发生这种情况呢?
This depends on Thread Scheduler as main shares the same priority order. Calling start() doesn't mean run() will be called immediately, it depends on thread scheduler when it chooses to run your thread.
这取决于线程调度器,因为 main 共享相同的优先级顺序。调用 start() 并不意味着会立即调用 run(),它取决于线程调度程序何时选择运行您的线程。
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??
join() 方法用于等待直到调用它的线程不终止,但在输出中我们看到线程的替代输出为什么?
This is because of the Thread.sleep(1000) in your code. Remove that line and you will see ob1 finishes before ob2 which in turn finishes before ob3 (as expected with join()). Having said that it all depends on when ob1 ob2 and ob3 started. Calling sleep will pause thread execution for >= 1 second (in your code), giving scheduler a chance to call other threads waiting (same priority).
这是因为代码中的 Thread.sleep(1000) 。删除该行,您将看到 ob1 在 ob2 之前完成,而 ob2 又在 ob3 之前完成(如使用 join() 所预期的那样)。话虽如此,这一切都取决于 ob1 ob2 和 ob3 何时开始。调用 sleep 将暂停线程执行 >= 1 秒(在您的代码中),让调度程序有机会调用其他线程等待(相同优先级)。
回答by Rajesh Dixit
join() is a instance method of java.lang.Thread class which we can use join() method to ensure all threads that started from main must end in order in which they started and also main should end in last. In other words waits for this thread to die.
join() 是 java.lang.Thread 类的一个实例方法,我们可以使用 join() 方法来确保所有从 main 开始的线程必须按照它们开始的顺序结束,并且 main 应该最后结束。换句话说,等待这个线程死亡。
Exception:join() method throws InterruptedException.
异常:join() 方法抛出 InterruptedException。
Thread state:When join() method is called on thread it goes from running to waiting state. And wait for thread to die.
线程状态:当线程上调用 join() 方法时,它从运行状态变为等待状态。并等待线程死亡。
synchronized block:Thread need not to acquire object lock before calling join() method i.e. join() method can be called from outside synchronized block.
同步块:线程在调用 join() 方法之前不需要获取对象锁,即可以从同步块外部调用 join() 方法。
Waiting time:join(): Waits for this thread to die.
等待时间:join():等待该线程死亡。
public final void join() throws InterruptedException;
This method internally calls join(0). And timeout of 0 means to wait forever;
此方法在内部调用 join(0)。而 timeout 为 0 表示永远等待;
join(long millis)– synchronized method Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
join(long millis)– 同步方法最多等待 ms 毫秒让该线程死亡。超时为 0 意味着永远等待。
public final synchronized void join(long millis)
throws InterruptedException;
public final synchronized void join(long millis, int nanos)
throws InterruptedException;
Example of join method
连接方法示例
class MyThread implements Runnable {
public void run() {
String threadName = Thread.currentThread().getName();
Printer.print("run() method of "+threadName);
for(int i=0;i<4;i++){
Printer.print("i="+i+" ,Thread="+threadName);
}
}
}
public class TestJoin {
public static void main(String...args) throws InterruptedException {
Printer.print("start main()...");
MyThread runnable = new MyThread();
Thread thread1=new Thread(runnable);
Thread thread2=new Thread(runnable);
thread1.start();
thread1.join();
thread2.start();
thread2.join();
Printer.print("end main()");
}
}
class Printer {
public static void print(String str) {
System.out.println(str);
}
}
Output:
start main()...
run() method of Thread-0
i=0 ,Thread=Thread-0
i=1 ,Thread=Thread-0
i=2 ,Thread=Thread-0
i=3 ,Thread=Thread-0
run() method of Thread-1
i=0 ,Thread=Thread-1
i=1 ,Thread=Thread-1
i=2 ,Thread=Thread-1
i=3 ,Thread=Thread-1
end main()
Note: calling thread1.join() made main thread to wait until Thread-1 dies.
注意:调用 thread1.join() 会使主线程等待直到 Thread-1 死亡。
Let's check a program to use join(long millis)
让我们检查一个程序来使用 join(long millis)
First, join(1000) will be called on Thread-1, but once 1000 millisec are up, main thread can resume and start thread2 (main thread won't wait for Thread-1 to die).
首先,将在线程 1 上调用 join(1000),但是一旦 1000 毫秒到了,主线程可以恢复并启动线程 2(主线程不会等待线程 1 死亡)。
class MyThread implements Runnable {
public void run() {
String threadName = Thread.currentThread().getName();
Printer.print("run() method of "+threadName);
for(int i=0;i<4;i++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Printer.print("i="+i+" ,Thread="+threadName);
}
}
}
public class TestJoin {
public static void main(String...args) throws InterruptedException {
Printer.print("start main()...");
MyThread runnable = new MyThread();
Thread thread1=new Thread(runnable);
Thread thread2=new Thread(runnable);
thread1.start();
// once 1000 millisec are up,
// main thread can resume and start thread2.
thread1.join(1000);
thread2.start();
thread2.join();
Printer.print("end main()");
}
}
class Printer {
public static void print(String str) {
System.out.println(str);
}
}
Output:
start main()...
run() method of Thread-0
i=0 ,Thread=Thread-0
run() method of Thread-1
i=1 ,Thread=Thread-0
i=2 ,Thread=Thread-0
i=0 ,Thread=Thread-1
i=1 ,Thread=Thread-1
i=3 ,Thread=Thread-0
i=2 ,Thread=Thread-1
i=3 ,Thread=Thread-1
end main()
For more information see my blog:
更多信息请看我的博客:
http://javaexplorer03.blogspot.in/2016/05/join-method-in-java.html
http://javaexplorer03.blogspot.in/2016/05/join-method-in-java.html
回答by Vijay Tyagi
No words just running code
没有文字只是运行代码
// Thread class
public class MyThread extends Thread {
String result = null;
public MyThread(String name) {
super(name);
}
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("Hello from " + this.getName());
}
result = "Bye from " + this.getName();
}
}
Main Class
主类
public class JoinRND {
public static void main(String[] args) {
System.out.println("Show time");
// Creating threads
MyThread m1 = new MyThread("Thread M1");
MyThread m2 = new MyThread("Thread M2");
MyThread m3 = new MyThread("Thread M3");
// Starting out Threads
m1.start();
m2.start();
m3.start();
// Just checking current value of thread class variable
System.out.println("M1 before: " + m1.result);
System.out.println("M2 before: " + m2.result);
System.out.println("M3 before: " + m3.result);
// After starting all threads main is performing its own logic in
// parallel to other threads
for (int i = 0; i < 1000; i++) {
System.out.println("Hello from Main");
}
try {
System.out
.println("Main is waiting for other threads to get there task completed");
m1.join();
m2.join();
m3.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("M1 after" + m1.result);
System.out.println("M2 after" + m2.result);
System.out.println("M3 after" + m3.result);
System.out.println("Show over");
}
}
回答by Saathvik
Thread scheduler is responsible for scheduling of threads. So every time you run the program, there is no guarantee to the order of execution of threads. Suppose you have a thread object named threadOne and if join() is called on threadOne like this:
线程调度器负责线程的调度。所以每次运行程序时,都无法保证线程的执行顺序。假设您有一个名为 threadOne 的线程对象,并且如果像这样在 threadOne 上调用 join() :
threadOne.join()
threadOne.join()
then all currently executing threads will be paused until thread1 has finished its execution or terminates.
然后所有当前正在执行的线程将暂停,直到线程 1 完成其执行或终止。
Consider the following piece of code:
考虑以下代码:
class RunnableSample implements Runnable {
private Thread t;
private String threadName;
public RunnableSample(String name) {
this.threadName = name;
}
public void run() {
try {
for(int i = 4; i >= 1; i--) {
System.out.println(Thread.currentThread().getName() + ", " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted");
}
}
public void start() {
if(t == null)
t = new Thread(this, threadName);
t.start();
try {
t.join();
} catch(Exception e) {
System.out.println(e);
}
}
}
public class RunnableDemo {
public static void main(String[] args) {
RunnableSample r1 = new RunnableSample("threadOne");
r1.start();
RunnableSample r2 = new RunnableSample("threadTwo");
r2.start();
RunnableSample r3 = new RunnableSample("threadThree");
r3.start();
}
}
The output of the above program will be:
上述程序的输出将是:
threadOne, 4
threadOne, 3
threadOne, 2
threadOne, 1
threadTwo, 4
threadTwo, 3
threadTwo, 2
threadTwo, 1
threadThree, 4
threadThree, 3
threadThree, 2
threadThree, 1
Since join() is called on threadOne first, threadTwo and threadThree will be paused until threadOne terminates. (NOTE that threadOne, threadTwo and ThreadThree all have started). Now the threads are executing in a specific order. If join() is not called on a thread in our example, then there will be no order of execution of threads.
由于 join() 首先在 threadOne 上调用,因此 threadTwo 和 threadThree 将暂停,直到 threadOne 终止。(注意 threadOne、threadTwo 和 ThreadThree 都已启动)。现在线程按特定顺序执行。如果在我们的示例中未在线程上调用 join(),则线程的执行顺序将不存在。
public void start() {
if(t == null)
t = new Thread(this, threadName);
t.start();
}
Its output will be:
它的输出将是:
threadOne, 4
threadThree, 4
threadTwo, 4
threadTwo, 3
threadThree, 3
threadOne, 3
threadOne, 2
threadThree, 2
threadTwo, 2
threadOne, 1
threadThree, 1
threadTwo, 1
Coming to synchronization, which is useful if you want to control the access of multiple threads on any shared resource. If you want to restrict only one thread to access shared resources then synchronization is the best way to do it.
来到同步,如果你想控制多个线程对任何共享资源的访问,这很有用。如果您只想限制一个线程访问共享资源,那么同步是最好的方法。