我需要幫助解決這個問題我需要每個人都有一個隨機延遲我只是不知道如何在陣列線程中實現它。請有人幫助我!
我需要在線程中的鏈接列表中的某些東西,或者任何東西,我需要知道在哪裏以及如何實現。我堅持這一點。即時假設做一個排列的線程,使每個人的延遲多線程創建每個線程的隨機延遲
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){
}
}
}
}
}
您希望實現什麼樣的邏輯以實現延遲? – Lokesh
這是爲什麼全部在UPPER_CASE?現在的隨機延遲代碼有什麼問題? –
當男人或女人進入CR時,應該有一個延遲。我想實現Threaded延遲,它可以有多個線程,但在數組或鏈表... – ProblematicSolution