java - Producer Consumer-Average Wait times not outputting/buffer query -
i making hypothetical producer consumer problem using java. object have operating system 1000 bytes, 500 bytes available use threads 500 bytes have been consumed drivers , other operations. threads follows:
- a thread start bubblewitch2 session of 10 seconds, requires 100 bytes of ram per second
- a thread start spotify stream of 20 seconds, requires 250 bytes of ram per second should take account fact operating system simultaneously supporting system activity , managing processor, memory , disk space of device on installed. therefore, additionally create:
- system , management threads, which, together, require 50 bytes of ram per second, , execute random length of time, once invoked.
- a thread install new security update of 2 kb, stored disk, , requires 150 bytes of ram per second while installing. assume sufficient disk capacity in system support thread.
the operating system has capacity 200 bytes per second, therefore larger thread such spotify experience delays or forced wait. have used code far can tell, implements this. required generate exit times have done timestamps , calculate average waiting times threads.
i have included code in solution average waiting times system.out.print no matter do, not outputting times @ all-as if did not exist.
i not sure if buffer size limitations working matter of milliseconds-is there way tell if working code below?
my main method.
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); } } buffer class import java.text.dateformat; import java.text.simpledateformat; /** * created 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(); } } } my security update class
import java.lang.*; import java.lang.system; /** * created 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() { (int = 0; < 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("------------------------------"); } } my processor class
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; (int = 0; < 60; i++) { value = processor.get(); system.out.println("processor #" + this.number + " got: " + value); } } } my bubblewitch class
import java.lang.*; import java.lang.system; import java.sql.timestamp; /** * created 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() { (int = 0; < 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 execute " +timetaken+ " milliseconds"); system.out.println("time bubblewitch2 thread exited processor " + new timestamp(date.gettime())); system.out.println("-----------------------------"); } } my system management
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() { (int = 0; < 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("-----------------------------"); } } my spotify class import java.sql.timestamp;
/** * created 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() { (int = 0; < 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 execute " + timetaken + " milliseconds"); system.out.println("time spotify thread exited processor " + date); system.out.println("-----------------------------"); } } i may need add timestamps 1 or 2 classes yet have idea how average times print out? or preventing , if buffer limitation being shown here(given talking milliseconds?) thanks.
the reason why sys out's not printing because of below condition in buffer class:-
public synchronized void put(int value) { while (process >= 1000) { ..... notifyall(); } } this condition never gets satisified process never greater 1000
this reason why processor thread gets stuck because when calls get() finds process less 500 , hence indefinitely waits when reaches wait() line of code.
rectifying process condition appropriately in put should let missing sys out printed
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(); }
Comments
Post a Comment