代码之家  ›  专栏  ›  技术社区  ›  paul

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

  •  -1
  • paul  · 技术社区  · 11 年前

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

    1. 启动10秒BubbleWitch2会话的线程,每个会话需要100字节的RAM 第二
    2. 启动Spotify流的线程为20秒,每秒需要250字节的RAM 您还应考虑操作系统同时支持系统这一事实 活动并管理其上安装的设备的处理器、内存和磁盘空间。 因此,额外创建:
    3. 系统和管理线程,它们一起需要每秒50字节的RAM,以及 一旦调用,执行一段随机时间。
    4. 一个线程,用于安装2 KB的新安全更新,该更新将存储到磁盘,需要150 安装时每秒RAM字节数。假设系统中有足够的磁盘容量来支持 此线程。

    操作系统的容量仅为每秒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);
            }
        }
    }
    

    我的泡泡球课

    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类 导入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 回复  |  直到 11 年前
        1
  •  2
  •   Manjunath    11 年前

    系统输出不打印的原因是缓冲区类中存在以下情况:-

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

    这个条件永远不会得到满足,因为过程永远不会超过1000

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

    在你的put中适当地纠正工艺条件应该可以让你缺失的系统打印出来

    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();
        }
    
        2
  •  2
  •   Manjunath    11 年前

    如果您希望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("------------------------------");
        }
    }
    
    推荐文章