首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java生产者消费者问题-让线程在完成之前等待其他人完成

java生产者消费者问题-让线程在完成之前等待其他人完成
EN

Stack Overflow用户
提问于 2014-08-15 21:58:19
回答 2查看 852关注 0票数 2

我目前正在处理多个线程的生产者消费者问题。最初有1000个字节可用,使用RAM nad驱动程序占用了500个字节,留给我来处理线程的500个字节。将有4个生产者,具体如下:

  1. 启动10秒BubbleWitch2会话的线程,它需要每秒100个字节的内存。
  2. 一个线程,用于启动每秒20秒的Spotify流,它需要每秒250个字节的RAM。
  3. 系统线程和管理线程,它们一起需要每秒50字节的RAM,并在调用之后执行一段随机时间。
  4. 安装一个新的2KB安全更新程序的线程,它将被存储到磁盘中,安装时需要每秒150个字节的RAM。假设系统中有足够的磁盘容量来支持这个线程。

该程序意味着在安全更新完成后停止执行。理想情况下,这应该在不设置线程优先级的情况下实现。它以前是工作的,但现在当我运行程序时,安全线程在中间完成,spotify正在结束笑。是否有可能导致这种情况的错误?我已经在下面列出了我的代码。我还没有将所有字节大小分配给线程和缓冲区。

我的主要方法。

代码语言:javascript
复制
/**
 * Created by User on 10/08/2014.
 */
public class ProducerConsumerTest {
        public static void main(String[] args) throws InterruptedException {
            Buffer c = new Buffer();
            BubbleWitch2 p1 = new BubbleWitch2(c, 1);
            Processor c1 = new Processor(c, 2);
            Spotify p2 = new Spotify(c, 3);
            SystemManagement p3 = new SystemManagement(c,4);
            securityUpdate p4 = new securityUpdate(c,5, p1,p2,p3);

            p1.setName("BubbleWitch2 ");
            p2.setName("Spotify ");
            p3.setName("System Management ");
            p4.setName("Security Update ");

            c1.start();
            p1.start();
            p2.start();
            p3.start();
            p4.start();

            p2.join();
            p3.join();
            p4.join();
            System.exit(0);

        }
    }

我的缓冲区/库洞类

代码语言:javascript
复制
/**
 * Created by User on 10/08/2014.
 */
class Buffer {
    private int contents;
    private boolean available = false;
    public synchronized int get() {
        while (available == false) {
            try {
                wait();
            }
            catch (InterruptedException e) {
            }
        }
        available = false;
        notifyAll();
        return contents;
    }
    public synchronized void put(int value) {
        while (available == true) {
            try {
                wait();
            }
            catch (InterruptedException e) {
            }
        }
        contents = value;
        available = true;
        notifyAll();
    }
}

我的消费者课

代码语言:javascript
复制
class Processor extends Thread {
    private Buffer cubbyhole;
    private int number;
    public Processor(Buffer c, int number) {
        cubbyhole = c;
        this.number = number;
    }
    public void run() {
        int value = 0;
        for (int i = 0; i < 60; i++) {
            value = cubbyhole.get();
            System.out.println("Processor #"
                    + this.number
                    + " got: " + value);
        }
    }
}

我的spotify制片人班

代码语言:javascript
复制
class Spotify extends Thread {
    private Buffer buffer;
    private int number;
    private int bytes;

    public Spotify(Buffer c, int number) {
        buffer = c;
        this.number = number;
    }

    public void run() {
        for (int i = 0; i < 20; i++) {
            buffer.put(i);
            System.out.println(getName() + this.number
                    + " put: " + i);
            try {
                sleep(1000);
            } catch (InterruptedException e) { }
        }
        System.out.println("*****************************");
        System.out.println("Spotify has finished executing.");
        System.out.println("*****************************");

    }
}

我的泡泡剂制作课

代码语言:javascript
复制
import java.lang.*;
import java.lang.System;
/**
 * Created by User on 10/08/2014.
 */
class BubbleWitch2 extends Thread {
    private Buffer buffer;
    private int number;
    private int bytes;

    public BubbleWitch2(Buffer c, int number) {
        buffer = c;
        this.number = number;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            buffer.put(i);
            System.out.println(getName() + this.number
                    + " put: " + i);
            try {
                sleep(1000);
            } catch (InterruptedException e) { }
        }
        System.out.println("*****************************");
        System.out.println("BubbleWitch2 has finished executing.");
        System.out.println("*****************************");
    }
}

我的系统管理制作人课程

代码语言:javascript
复制
  class SystemManagement extends Thread {
        private Buffer buffer;
        private int number, min = 1, max = 15;
        private int loopCount = (int) (Math.random() * ( max - min ));

        public SystemManagement(Buffer c, int number) {
            buffer = c;
            this.number = number;
        }

        public void run() {
            for (int i = 0; i < loopCount; i++) {
                buffer.put(i);
                System.out.println(getName() + this.number
                        + " put: " + i);
                try {
                    sleep(1000);
                } catch (InterruptedException e) { }
            }
            System.out.println("*****************************");
            System.out.println("System Management has finished executing.");
            System.out.println("*****************************");
        }
    }

我的安全更新类

代码语言:javascript
复制
/**
 * Created by User on 14/08/2014.
 */
import java.lang.*;
import java.lang.System;

/**
 * Created by User on 11/08/2014.
 */
class securityUpdate extends Thread {
    private Buffer buffer;
    private int number;
    private int bytes = 150;
    private int process = 0;

    public securityUpdate (Buffer c, int number, BubbleWitch2 bubbleWitch2, Spotify spotify, SystemManagement systemManagement) throws InterruptedException {
        buffer = c;
        this.number = number;
        bubbleWitch2.join();
        spotify.join();
        systemManagement.join();
    }

    public void run() {

        for (int i = 0; i < 10; i++) {
            buffer.put(i);
            System.out.println(getName() + this.number
                    + " put: " + i);
            try {
                sleep(1000);
            } catch (InterruptedException e) { }
        }
        System.out.println("*****************************");
        System.out.println("Security Update has finished executing.");
        System.out.println("*****************************");
    }
}

我希望它能够在没有硬编码的情况下在计数中最后运行,因为我需要在代码中计算以每秒150个字节的大小运行2000字节所需的时间,这将使硬编码变得无关紧要。有人有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-16 05:47:10

使用executor框架尝试下面修改的代码,它更简单:-

代码语言:javascript
复制
public class ProducerConsumerTest {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> c = new ArrayBlockingQueue<Integer>(1);
        CountDownLatch doneSignal = new CountDownLatch(3);

        Processor c1 = new Processor(c, 2, doneSignal);

        BubbleWitch2 p1 = new BubbleWitch2(c, 1, doneSignal);        
        Spotify p2 = new Spotify(c, 3, doneSignal);
        SystemManagement p3 = new SystemManagement(c,4, doneSignal);
        SecurityUpdate p4 = new SecurityUpdate(c,5, doneSignal);

        p1.setName("BubbleWitch2 ");
        p2.setName("Spotify ");
        p3.setName("System Management ");
        p4.setName("Security Update ");

        ExecutorService exec = Executors.newCachedThreadPool();
        exec.submit(c1);
        exec.submit(p1);
        exec.submit(p2);
        exec.submit(p3);        

        Future<?> securityFuture = exec.submit(p4);

        try {
            while(securityFuture.get()!=null) {

            }           
            exec.shutdown();
            while(exec.awaitTermination(1000, TimeUnit.MILLISECONDS)) {

            }
            exec.shutdownNow();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.exit(0);
    }
}
class Processor extends Thread {
    private BlockingQueue<Integer> cubbyhole;
    private int number;
    private CountDownLatch doneSignal;

    public Processor(BlockingQueue<Integer> c, int number,CountDownLatch doneSignal) {
        cubbyhole = c;
        this.number = number;
        this.doneSignal = doneSignal;
    }
    public void run() {
        int value = 0;
       // for (int i = 0; i < 60; i++) {
        while(true) {
            try {
                value = cubbyhole.take();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }
            System.out.println("Processor #"
                    + this.number
                    + " got: " + value);
        }
        //doneSignal.countDown();
    }
}

class Spotify extends Thread {
    private BlockingQueue<Integer> buffer;
    private int number;
    private int bytes;
    private CountDownLatch doneSignal;

    public Spotify(BlockingQueue<Integer> c, int number, CountDownLatch doneSignal) {
        buffer = c;
        this.number = number;
        this.doneSignal = doneSignal;
    }

    public void run() {
        for (int i = 0; i < 20; i++) {
            try {
                buffer.put(i);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(getName() + this.number
                    + " put: " + i);           
        }
        System.out.println("*****************************");
        System.out.println("Spotify has finished executing.");
        System.out.println("*****************************");
        doneSignal.countDown();
    }
}

class BubbleWitch2 extends Thread {
    private BlockingQueue<Integer> buffer;
    private int number;
    private int bytes;
    private CountDownLatch doneSignal;

    public BubbleWitch2(BlockingQueue<Integer> c, int number, CountDownLatch doneSignal) {
        buffer = c;
        this.number = number;
        this.doneSignal = doneSignal;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                buffer.put(i);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println(getName() + this.number
                    + " put: " + i);

        }
        System.out.println("*****************************");
        System.out.println("BubbleWitch2 has finished executing.");
        System.out.println("*****************************");
        doneSignal.countDown();
    }
}

class SystemManagement extends Thread {
    private BlockingQueue<Integer> buffer;
    private int number, min = 1, max = 15;
    private int loopCount = (int) (Math.random() * ( max - min ));
    private CountDownLatch doneSignal;

    public SystemManagement(BlockingQueue<Integer> c, int number, CountDownLatch doneSignal) {
        buffer = c;
        this.number = number;
        this.doneSignal = doneSignal;
    }

    public void run() {
        for (int i = 0; i < loopCount; i++) {
            try {
                buffer.put(i);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println(getName() + this.number
                    + " put: " + i);

        }
        System.out.println("*****************************");
        System.out.println("System Management has finished executing.");
        System.out.println("*****************************");
        doneSignal.countDown();
    }
}

class SecurityUpdate extends Thread {
    private BlockingQueue<Integer> buffer;
    private int number;
    private int bytes = 150;
    private int process = 0;
    private CountDownLatch doneSignal;

    public SecurityUpdate (BlockingQueue<Integer> c, int number, CountDownLatch doneSignal) {
        buffer = c;
        this.number = number;
        this.doneSignal = doneSignal;
    }

    public void run() {
        try {
            doneSignal.await();         
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 0; i < 10; i++) {
            try {
                buffer.put(i);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println(getName() + this.number
                    + " put: " + i);

        }
        System.out.println("*****************************");
        System.out.println("Security Update has finished executing.");
        System.out.println("*****************************");
    }
}

如果你有什么问题,请告诉我。

票数 2
EN

Stack Overflow用户

发布于 2014-08-16 05:33:06

我不确定我是否完全理解这个问题,但是您正在加入securityUpdate的构造函数中的线程:

代码语言:javascript
复制
bubbleWitch2.join();
spotify.join();
systemManagement.join();

这些连接发生在线程启动之前,因此它们是不操作的。

同样,我在这里还没有完全理解这个问题,但似乎您正在尝试让securityUpdate等待这三个线程的完成。如果是这样的话,您可能希望将对join()的调用移到run()方法中。

或者类似的东西。祝好运。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25334793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档