多线程创建每个线程的随机延迟

时间:2013-10-10 06:28:35

标签: java multithreading algorithm concurrency multidimensional-array

我需要帮助来解决这个问题我需要对每个人进行随机延迟,我不知道如何在阵列线程中实现它。请有人帮帮我!我在线程列表中需要一些东西或者其他东西,我需要知道在哪里以及如何实现。我坚持这个。我想要制作一个排列的线程来为每个人制造延迟

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


            }

        }



    }

1 个答案:

答案 0 :(得分: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;
        }

    }        
}

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

这样,您可以根据需要向上或向下缩放生产者