-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){
                }
            }


            }

        }



    }
4

1 回答 1

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;
        }

    }        
}

基本思想是您应该有一个或多个“生产者”或“生成者”线程将内容生成到您的队列中,然后“这些”中的每一个都会以随机间隔休眠。

这样,您可以根据需要扩大或缩小生产者的规模

于 2013-10-10T06:53:33.073 回答