0

我有两个名为 Player1 和 player2 的线程类。第一个 player1 将开始得分运行。现在 player2 处于等待状态,一旦 player1 完成他的游戏,那么 player2 将有机会玩。

一旦两名球员都完成了,那么我必须总结两名球员的得分。

最重要的是,我完成了所有工作。但我无法让主线程(打印玩家摘要)等到所有玩家都完成。

这是我的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 Score 是:0,Hossein Score 是:0)。我可以使用 Thread.Sleep(3000) 那么答案是正确的。但是这不是好方法,我认为...

能不能请教一下这个...

期待您的宝贵回复...

4

3 回答 3

5

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

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

Amir.join();
Hossein.join();
于 2013-05-07T11:34:21.927 回答
2

你可以使用一个CountdownLatch. CountdownLatch(2)在创建 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);
    }  

}  
于 2013-05-07T11:36:44.863 回答
1

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

于 2013-05-07T11:34:06.230 回答