![](/img/trans.png)
[英]Calling sleep on one thread is sleeping all my threads (Java)
[英]Is there a way to keep main thread in sleep mode until all my threads finish their execution in java?
我有兩個線程類,分別命名為Player1和player2。第一個玩家1將開始得分運行。現在玩家2處於等待狀態,一旦玩家1完成他的游戲,那么玩家2將有機會參加比賽。
兩名球員完成比賽后,我就必須總結兩名球員的得分。
最重要的是,我完成了所有工作。但是我無法讓主線程(打印球員摘要)等到所有球員都完成之后。
這是我的Java程序...
class Player1 extends Thread
{
private Object o;
int total=0;
Player1 (Object o)
{
this.o = o;
}
public void run()
{
System.out.println(Thread.currentThread().getName()+
" is running now...");
for(int i=0;i<10;i++)
{
++total ;
}
System.out.println(this.getName()+" is "+total+
" Run! and finished ");
synchronized (o)
{
o.notify();
}
}
}
class Player2 extends Thread
{
private Object o;
int total=0;
Player2 (Object o)
{
this.o = o;
}
public void run()
{
try
{
synchronized (o)
{
o.wait();
}
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+
" is running now");
for(int i=0;i<15;i++)
{
++total ;
}
System.out.println(this.getName()+" is "+total+
" Run! and finished ");
}
}
public class MultiThreading
{
public static void main(String[] args)
{
Object lock= new Object();
Player1 Amir=new Player1(lock);
Amir.setName("Amir");
Player2 Hossein=new Player2(lock);
Hossein.setName("Hossein");
Amir.start();
Hossein.start();
System.out.println("Amir Score is :"+Amir.total);
System.out.println("Hossein Score is :"+Hossein.total);
}
}
輸出為:
Amir Score is :0
Hossein Score is :0
Amir is running now...
Amir is 10 Run! and finished
Hossein is running now
Hossein is 15 Run! and finished
在我的輸出中,游戲概要是在玩家實際開始游戲之前由主線程打印的(阿米爾分數為:0,侯賽因分數為:0)。我可以使用Thread.Sleep(3000)來回答正確。我認為那不是好方法。
你能給我啟發一下嗎?
期待您的寶貴答復...
只需使用Thread.join()
方法即可:
Amir.start();
Hossein.start();
Amir.join();
Hossein.join();
您可以使用CountdownLatch
。 在創建2個Thread
類之前,創建一個CountdownLatch(2)
,並將該CountdownLatch
傳遞給兩個線程的構造函數。 然后,在主代碼中,調用latch.await()
。 這將阻塞並等待其他線程同時調用latch.countDown()
並且僅在發生這種情況后才繼續。 您只需要在完成工作后使每個線程調用latch.countDown()
,因此控制權將返回執行摘要的代碼。
class Player1 extends Thread
{
private Object o;
int total=0;
private latch:CountDownLatch;
Player1 (Object o, CountDownLatch latch)
{
this.o = o;
this.latch = latch;
}
public void run()
{
System.out.println(Thread.currentThread().getName()+ " is running now...");
for(int i=0;i<10;i++)
{
++total ;
}
System.out.println(this.getName()+" is "+total+ " Run! and finished ");
synchronized (o)
{
o.notify();
}
latch.countDown();
}
}
class Player2 extends Thread
{
private Object o;
int total=0;
private CountDownLatch latch;
Player2 (Object o, CountDownLatch latch)
{
this.o = o;
this.latch = latch;
}
public void run()
{
try
{
synchronized (o)
{
o.wait();
}
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+ " is running now");
for(int i=0;i<15;i++)
{
++total ;
}
System.out.println(this.getName()+" is "+total+ " Run! and finished ");
latch.countDown();
}
}
public class MultiThreading
{
public static void main(String[] args)
{
Object lock= new Object();
CountDownLatch latch = new CountDownLatch(2);
Player1 Amir=new Player1(lock, latch);
Amir.setName("Amir");
Player2 Hossein=new Player2(lock, latch);
Hossein.setName("Hossein");
Amir.start();
Hossein.start();
latch.await();
System.out.println("Amir Score is :"+Amir.total);
System.out.println("Hossein Score is :"+Hossein.total);
}
}
Thread.join允許一個線程在繼續執行之前等待另一個線程完成。 那可能對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.