生产者消费者平均等待时间不输出/缓冲查询

时间:2014-08-18 22:40:35

标签: java multithreading buffer wait producer-consumer

我目前正在使用java制作一个假设的生产者消费者问题。对象是有一个1000字节的操作系统,但只有500字节可用于线程,因为驱动程序和其他操作已经消耗了500字节。线程如下:

  1. 启动BubbleWitch2会话10秒的线程,每个需要100个字节的RAM 第二
  2. 启动Spotify流20秒的线程,每秒需要250个字节的RAM 您还应该考虑到操作系统同时支持系统的事实 活动和管理安装它的设备的处理器,内存和磁盘空间。 因此,另外创建:
  3. 系统和管理线程,它们一起每秒需要50个字节的RAM,以及 一旦被调用,就执行一段随机的时间。
  4. 用于安装2 KB的新安全更新的线程,该安全更新将存储到磁盘,并且需要150 安装时每秒的RAM字节数。假设系统中有足够的磁盘容量来支持 这个帖子。
  5. 操作系统仅具有每秒200字节的容量,因此较大的线程(例如spotify)将经历延迟或被迫等待。我已经使用了代码,据我所知,实现了这一点。我还需要生成退出时间,这是我用时间戳完成的,并计算线程的平均等待时间。

    我已经在我的解决方案中包含了使用system.out.print的平均等待时间的代码,但无论我做什么,它实际上并没有输出时间 - 就好像它们不存在一样。

    我也不确定缓冲区大小限制是否有效,因为它只是几毫秒 - 有什么方法可以判断这是否适用于下面的代码?

    我的主要方法。

       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, 1);
                Spotify p2 = new Spotify(c, 2);
                SystemManagement p3 = new SystemManagement(c, 3);
                SecurityUpdate p4 = new SecurityUpdate(c, 4, p1, p2, p3);
    
    
                p1.setName("BubbleWitch2 ");
                p2.setName("Spotify ");
                p3.setName("System Management ");
                p4.setName("Security Update ");
    
                p1.setPriority(10);
                p2.setPriority(10);
                p3.setPriority(10);
                p4.setPriority(5);
    
                c1.start();
                p1.start();
                p2.start();
                p3.start();
                p4.start();
    
                p2.join();
                p3.join();
                p4.join();
                System.exit(0);
    
            }
        }
    
    
    My buffer class
    
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    
    /**
     * Created by Rory on 10/08/2014.
     */
    class Buffer {
        private int contents, count = 0, process = 0;
        private boolean available = false;
        private long start, end, wait, request= 0;
        private DateFormat time = new SimpleDateFormat("mm:ss:SSS");
    
    
    
        public synchronized int get() {
            while (process <= 500) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
            process -= 200;
            System.out.println("CPU After Process " + process);
            notifyAll();
            return contents;
        }
    
        public synchronized void put(int value) {
            while (process >= 1000) {
                start = System.currentTimeMillis();
                try {
                    wait();
                } catch (InterruptedException e) {
                }
                end = System.currentTimeMillis();
                wait = end - start;
                count++;
                request += wait;
                System.out.println("Application Request Wait Time: " + time.format(wait));
                process += value;
                contents = value;
                notifyAll();
            }
        }
    }
    

    我的安全更新类

    import java.lang.*;
    import java.lang.System;
    
    
    /**
     * Created by Rory 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 < 15; i++) {
                buffer.put(i);
                System.out.println(getName() + this.number
                        + " put: " + i);
                try {
                    sleep(1500);
                } catch (InterruptedException e) {
                }
            }
            System.out.println("-----------------------------");
            System.out.println("Security Update has finished executing.");
            System.out.println("------------------------------");
        }
    }
    

    我的处理器类

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

    我的bubblewitch课程

    import java.lang.*;
    import java.lang.System;
    import java.sql.Timestamp;
    
    /**
     * Created by Rory on 10/08/2014.
     */
    class BubbleWitch2 extends Thread {
        private Buffer buffer;
        private int number;
        private int bytes = 100;
        private int duration;
    
        public BubbleWitch2(Buffer c, int pduration) {
            buffer = c;
    
            duration = pduration;
        }
    
        long startTime = System.currentTimeMillis();
        public void run() {
            for (int i = 0; i < 10; i++) {
                buffer.put(bytes);
                System.out.println(getName() + this.number
                        + " put: " + i);
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                }
            }
            long endTime = System.currentTimeMillis();
            long timeTaken = endTime - startTime;
            java.util.Date date = new java.util.Date();
    
    
            System.out.println("-----------------------------");
            System.out.println("BubbleWitch2 has finished executing.");
            System.out.println("Time taken to execute was " +timeTaken+ " milliseconds");
            System.out.println("Time Bubblewitch2 thread exited Processor was " + new Timestamp(date.getTime()));
            System.out.println("-----------------------------");
        }
    }
    

    我的系统管理

    class SystemManagement extends Thread {
        private Buffer buffer;
        private int number, min = 1, max = 15;
        private int loopCount = (int) (Math.random() * (max - min));
        private int bytes = 50;
        private int process = 0;
    
    
        public SystemManagement(Buffer c, int number) {
            buffer = c;
            this.number = number;
        }
    
        public void run() {
            for (int i = 0; i < loopCount; i++) {
                buffer.put(50);
                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("-----------------------------");
        }
    }
    

    我的spotify课程     import java.sql.Timestamp;

    /**
     * Created by Rory on 11/08/2014.
     */
    class Spotify extends Thread {
        private Buffer buffer;
        private int number;
        private int bytes = 250;
    
    
        public Spotify(Buffer c, int number) {
            buffer = c;
            this.number = number;
        }
    
    
        long startTime = System.currentTimeMillis();
        public void run() {
            for (int i = 0; i < 20; i++) {
                buffer.put(bytes);
                System.out.println(getName() + this.number
                        + " put: " + i);
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                }
            }
    
            long endTime = System.currentTimeMillis();
            long timeTaken = endTime - startTime;
            java.util.Date date = new java.util.Date();
            System.out.println(new Timestamp(date.getTime()));
    
    
            System.out.println("-----------------------------");
            System.out.println("Spotify has finished executing.");
            System.out.println("Time taken to execute was " + timeTaken + " milliseconds");
            System.out.println("Time that Spotify thread exited Processor was " + date);
    
            System.out.println("-----------------------------");
    
    
        }
    }
    

    我可能需要将时间戳添加到一个或两个类但是有没有人知道如何让我的平均时间实际打印出来?或者是什么阻止它以及缓冲限制是否有效地显示在这里(假设我们正在讨论毫秒?) 谢谢。

2 个答案:

答案 0 :(得分:2)

sys out未打印的原因是缓冲类中的以下条件: -

public synchronized void put(int value) {
        while (process >= 1000) {
            .....
            notifyAll();
        }
}

这个条件永远不会令人满意,因为这个过程永远不会超过1000

这就是你的处理器线程也被卡住的原因,因为当它调用get()时它发现进程小于500,因此当它到达wait()代码行时它会无限期地等待。

在你的装配中适当地纠正过程条件应该让你丢失的系统打印出来

public synchronized void put(int value) {
        if(process <= 500) {
            process+=value;
        } else {
            //while (process >= 1000) {
                start = System.currentTimeMillis();
                try {
                    wait();
                } catch (InterruptedException e) {
                }
                end = System.currentTimeMillis();
                wait = end - start;
                count++;
                request += wait;
                System.out.println("Application Request Wait Time: " + time.format(wait));
                process += value;
                contents = value;               
            //}
        }
        notifyAll();
    }

答案 1 :(得分:2)

如果您希望securityupdate线程始终在最后运行,那么在该线程中使用join的正确方法如下: -

class SecurityUpdate extends Thread {
    private Buffer buffer;
    private int number;
    private int bytes = 150;
    private int process = 0;
    private BubbleWitch2 bubbleWitch2;
    private Spotify spotify;
    private SystemManagement systemManagement;

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

    public void run() {

        try {
            bubbleWitch2.join();
            spotify.join();
            systemManagement.join();
        } catch (InterruptedException e) {      
        }
        System.out.println("Finally starting the security update");
        for (int i = 0; i < 15; i++) {
            buffer.put(bytes);  // Paul check if it should be i or bytes
            System.out.println(getName() + this.number
                    + " put: " + i);
            try {
                sleep(1500);  // Paul why is this made to sleep 1500 seconds?
            } catch (InterruptedException e) {
            }
        }
        System.out.println("-----------------------------");
        System.out.println("Security Update has finished executing.");
        System.out.println("------------------------------");
    }
}