-1

我需要幫助解決這個問題我需要每個人都有一個隨機延遲我只是不知道如何在陣列線程中實現它。請有人幫助我!
我需要在線程中的鏈接列表中的某些東西,或者任何東西,我需要知道在哪裏以及如何實現。我堅持這一點。即時假設做一個排列的線程,使每個人的延遲多線程創建每個線程的隨機延遲

import java.util.concurrent.*; 
import java.util.Random; 
import java.util.LinkedList; 
import java.util.Queue; 

public class Try5 extends Thread { 

    public static Semaphore Door = new Semaphore(3); 
    public static int COUNTER = 0; 
    public static final int LIST = 3; 
    public static int VACANCY = LIST; 

    //private DELAY_GENERATOR[] Person; 
    //private LinkedList<Runnable> taskQueue; 



    public static void main (String[] args) { 


     class CONTROLLER extends Thread{ 
      int ID; 
      int SEX; 
      public CONTROLLER(int a, int b){ 
       ID = a; 
       SEX = b; 
       } 
      public void run(){ 
       while(VACANCY < 0){ 
       if(SEX == 0){ 
        try{ 
        Door.acquire(); 
        VACANCY--; 
        this.MEN(); 
        System.out.println("MEN has ENTERED The CR"); 
        }catch (InterruptedException ex){} 
       } 
       else{ 
        try{ 
        Door.acquire(); 
        VACANCY--; 
        this.WOMEN(); 
        System.out.println("WOMEN has ENTERED The CR"); 
        }catch (InterruptedException ex){} 

       }}} 

      public void MEN(){ 
       System.out.println("MEN has USED The CR"); 
       //taskQueue = new LinkedList<Runnable>(); 
       //Person = new DELAY_GENERATOR[COUNTER]; 
       //Person[COUNTER] = new DELAY_GENERATOR(); 
       //Person[COUNTER].start(); 
       //Queue<Integer> MenQueue = new LinkedList<Integer>(); 
       //MenQueue.offer(COUNTER); 
       VACANCY++; 
       Door.release(); 
      } 
      public void WOMEN(){ 
       System.out.println("WOMEN has USED The CR"); 
       //taskQueue = new LinkedList<Runnable>(); 
       //Person = new DELAY_GENERATOR[COUNTER]; 
       //Person[COUNTER] = new DELAY_GENERATOR(); 
       //Person[COUNTER].start(); 
       //Queue<Integer> WomenQueue = new LinkedList<Integer>(); 
       //WomenQueue.offer(COUNTER); 
       VACANCY++; 
       Door.release(); 
      } 


     } 
     class DELAY_GENERATOR extends Thread{ 

      public void run(){ 
       try { 
        Thread.sleep((int)Math.random()*(5000-1000)); 
       } catch (InterruptedException e){ 
       } 
      } 


      } 

     } 



    } 
+0

您希望實現什麼樣的邏輯以實現延遲? – Lokesh

+0

這是爲什麼全部在UPPER_CASE?現在的隨機延遲代碼有什麼問題? –

+0

當男人或女人進入CR時,應該有一個延遲。我想實現Threaded延遲,它可以有多個線程,但在數組或鏈表... – ProblematicSolution

回答

0

這是一個非常簡單的生產者/消費者的例子。

基本上,有兩個線程會產生一個值(QueueItem)並將其添加到中央隊列中。然後每個生產者在排隊下一個物品之前等待250毫秒到5秒。

有一個消費者線程這需要在未來QueueItem關閉隊列(可用時),並將其顯示...

import java.util.Date; 
import java.util.concurrent.BlockingQueue; 
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class ProducerConsumer { 

    private BlockingQueue<QueueItem> queue; 

    public static void main(String[] args) { 
     new ProducerConsumer(); 
    } 

    public ProducerConsumer() { 
     queue = new LinkedBlockingDeque<>(); 

     startThread(new Consumer(), false); 

     startThread(new Producer("One"), true); 
     startThread(new Producer("Two"), true); 
    } 

    protected void startThread(Runnable runnable, boolean daemon) { 

     Thread thread = new Thread(runnable); 
     thread.setDaemon(daemon); 
     thread.start(); 

    } 

    public class Consumer implements Runnable { 

     @Override 
     public void run() { 
      while (true) { 
       try { 
        QueueItem next = queue.take(); 
        System.out.println("Picked " + next); 
       } catch (InterruptedException ex) { 
        ex.printStackTrace(); 
       } 
      } 
     } 
    } 

    public class Producer implements Runnable { 

     private final String name; 

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

     @Override 
     public void run() { 

      while (true) { 

       try { 
        Thread.sleep((int)(250 + (Math.random() * 4750))); 
       } catch (InterruptedException ex) { 
       } 

       queue.offer(new QueueItem(name)); 

      } 

     } 

    } 

    public class QueueItem { 

     private final String name; 
     private final Date date; 

     public QueueItem(String name) { 
      this.name = name; 
      date = new Date(); 
     } 

     @Override 
     public String toString() { 
      return "From " + name + " @ " + date; 
     } 

    }   
} 

的基本想法是,你應該有一個或多個「的製片人「或」生成器「線程生成內容到您的隊列中,然後」這些「中的每一個都會隨機休眠。

這樣,您可以根據需要縮放製作者的尺寸

+0

我真的在尋找陣列線程的代碼片段,其中有線程排列,我已經有一個生產者創造延遲生產,但我想做的和線程,將隨機睡覺,不會與其他創建問題人。這就像去CR做一些東西,我希望實際情況在我的程序中。謝謝你的方式 – ProblematicSolution

+0

你是什麼意思的「陣列線程」? – MadProgrammer

+0

MadProgrammer我認爲這就是他們所說的threadpools我只是不會如何在我的程序中實現它。工作人員實際上是一個線程,但可以循環多次地再現爲線程。我認爲很多次重複的工作線程是我的代碼的解決方案。 – ProblematicSolution