[英]Thread2 waits Thread1 to complete to start Problem? Java
So I have a simple code that I want to print the value I 10 times with Thread1, after that 10 times of Thread2 and at the end, print the count ( it should be 20).所以我有一个简单的代码,我想用 Thread1 打印值 I 10 次,然后 Thread2 打印 10 次,最后打印计数(应该是 20)。 I am using the ".join()" but the result is executing random times of Thread1 and Thread2 and then the Sum is correct.
我正在使用“.join()”,但结果是执行 Thread1 和 Thread2 的随机时间,然后 Sum 是正确的。 How can is it possible to print first all the Thread's1 loop and then the Tread's2??
怎么可能先打印所有 Thread's1 循环然后打印 Tread's2?
class MyClass extends Thread {
public static synchronized void incount() {
SimpleThreads.count++;
}
public void run() {
for(int i=0; i<10; i++) {
incount();
System.out.println(Thread.currentThread().getId()+" value : " + i);
}
}
}
public class SimpleThreads {
static int count=0;
public static void main(String[] args) {
MyClass thread1 =new MyClass();
MyClass thread2 =new MyClass();
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" Sum : "+count);
}
}
The Result:结果:
11 value : 0
10 value : 1
11 value : 1
10 value : 2
11 value : 2
10 value : 3
11 value : 3
11 value : 4
11 value : 5
11 value : 6
11 value : 7
11 value : 8
11 value : 9
10 value : 4
10 value : 5
10 value : 6
10 value : 7
10 value : 8
10 value : 9
Sum : 20
You are starting Thread2
before calling the join()
on thread1
.在调用
thread1
上的join()
之前,您正在启动Thread2
。
That is why your both threads are basically running simultaneously and your join is not affecting the run()
of any other the 2 threads.这就是为什么您的两个线程基本上同时运行并且您的连接不会影响任何其他 2 个线程的
run()
。
Try to change your start and join calling code to something like this;尝试将您的开始和加入调用代码更改为这样的内容;
try{
thread1.start();
thread1.join();
thread2.start();
}
You shouldn't need to call join()
on thread2 in this case.在这种情况下,您不需要在 thread2 上调用
join()
。
If you want thread2
to start after thread1
terminates, then of-course you can simply wait for thread1
to terminate and then launch thread2
.如果您希望
thread2
在thread1
终止后启动,那么您当然可以简单地等待thread1
终止然后启动thread2
。 But then, what is the point of using threads?但是,使用线程有什么意义呢?
If you want to launch thread1
and thread2
at the same time and still have thread2
wait until thread1
terminates, you can use one of Java's many concurrency utilities , such as Semaphore如果您想同时启动
thread1
和thread2
并且仍然让thread2
等到thread1
终止,您可以使用 Java 的许多并发实用程序之一,例如Semaphore
The below code demonstrates the use of Semaphore
.下面的代码演示了
Semaphore
的使用。 As you can see, just as in the code in your question, both threads - thread1
and thread2
- are launched at the same time.正如您所看到的,就像您问题中的代码一样,线程
thread1
和thread2
2 都是同时启动的。 In the run()
method of class MyClass
, the code tries to acquire the semaphore.在 class
MyClass
的run()
方法中,代码尝试获取信号量。 Method acquire()
will block, ie it will not return, until it succeeds in acquiring the semaphore.方法
acquire()
将阻塞,即它不会返回,直到它成功获取信号量。 Hence the first thread that manages to acquire the semaphore will run, while the other thread will wait until the first thread releases the semaphore.因此,第一个设法获取信号量的线程将运行,而另一个线程将等待,直到第一个线程释放信号量。 Note that I create the semaphore with only one permit which means that only one thread can acquire the semaphore at any one time.
请注意,我只使用一个许可创建信号量,这意味着任何时候只有一个线程可以获取信号量。 If you change the
1
to a 2
in the call to the Semaphore
constructor, you will get exactly the same behavior as in your original code in your question, ie both threads will run simultaneously because both can immediately acquire the semaphore.如果在对
Semaphore
构造函数的调用中将1
更改为2
,您将获得与问题中原始代码完全相同的行为,即两个线程将同时运行,因为它们都可以立即获取信号量。
Note also that since I am using a semaphore, I don't need to call Thread.join()
at all in order to have one thread wait until the other completes, but since you want to print the "sum" in the "main" thread, the "main" thread needs to wait, but it only needs to wait for the second thread to terminate.另请注意,由于我使用的是信号量,因此我根本不需要调用
Thread.join()
来让一个线程等待另一个线程完成,但是因为您想在“main”中打印“sum” "线程,“主”线程需要等待,但它只需要等待第二个线程终止。
Here is the code:这是代码:
import java.util.concurrent.Semaphore;
class MyClass extends Thread {
private Semaphore semaphore;
public MyClass(Semaphore semaphore) {
this.semaphore = semaphore;
}
public static synchronized void incount() {
SimpleThreads.count++;
}
public void run() {
try {
semaphore.acquire();
for (int i = 0; i < 10; i++) {
incount();
System.out.println(Thread.currentThread().getId() + " value : " + i);
}
}
catch (InterruptedException xInterrupted) {
xInterrupted.printStackTrace();
}
finally {
semaphore.release();
}
}
}
public class SimpleThreads {
static int count = 0;
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(1);
MyClass thread1 = new MyClass(semaphore);
MyClass thread2 = new MyClass(semaphore);
thread1.start();
thread2.start();
try {
thread2.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" Sum : " + count);
}
}
And here is the output obtained when running the above code:而这里是运行上述代码时得到的output:
13 value : 0
13 value : 1
13 value : 2
13 value : 3
13 value : 4
13 value : 5
13 value : 6
13 value : 7
13 value : 8
13 value : 9
14 value : 0
14 value : 1
14 value : 2
14 value : 3
14 value : 4
14 value : 5
14 value : 6
14 value : 7
14 value : 8
14 value : 9
Sum : 20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.