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

Java 生产者消费者平均等待时间未输出/缓冲区查询,java,multithreading,buffer,wait,producer-consumer,Java,Multithreading,Buffer,Wait,Producer Consumer,我目前正在使用java解决一个假设的生产者-消费者问题。目标是拥有一个1000字节的操作系统,但只有500字节可用于线程,因为驱动程序和其他操作已经消耗了500字节。线程如下所示: 一个线程,用于启动10秒的BubbleWitch2会话,每个会话需要100字节的RAM 第二 启动Spotify流20秒的线程,每秒需要250字节的RAM 您还应该考虑到这样一个事实,即操作系统同时支持该系统 活动并管理安装该活动的设备的处理器、内存和磁盘空间。 因此,另外创建: 系统线程和管理线程,它们一起需要每秒

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

一个线程,用于启动10秒的BubbleWitch2会话,每个会话需要100字节的RAM 第二 启动Spotify流20秒的线程,每秒需要250字节的RAM 您还应该考虑到这样一个事实,即操作系统同时支持该系统 活动并管理安装该活动的设备的处理器、内存和磁盘空间。 因此,另外创建: 系统线程和管理线程,它们一起需要每秒50字节的RAM,以及 一旦调用,执行一段随机长度的时间。 安装2 KB新安全更新的线程,该更新将存储到磁盘,需要150个线程 安装时每秒内存字节数。假设系统中有足够的磁盘容量来支持 这根线。 操作系统每秒只有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("-----------------------------");


    }
}
我可能需要给一两个类添加时间戳,但有人知道如何让我的平均时间真正打印出来吗?或者,是什么阻止了它,如果缓冲区限制在这里有效地显示出来,那么我们谈论的是毫秒?
谢谢

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

public synchronized void put(int value) {
        while (process >= 1000) {
            .....
            notifyAll();
        }
}
这个条件永远不会满足,因为过程永远不会超过1000

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

在您的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();
    }

如果希望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("------------------------------");
    }
}

请告诉我们哪个系统输出不工作?不工作的系统输出是指缓冲区类中的cpu后处理和应用程序请求时间,尽管我现在意识到我忘了在其中写入平均值。即使如此,它仍然没有打印。我还想知道,我的时间变量是否需要在我所有的类中共享相同的名称?例如,在所有线程中,它应该是starttime和endtime,而不是spotstarttime、bubblestarttime等?那么我应该如何更改它呢?我在一段时间内将1000改为500,但它仍然没有打印?或者我如何纠正它?代码中还有其他错误吗?@paul:想想while循环内部和外部的内容。我不确定你的意思,是否可以将所有额外的内容包括notify all放入while循环内部?在进行这些更改后,我刚刚运行了我的程序,现在处理器没有显示在输出中,这与此代码有关吗?@paul看起来您在put方法中将进程的检查值设为1000,将其等同于1000字节,而在spotify线程中,您正试图将250个值放入缓冲区,因为它需要250个字节根据你的问题,但整个设计的映射字节需要一个程序运行的整数值,你把它放在缓冲区,并检查进程>=1000是混乱的,我不知道这是否是你手头的问题的准确表示,或它会给你的问题的解决方案。哦,我让它睡眠到15秒作为其2000字节的总大小,以每秒150字节的速率运行。这是不正确的还是有其他方法?它应该是随机的吗?我确实希望输出也以毫秒为单位,这是可以的。我已经运行了整个程序,有时平均等待时间是00:00:00,其他随机时间现在是00:00:07或其他时间。这种随机性正常吗?@paul你正在制作安全线程 睡眠1.5秒。检查您的要求是否正确。关于等待时间的随机性,是的,它会有所不同,但我觉得即使是处理器线程也需要休眠1000毫秒。