2017-02-22 125 views
1

我想讓我的線程跟隨對方,就像它的一個種族,並希望線程等待對方。 像:等待對方的線程

DUCK is on his 1 lap 
PIGGY is on his 1 lap 
ELEFANT is on his 1 lap 
STAR is on his 1 lap 

DUCK is on his 2 lap 
PIGGY is on his 2 lap 
ELEFANT is on his 2 lap 
STAR is on his 2 lap 

等..

public class ExThread implements Runnable { 

    String name; 
    Random r = new Random(); 
    int count; 
    int sleepTimer; 

    public ExThread(String name) { 
     this.name = name; 
    } 

    @Override 
    public void run() { 
     try { 
      for(int i = 0; i < 10; i++) { 
       count++; 
       sleepTimer = r.nextInt(1000)+1; 
       Thread.sleep(sleepTimer); 
       System.out.println(
        name + " Starts after "+sleepTimer+" milliseconds break"); 
       System.out.println(
        name+" is on his "+count+" lap"); 

      } 
      System.out.println(name+" completes the race!!!"); 
     } 
     catch (Exception e){} 
    } 

public class ThreadsEx { 

    public static void main(String[] args) throws InterruptedException { 
     Thread t1 = new Thread(new ExThread("STAR")); 
     Thread t2 = new Thread(new ExThread("ELEFANT")); 
     Thread t3 = new Thread(new ExThread("PIGGY")); 
     Thread t4 = new Thread(new ExThread("DUCK")); 
     t1.start(); 
     t2.start(); 
     t3.start(); 
     t4.start(); 
    } 
} 
+2

你能請更具體一點。你有什麼問題?你目前的成果是什麼?線程必須以什麼方式「跟隨並等待」彼此? – GregaMohorko

+0

@CasperFlintrup你的意思是一個線程複雜化另一個線程將啓動?我是對的 ? –

+0

看看[Phaser](https://dzone.com/articles/java-7-understanding-phaser)。它應該幫助你。我想稍後寫一個例子 – rvit34

回答

0

你可以使用join

for(i = 0; i < threads.length; i++){ 
    threads[i].join(); 
} 

加入將阻止當前線程(主線程在你的情況下,我假設)。當for循環結束時,所有的線程都將完成其工作。

在你的情況,你可以讓每個線程做的,而不是10

for(j = 0; j < numberOfLaps; j++){ 
    for(i = 0; i < threads.length; i++){ 
     threads[i].join(); 
    } 
} 
0

一個單圈,您可以使用https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CyclicBarrier.html這一點。

代碼是這樣

import java.util.Random; 
import java.util.concurrent.CyclicBarrier; 

public class ExThread implements Runnable { 

String name; 
Random r = new Random(); 
int count; 
int sleepTimer; 
CyclicBarrier barrier; 

public ExThread(String name, CyclicBarrier barrier) { 
    this.name = name; 
    this.barrier = barrier; 
} 

public static void main(String[] args) throws InterruptedException { 
    CyclicBarrier barrier = new CyclicBarrier(4); 

    Thread t1 = new Thread(new ExThread("STAR", barrier)); 
    Thread t2 = new Thread(new ExThread("ELEFANT", barrier)); 
    Thread t3 = new Thread(new ExThread("PIGGY", barrier)); 
    Thread t4 = new Thread(new ExThread("DUCK", barrier)); 
    t1.start(); 
    t2.start(); 
    t3.start(); 
    t4.start(); 
} 

@Override 
public void run() { 
    try { 
     for (int i = 0; i < 10; i++) { 
      count++; 
      sleepTimer = r.nextInt(1000) + 1; 
      Thread.sleep(sleepTimer); 
      System.out.println(
        name + " Starts after " + sleepTimer + " milliseconds break"); 
      System.out.println(
        name + " is on his " + count + " lap"); 
      barrier.await(); 
     } 

     System.out.println(name + " completes the race!!!"); 
    } catch (Exception e) { 
    } 
} 

}

2

首先,這是非常不好的有,特別是如果他們是彼此(毫秒內產生的Random多個實例,因爲它們與隨機狀態播種您的計算機在時間尺度上的變化不大)

要使線程彼此等待,請使用CyclicBarrierjavadoc就是一個很好的例子。不過,我不認爲這就是你之後的事情,因爲這會干擾比賽本身。

你可能想要的是某種裁判線程,它會定期打印其他線程迄今運行的圈數。

1

我認爲規則是:

  • 所有線程都必須在同一圈
  • 線程可以完成他們的腿上以任何順序,但所有線程已完成一圈之前不能進展到下一圈。上述

這些規則需要某種形式的線程之間的同步和有幾個同步選項可用在Java中

初始建議將使用所有線程都具有訪問權限的值以便他們能夠傳達他們的進展,並知道他們是否可以進入下一圈,並跟蹤彼此的進展。

的一般常識你實現

  • 的System.out。println不保證消息在多線程應用程序中打印出來的順序。它們可以以不同的順序到達控制檯,而不是在代碼中調用該方法。
  • Thread.sleep不保證精確的睡眠時間。
0

你必須使用java.util.concurrent中,像這樣:

import java.util.Random; 
import java.util.concurrent.CountDownLatch; 
import java.util.concurrent.CyclicBarrier; 

public class RendezVous extends Thread { 

    private final CountDownLatch _start; 
    private final CyclicBarrier _rdv; 
    private final CountDownLatch _stop; 

    public RendezVous(
     String   name, 
     CountDownLatch start, 
     CyclicBarrier rdv, 
     CountDownLatch stop ) 
    { 
     super(name); 
     _start = start; 
     _rdv = rdv; 
     _stop = stop; 
     start(); 
    } 

    @Override 
    public void run() { 
     final Random rnd = new Random(System.currentTimeMillis()); 
     try { 
     System.out.println(getName() + " is started"); 
     _start.countDown(); 
     System.out.println(getName() + " waits for others"); 
     _start.await(); 
     System.out.println(getName() + " is running"); 
     for(int count = 0, i = 0; i < 10; ++i, ++count) { 
      final long sleepTimer = rnd.nextInt(1000) + 1; 
      sleep(sleepTimer); 
      System.out.println(getName() +" is on his " + count + " lap"); 
      _rdv.await(); 
     } 
     _stop.countDown(); 
     _stop.await(); 
     System.out.println(getName() + " completes the race"); 
     } 
     catch(final Exception e) { 
     e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     final CountDownLatch start = new CountDownLatch(4); 
     final CyclicBarrier rdv = new CyclicBarrier(4); 
     final CountDownLatch stop = new CountDownLatch(4); 
     new RendezVous("STAR" , start, rdv, stop); 
     new RendezVous("ELEFANT", start, rdv, stop); 
     new RendezVous("PIGGY" , start, rdv, stop); 
     new RendezVous("DUCK" , start, rdv, stop); 
    } 
} 

執行日誌:

DUCK is started 
STAR is started 
DUCK waits for others 
PIGGY is started 
ELEFANT is started 
PIGGY waits for others 
STAR waits for others 
PIGGY is running 
STAR is running 
ELEFANT waits for others 
DUCK is running 
ELEFANT is running 
STAR is on his 0 lap 
PIGGY is on his 0 lap 
DUCK is on his 0 lap 
ELEFANT is on his 0 lap 
DUCK is on his 1 lap 
STAR is on his 1 lap 
ELEFANT is on his 1 lap 
PIGGY is on his 1 lap 
STAR is on his 2 lap 
ELEFANT is on his 2 lap 
PIGGY is on his 2 lap 
DUCK is on his 2 lap 
STAR is on his 3 lap 
PIGGY is on his 3 lap 
ELEFANT is on his 3 lap 
DUCK is on his 3 lap 
DUCK is on his 4 lap 
PIGGY is on his 4 lap 
ELEFANT is on his 4 lap 
STAR is on his 4 lap 
STAR is on his 5 lap 
PIGGY is on his 5 lap 
ELEFANT is on his 5 lap 
DUCK is on his 5 lap 
STAR is on his 6 lap 
DUCK is on his 6 lap 
PIGGY is on his 6 lap 
ELEFANT is on his 6 lap 
PIGGY is on his 7 lap 
ELEFANT is on his 7 lap 
STAR is on his 7 lap 
DUCK is on his 7 lap 
ELEFANT is on his 8 lap 
DUCK is on his 8 lap 
PIGGY is on his 8 lap 
STAR is on his 8 lap 
STAR is on his 9 lap 
ELEFANT is on his 9 lap 
DUCK is on his 9 lap 
PIGGY is on his 9 lap 
DUCK completes the race 
PIGGY completes the race 
ELEFANT completes the race 
STAR completes the race