有没有办法让主线程处于睡眠模式,直到我的所有线程在java中完成执行?

时间:2013-05-07 11:31:15

标签: java multithreading

我有两个名为Player1的线程类和玩家2.第一个玩家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 

在我的输出中,游戏摘要在玩家实际开始游戏之前由主线程打印(Amir得分为:0,Hossein得分为:0)。我可以使用Thread.Sleep(3000)然后回答是但是,这不是好方法,我想......

你能不能请教我...

期待您的宝贵回复......

3 个答案:

答案 0 :(得分:5)

只需使用Thread.join()方法:

Amir.start();  
Hossein.start();

Amir.join();
Hossein.join();

答案 1 :(得分:2)

您可以使用CountdownLatch。在创建2 CountdownLatch(2)个类之前创建Thread,并将该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);
    }  

}  

答案 2 :(得分:1)

Thread.join允许一个线程在继续执行之前等待另一个线程完成。这可能对你有帮助。