I am currently making a hypothetical producer consumer problem using java. The object is to have an operating system which is 1000 bytes, but only 500 bytes available to use for threads as 500 bytes have already been consumed by drivers and other operations. The threads are as follows:
A thread to start a BubbleWitch2 session of 10 seconds, which requires 100 bytes of RAM per
second
A thread to start a Spotify stream of 20 seconds, which requires 250 bytes of RAM per second
You should also take into account the fact that the operating system is simultaneously supporting system
activity and managing the processor, memory and disk space of the device on which it is installed.
Therefore, additionally create:
System and management threads, which, together, require 50 bytes of RAM per second, and
execute for a random length of time, once invoked.
A thread to install a new security update of 2 KB, which will be stored to disk, and requires 150
bytes of RAM per second while installing. Assume sufficient disk capacity in the system to support
this thread.
The operating system has only capacity for 200 bytes per second, therefore a larger thread such as spotify will experience delays or be forced to wait. I have used code which as far as I can tell, implements this. I am also required to generate exit times which I have done with timestamps and to calculate average waiting times for threads.
I have included code in my solution for the average waiting times with system.out.print but no matter what I do, it is not actually outputting the times at all-as if they did not exist.
I am also not sure if the buffer size limitations are working as it is a matter of milliseconds-is there any way to tell if this is working from the 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);
}
}
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();
}
}
}
My security update class
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("------------------------------");
}
}
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;
for (int i = 0; i < 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 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("-----------------------------");
}
}
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() {
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("-----------------------------");
}
}
My spotify class
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("-----------------------------");
}
}
I may need to add timestamps to one or two classes yet but does anyone have any idea how to get my average times to actually print out? Or what is preventing it and if the buffer limitation is effectively being shown here(given that we are talking about milliseconds?)
Thanks.
The reason why sys out's are not printing is because of the below condition in your buffer class:-
public synchronized void put(int value) {
while (process >= 1000) {
.....
notifyAll();
}
}
this condition never gets satisified as the process never is greater than 1000
This is the reason why your Processor thread also gets stuck because when it calls get() it finds that the process is less than 500 and hence it indefinitely waits when it reaches the wait() line of code.
Rectifying the process condition appropriately in your put should let your missing sys out get 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();
}
If you want securityupdate thread to always run at the last then the correct way of using join within that thread is as below:-
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("------------------------------");
}
}
Related
So, like the question tile. I'm trying to learn multithreading programming. I have a awkward program to hlep me understand multithreading is faster than regular execution. The programm has seven classes in one java file, one test class, three classes implement Runnable, and three regular classes. The six classes all do the same thing, counting to 10 millions and return result. My problem is the three classes using three threads to run, but they didn't return the correct counts as I expected. However the three regular classes work fine.
I really appreciate anyone can help me to understand why it happens! I using JDK 9 and Eclipse 2018-12.
import java.time.Duration;
import java.time.Instant;
class MyMultiThreadExample{
public static void main(String[] args) {
GameOne g1 = new GameOne();
GameTwo g2 = new GameTwo();
GameThree g3 = new GameThree();
Thread thread1 = new Thread(g1);
Thread thread2 = new Thread(g2);
Thread thread3 = new Thread(g3);
Instant start1 = Instant.now();
thread1.start();
thread2.start();
thread3.start();
Instant end1 = Instant.now();
long elapsed = Duration.between(start1, end1).toMillis();
int total = g1.getCount() + g2.getCount() + g3.getCount();
System.out.println("MultiThread running cost " + elapsed + " to count " + total + " times");
GameFour g4 = new GameFour();
GameFive g5 = new GameFive();
GameSix g6 = new GameSix();
Instant start2 = Instant.now();
g4.run();
g5.run();
g6.run();
Instant end2 = Instant.now();
long elapsed2 = Duration.between(start2, end2).toMillis();
int total2 = g3.getCount() + g4.getCount() + g5.getCount();
System.out.println("Sequential running cost " + elapsed2 + " to count " + total2 + " times");
}
}
class GameOne implements Runnable {
int count1 = 0;
#Override
public void run() {
for (int i = 0; i < 10000000; i++) {
// System.out.print("Game1 at round " + count + " now");
count1++;
}
}
public int getCount() {
System.out.println("GameOne counts " + count1);
return count1;
}
}
class GameTwo implements Runnable {
int count2 = 0;
#Override
public void run() {
for (int i = 0; i < 10000000; i++) {
// System.out.print("Game2 at round " + count + " now");
count2++;
}
}
public int getCount() {
System.out.println("GameTwo counts " + count2);
return count2;
}
}
class GameThree implements Runnable {
int count3 = 0;
#Override
public void run() {
for (int i = 0; i < 10000000; i++) {
// System.out.print("Game3 at round " + count + " now");
count3++;
}
}
public int getCount() {
System.out.println("GameThree counts " + count3);
return count3;
}
}
class GameFour {
int count4 = 0;
public void run() {
for (int i = 0; i < 10000000; i++) {
// System.out.print("Game3 at round " + count + " now");
count4++;
}
}
public int getCount() {
System.out.println("GameFour counts " + count4);
return count4;
}
}
class GameFive {
int count5 = 0;
public void run() {
for (int i = 0; i < 10000000; i++) {
// System.out.print("Game3 at round " + count + " now");
count5++;
}
}
public int getCount() {
System.out.println("GameFive counts " + count5);
return count5;
}
}
class GameSix {
int count6 = 0;
public void run() {
for (int i = 0; i < 10000000; i++) {
// System.out.print("Game3 at round " + count + " now");
count6++;
}
}
public int getCount() {
System.out.println("GameFive counts " + count6);
return count6;
}
}
I have a awkward program to hlep me understand multithreading is faster than regular execution.
It's important to understand this is not always the case. You should only use multiple Threads when you have long running tasks that can run in parallel. IF your tasks are short, they almost certainly will run faster by running on a single Thread as there's an overhead on creating an specially synchronizing between Threads.
With that out of the way, you are not actually measuring the correct time here.
When you call Thread.start(), it will run the relevant Runnable in parallel with the code inside your function.
To let the Threads run until they complete before proceeding, you must call Thread#join():
thread1.start();
thread2.start();
thread3.start();
// all 3 Threads may be running now, but maybe not even started!
// let's wait for them to finish running by joining them
thread1.join();
thread2.join();
thread3.join();
This is the easiest way to wait... but there are others and this is a complex topic.
You may also run into trouble as your tasks have mutable state (the count variables) and the visibility of changes from different Threads needs to be carefully managed (you can make it volatile, for example, so updates are flushed to other Threads).
To learn more about concurrency in Java, I recommend you read about it. The Baeldung tutorials are excellent.
You're forgetting to call thread.join() -- this waits until the thread finishes executing.
Otherwise you're reading the counters in the middle of the execution.
Your code should be:
thread1.start()
thread2.start()
thread3.start()
thread1.join()
thread2.join()
thread3.join()
Additionally, all your classes can be compacted into a single class Game:
class Game implements Runnable {
String name;
int count = 0;
public Game(String name) {
this.name = name;
}
#Override
public void run() {
for (int i = 0; i < 10000000; i++) {
// System.out.print(name + " at round " + count + " now");
count++;
}
}
public int getCount() {
System.out.println(name + " counts " + count);
return count;
}
}
Each will have its own counter, and you can run them in a thread or in the same thread by calling run() -- your main method remains mostly unchanged except where they're instantiated. They can be instantiated like:
Game g1 = new Game("GameOne");
Game g2 = new Game("GameTwo");
Game g3 = new Game("GameThree");
Game g4 = new Game("GameFour");
Game g5 = new Game("GameFive");
Game g6 = new Game("GameSix");
I have a problem dealing with thread communication in java. I am doing a project that will connect people to 3 different Elevators (depending on the floor they want to go) that have a capacity limit. The thing is I have three difficulties.
My code is Consumer-Producer problem based, and I don't know how to change it so the elevator doesn't wait for it to be full, but starts after time by itself.
Another one is that the program stops before completing the loops. (no idea why).
If i try to check if the elevator hasn't been chosen (by geting the capacity) and not display the info of it being back at floor 0, the program doesn't work.
My code: (Classes of elevator 2 and 3 and their buffers are identical)
public class Proba { //test class
public static void main(String[] args) {
Pojemnik c = new Pojemnik();
Pojemnik1 d = new Pojemnik1();
Pojemnik2 e = new Pojemnik2();
Winda p1 = new Winda(c, 1);
Winda1 p2 = new Winda1(d, 2);
Winda2 p3 = new Winda2(e, 3);
Osoba c1 = new Osoba(c, d, e, 1);
p1.start();
p2.start();
p3.start();
c1.start();
}
}
class Osoba extends Thread //person class
{
private Pojemnik pojemnik;
private Pojemnik1 pojemnik1;
private Pojemnik2 pojemnik2;
private int number;
public Osoba(Pojemnik c, Pojemnik1 d, Pojemnik2 e, int number) {
pojemnik = c;
pojemnik1 = d;
pojemnik2 = e;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
{
int k=0;
while (k==0){ //i dont; want floor 0
k = -2 + (int)(Math.random()*7);} //choosing floor
int h;
if(k>-3&&k<1){ //decision which elevator
value = pojemnik.get(); // getting possible capacity
if(value>0){
pojemnik.put(value-1);} //lowering capacity
h=5-value; // how many people are already in
System.out.println("Ktos wsiadl do windy #1"
//+ this.number
+ " jest w niej " + h + " osob i wybrano pietro nr " + k);
}
if(k>-1&&k<4){
value = pojemnik1.get();
if(value>0){
pojemnik1.put(value-1);}
h=5-value;
System.out.println("Ktos wsiadl do windy #2"
//+ this.number
+ " jest w niej " + h + " osob i wybrano pietro nr " + k);
}
if(k>3&&k<8){
value = pojemnik2.get();
if(value>0){
pojemnik1.put(value-1);}
h=5-value;
System.out.println("Ktos wsiadl do windy #3"
//+ this.number
+ " jest w niej " + h + " osob i wybrano pietro nr " + k);
}
}
}
}
}
//import java.util.*;
//import java.lang.*;
class Pojemnik //buffor class
{
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;
if(value>5){ //way to never get too high capacity
contents=5;
}
available = true;
notifyAll();
}
}
// import java.lang.*; // elevator class
class Winda extends Thread {
private Pojemnik pojemnik; //bufor
private int number;
public Winda(Pojemnik c, int number) {
pojemnik = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
//pojemnik.get();
pojemnik.put(5); // the elevator is empty 5 people can go in
System.out.println("Winda #" + this.number
+ " jest na poziomie 0 "); //info that elevator is on floor 0
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) { }
}
}
}
I have tried to create a recursive task that divides a set of elements into smaller pieces and runs a series of math works on them, say numerically integration, then tries to use separate results for each element. but it seems that an error occurs before the pool does its works completely. my code tries to reach the elements that are not yet processed.
could you please help me with this?
public void ComputeStiffnessMatrix(DataScanner DS){
ans=new double [2*(DS.getXarray().length)][2*(DS.getXarray().length)];
int nelels=((DS.getNelKsi())*(DS.getNelEta()));
ArrayList<ElementStiffness> elsm=new ArrayList<>();
StiffnessMatrix SM=new StiffnessMatrix(elsm,DS,0,(nelels-1));
ForkJoinPool pool = new ForkJoinPool();
System.out.println("Stiffness Matrix Computation...");
long startTime = System.currentTimeMillis();
pool.invoke(SM);
long endTime = System.currentTimeMillis();
System.out.println("Stiffness Matrix Computation took " + (endTime - startTime) + " milliseconds.");
System.out.println("Arranging into an array...");
long startTime2 = System.currentTimeMillis();
try {
ArrayList<ElementStiffness> kk=(ArrayList<ElementStiffness>) SM.get();
for (int el=0;el<nelels;el++){
ElementStiffness pp=kk.get(el);
System.out.println("EL NO."+el);
double vals[][]=pp.getStiffnessMatrix();
int sup[]=pp.getControlpointsSupport();
for (int i=0;i<sup.length;i++){
for (int j=0;j<sup.length;j++){
ans[(2*sup[i])+0][(2*sup[j])+0]=ans[(2*sup[i])+0][(2*sup[j])+0]+vals[(2*i)+0][(2*j)+0];
ans[(2*sup[i])+0][(2*sup[j])+1]=ans[(2*sup[i])+0][(2*sup[j])+1]+vals[(2*i)+0][(2*j)+1];
ans[(2*sup[i])+1][(2*sup[j])+0]=ans[(2*sup[i])+1][(2*sup[j])+0]+vals[(2*i)+1][(2*j)+0];
ans[(2*sup[i])+1][(2*sup[j])+1]=ans[(2*sup[i])+1][(2*sup[j])+1]+vals[(2*i)+1][(2*j)+1];
}
}
}
} catch (InterruptedException | ExecutionException ex) {
Logger.getLogger(IGATest.class.getName()).log(Level.SEVERE, null, ex);
}
long endTime2 = System.currentTimeMillis();
System.out.println("Stiffness Matrix rearrangement took " + (endTime2 - startTime2) + " milliseconds.");
try{
FileOutputStream fs=new FileOutputStream("stifmat.tmp");
ObjectOutputStream os=new ObjectOutputStream(fs);
os.writeObject(ans);
os.close();
}catch (IOException e){
e.printStackTrace();
}
ans=null;
}
And the siffnessmatrix class:
public class StiffnessMatrix extends RecursiveTask {
private final int min;
private final int max;
private final DataScanner ds;
ArrayList<ElementStiffness> elsm;
public StiffnessMatrix (ArrayList<ElementStiffness> elsm,DataScanner DS,int min, int max){
this.ds=DS;
this.min=min;
this.max=max;
this.elsm=elsm;
}
protected void compdir(){
for (int i=min;i<=max;i++){
double[] elsp=ds.getElementSpan(i);
Element el=new Element(ds,((elsp[0]+elsp[1])*0.5),((elsp[2]+elsp[3])*0.5));
ElementStiffness els=new ElementStiffness(ds,el);
elsm.add(els);
}
}
#Override
protected ArrayList <ElementStiffness> compute(){
int processors = Runtime.getRuntime().availableProcessors();
if (max-min<processors) {
compdir();
} else {
int center = min + (max - min) / 2;
invokeAll(new StiffnessMatrix(elsm,ds, min, center) , new StiffnessMatrix(elsm, ds, center+1, max));
}
return elsm;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a simple Producer (1) and consumer (varying from 1-50) problem where the Producer simply adds elements in a Queue and the consumers read it. I am trying to get 100% CPU utilization.
Currently I am getting 20% utilization. And I cannot get 100% CPU utilization. I want all my CPU used to perform the operation faster. What should I look at?
Machine : i7 (4th generation - quad code with Hyper Threading) running windows 8 and Java 7.
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.Semaphore;
/*
* Purpose is to test system clock granularity
*/
public class ClockGranularity{
private static final int MaxExecutionTimeMsec = 6;
//60*1000 milliseconds
private static float delta = 0.01f;
//Inter-arrival Time(IAT): in milliseconds
static final int ArraySize =(int) ((float)MaxExecutionTimeMsec/delta);
private static final int convertMilliToNano = 1000000;
private static String getTime() {
DateFormat format = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
Calendar cal = Calendar.getInstance();
return format.format(cal.getTime());
}
/*
* Invoke 1 producer vs 1,2,3 consumers
* Write consumer to file
*/
public static void main(String args[]) {
ClockGranularity.delta = delta*convertMilliToNano;
long execStartTime = System.currentTimeMillis();
long experimentStartTime = System.nanoTime();
long execDuration, experimentRuntime;
Buffer requestQueue = new Buffer();
Producer producer = new Producer(requestQueue);
Consumer consumer = new Consumer(requestQueue);
Consumer consumer2 = new Consumer(requestQueue);
Consumer consumer3 = new Consumer(requestQueue);
consumer.start();
consumer2.start();
consumer3.start();
do {
execDuration = System.currentTimeMillis() - execStartTime;
experimentRuntime = System.nanoTime() - experimentStartTime;
if(experimentRuntime >= delta) {
experimentStartTime = System.nanoTime();
producer.run();
}
} while (execDuration <= MaxExecutionTimeMsec);
consumer.interrupt();
consumer2.interrupt();
consumer3.interrupt();
delta/=convertMilliToNano;
try {
String producerFile = "Producer-" + delta + " msec #" + getTime();
printToFile(producerFile,requestQueue.getQueue());
String consumerFile = "Consumer-" + delta + " msec#" + getTime();
printToFile(consumerFile, consumer.getValidateConsumerArray());
consumerFile = "Consumer2-" + delta + " msec#" + getTime();
printToFile(consumerFile, consumer2.getValidateConsumerArray());
consumerFile = "Consumer3-" + delta + " msec#" + getTime();
printToFile(consumerFile, consumer3.getValidateConsumerArray());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void printToFile(String outputFileName,Queue<Integer> requestQueue) throws IOException{
outputFileName = outputFileName.replace(':', '-');
String lineSeparator = System.lineSeparator();
File directory = new File("Practice Coding\\src\\ClockGranularity Test results\\Semaphore and Queue\\");
File file = File.createTempFile(outputFileName, ".txt",directory);
FileWriter writer = new FileWriter(file);
writer.append("Index \tQueue Contents" + lineSeparator);
int size = requestQueue.size();
String summary = "queue<>" + size;
for(int i = 0; i<size; i++) {
String temp = i + " ticks \t" + requestQueue.poll();
System.out.println(temp);
writer.append(temp + lineSeparator);
}
writer.append(lineSeparator + "Summary: " + lineSeparator);
writer.append(summary + lineSeparator);
System.out.println(outputFileName + " " + summary);
writer.close();
}
}
class Buffer {
private Queue<Integer> requestsQueue;
Semaphore accessQueue;
Buffer() {
requestsQueue = new LinkedList<Integer>();
accessQueue = new Semaphore(1);
}
public void put(Integer tick) throws InterruptedException {
accessQueue.acquire();
requestsQueue.add(tick);
accessQueue.release();
}
public synchronized int get() throws InterruptedException {
int tick;
while(requestsQueue.size() == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
accessQueue.acquire();
tick = requestsQueue.poll();
accessQueue.release();
return tick;
}
public Queue<Integer> getQueue() {
return requestsQueue;
}
}
class Consumer extends Thread{
private Buffer bufferQueue;
private Queue<Integer> validateConsumer;
Consumer(Buffer requestQueue) {
bufferQueue = requestQueue;
validateConsumer = new LinkedList<Integer>();
}
public void run() {
while(true) {
int i;
try {
i = bufferQueue.get();
validateConsumer.add(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public Queue<Integer> getValidateConsumerArray() {
return validateConsumer;
}
}
class Producer extends Thread{
public int tick = 0;
private Buffer bufferQueue;
Producer(Buffer requestQueue) {
bufferQueue = requestQueue;
}
public void run() {
try {
bufferQueue.put(tick++);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Your Buffer implementation is wrong. It essentially serializes read/writes thus terrible performance. You need to use or implement a reader-writer lock instead.
Things you can do:
-Instead of doing busy loops (the query for time may be reducing your utilization), see if there is a difference if you put the producer code code to while(true) like you do with the consumers, sleep the main thread for MaxExecutionTimeMsec time, and do an interrupt on the producer like you do a consumer.
-This is backwards from its typically usage but run a special profiler and see how much time is spent where. If it isn't a business section (say waiting for lock acquisition) do a redesign there.
-You may want to look into spin-locks if performance is key and you have a multi-core p
processor. A context switch can be more expensive than a spin-lock in some amortized scenarios.
I am trying to understand the utilities in java.util.concurrent package and learnt that we can submit callable objects to the ExecutorService, which returns Future, which is filled with the value returned by the callable, after successful completion of task within call() method.
I am understanding that all the callables are executed concurrently using multiple threads.
When I wanted to see how much improvement ExecutorService gives over the batch task execution, i thought of capturing time.
Following is the code which i tried to execute -
package concurrency;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ExecutorExample {
private static Callable<String> callable = new Callable<String>() {
#Override
public String call() throws Exception {
StringBuilder builder = new StringBuilder();
for(int i=0; i<5; i++) {
builder.append(i);
}
return builder.toString();
}
};
public static void main(String [] args) {
long start = System.currentTimeMillis();
ExecutorService service = Executors.newFixedThreadPool(5);
List<Future<String>> futures = new ArrayList<Future<String>>();
for(int i=0; i<5; i++) {
Future<String> value = service.submit(callable);
futures.add(value);
}
for(Future<String> f : futures) {
try {
System.out.println(f.isDone() + " " + f.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("Executer callable time - " + (end - start));
service.shutdown();
start = System.currentTimeMillis();
for(int i=0; i<5; i++) {
StringBuilder builder = new StringBuilder();
for(int j=0; j<5; j++) {
builder.append(j);
}
System.out.println(builder.toString());
}
end = System.currentTimeMillis();
System.out.println("Normal time - " + (end - start));
}
}
and here is the output of this -
true 01234
true 01234
true 01234
true 01234
true 01234
Executer callable time - 5
01234
01234
01234
01234
01234
Normal time - 0
Please let me know if I am missing something OR understanding something in a wrong way.
Thanks in advance for your time and help for this thread.
If you task in Callable is to small, you won't get benefits from concurrency due task switching and overhead for initialisation. Try to add more heavier loop in callable, say 1000000 iterations, and you can see difference
When you run any code esp for the first time, it takes time. If you pass a task to another thread it can take 1-10 micro-seconds and if your task take less time than this, the overhead can be greater than the benefit. i.e. using multiple threads can be much slower than using a single thread if your overhead is high enough.
I suggest you
increase the cost of the task to 1000 iterations.
make sure the result is not discarded in the single threaded example
run both tests for at least a couple of seconds to ensure the code has warmed up.
Not an answer (but I am not sure the code will fit a comment). To expand a bit on what Peter said, there is usually a sweet spot for the size of your jobs (measured in execution time), to balance pool/queue overhead with fair work distribution among workers. The code example helps find an estimate for that sweet spot. Run on your target hardware.
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
public class FibonacciFork extends RecursiveTask<Long> {
private static final long serialVersionUID = 1L;
public FibonacciFork( long n) {
super();
this.n = n;
}
static ForkJoinPool fjp = new ForkJoinPool( Runtime.getRuntime().availableProcessors());
static long fibonacci0( long n) {
if ( n < 2) {
return n;
}
return fibonacci0( n - 1) + fibonacci0( n - 2);
}
static int rekLimit = 8;
private static long stealCount;
long n;
private long forkCount;
private static AtomicLong forks = new AtomicLong( 0);
public static void main( String[] args) {
int n = 45;
long times[] = getSingleThreadNanos( n);
System.out.println( "Single Thread Times complete");
for ( int r = 2; r <= n; r++) {
runWithRecursionLimit( r, n, times[ r]);
}
}
private static long[] getSingleThreadNanos( int n) {
final long times[] = new long[ n + 1];
ExecutorService es = Executors.newFixedThreadPool( Math.max( 1, Runtime.getRuntime().availableProcessors() / 2));
for ( int i = 2; i <= n; i++) {
final int arg = i;
Runnable runner = new Runnable() {
#Override
public void run() {
long start = System.nanoTime();
final int minRuntime = 1000000000;
long runUntil = start + minRuntime;
long result = fibonacci0( arg);
long end = System.nanoTime();
int ntimes = Math.max( 1, ( int) ( minRuntime / ( end - start)));
if ( ntimes > 1) {
start = System.nanoTime();
for ( int i = 0; i < ntimes; i++) {
result = fibonacci0( arg);
}
end = System.nanoTime();
}
times[ arg] = ( end - start) / ntimes;
}
};
es.execute( runner);
}
es.shutdown();
try {
es.awaitTermination( 1, TimeUnit.HOURS);
} catch ( InterruptedException e) {
System.out.println( "Single Timeout");
}
return times;
}
private static void runWithRecursionLimit( int r, int arg, long singleThreadNanos) {
rekLimit = r;
long start = System.currentTimeMillis();
long result = fibonacci( arg);
long end = System.currentTimeMillis();
// Steals zählen
long currentSteals = fjp.getStealCount();
long newSteals = currentSteals - stealCount;
stealCount = currentSteals;
long forksCount = forks.getAndSet( 0);
System.out.println( "Fib(" + arg + ")=" + result + " in " + ( end-start) + "ms, recursion limit: " + r +
" at " + ( singleThreadNanos / 1e6) + "ms, steals: " + newSteals + " forks " + forksCount);
}
static long fibonacci( final long arg) {
FibonacciFork task = new FibonacciFork( arg);
long result = fjp.invoke( task);
forks.set( task.forkCount);
return result;
}
#Override
protected Long compute() {
if ( n <= rekLimit) {
return fibonacci0( n);
}
FibonacciFork ff1 = new FibonacciFork( n-1);
FibonacciFork ff2 = new FibonacciFork( n-2);
ff1.fork();
long r2 = ff2.compute();
long r1 = ff1.join();
forkCount = ff2.forkCount + ff1.forkCount + 1;
return r1 + r2;
}
}