I am trying to make program which simulates a waitingline and lift, and individual skiers.
Now my output is fine and as expected until the skiers hit the top of the lift then begin to ski, which is when the threads begin.
My problem is, once a skier is finished he should then pend himself back into the waiting line, but alot of the skiers go missing, and never return to the line.
Any ideas?
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class ThreadsAssignment {
// Declare and initalise queues and arrays
public static BlockingQueue<String> liftQueue = new LinkedBlockingQueue<String>(11);
public static BlockingQueue<String> waitQueue = new LinkedBlockingQueue<String>();
public static String toLift;
public static String toWait;
public static String liftFront = "EMPTY";
public static String waitFront;
public static int populatedLift = 0;
public static int pauseLift;
public static int slopeTime;
public static String toPend;
public static int queueSize;
public static void main(String[] args) throws Exception{
// fill both queues list for startup
for(int i = 0; i < 30; i++){
waitQueue.add(Integer.toString(i));
}
for(int j = 0; j < 10; j++){
liftQueue.add("EMPTY");
}
// loop the simulation
while(true){
System.out.println("In Queue " + "(" + waitQueue.size() + "): " + waitQueue);
System.out.println("On Lift " + "(" + populatedLift + "): " + liftQueue + "\n");
// Stop lift for 1 second
try{
Thread.sleep(1000);}
catch (InterruptedException ex) {}
// test if the lift stops
if ((Math.random() * 100) >= 95) {
Random rand = new Random();
pauseLift = rand.nextInt(8001);
System.out.println("Lift paused for " + pauseLift + " milliseconds");
try{Thread.sleep(pauseLift);}
catch (InterruptedException ex){}}
else{}
// get the head of the waiting line then add it to lift, check if any skier is waiting.
liftFront = liftQueue.peek();
if (waitQueue.size() == 0){
liftQueue.add("EMPTY");
}
else{
toLift = waitQueue.take();
liftQueue.add(toLift);
populatedLift++;
}
// if the front of the liftQueue is occupied, call a new skier thread
if (liftFront.equals("EMPTY")){
liftQueue.poll();}
else{
liftQueue.poll();
populatedLift--;
skier s = new skier(liftFront, waitQueue);
new Thread(s).start();
}
}
}
// skier thread
public static class skier extends Thread {
static String name;
static BlockingQueue<String> theQueue;
// Constructor for the thread
public skier(String name, BlockingQueue<String> theQueue){
skier.name = name;
skier.theQueue = theQueue;
}
// run method that makes random skiing time then pends the skier back into the queue
#Override public void run() {
toPend = skier.name;
Random speed = new Random();
slopeTime = speed.nextInt(10001) + 2000;
try {Thread.sleep(slopeTime);}
catch (InterruptedException ex){}
currentThread.
if (waitQueue.contains(toPend)){}
else {try {
waitQueue.put(toPend);
} catch (InterruptedException e){}
System.out.println(toPend + "has been pended");}
}
}
}
Following code may cause skiers to become missing:
static String name;
static BlockingQueue<String> theQueue;
static means that all instances of skier will share last submitted name. You must make all skiers keep their names to themselves:
final String name;
final BlockingQueue<String> theQueue; // this may be left `static` since there's only one instance, but this would be unclean code.
// Or, as an option, let `skier` instances re-use outer class `queue`.
Btw, Java has convention of starting class names with upper-case letter, so it should be Skier as well.
And you don't need EMPTY constant, just call queue.isEmpty()
Related
I'm trying to get into the nitty gritty of understanding how wait and notifyAll work and have hit a roadblock.
This program downloads a long text document, starts multiple threads to count characters and then outputs the count totals.
I'm using wait and notifyAll to control the thread execution so that they are completed in alphabetical order. Here's the code. I'll follow with an explanation of the problem.
public class Test {
public static void main(String[] args) {
//code that reads in the data
LongTask a = new LongTask(buffer, 'a', "Thread_a", 0);
a.start();
LongTask b = new LongTask(buffer, 'b', "Thread_b", 1);
b.start();
//Repeat code for all other characters
a.join();
System.out.println("Alphabet count is: " + SharedResults.getResults());
LongTask class contains constructor and run()
public class LongTask extends Thread {
//Instance variables created here
//LongTask constructor
public LongTask (StringBuffer buffer, char target, String name, int turn)
{
super(name);
this.sharedData = sharedData;
inputData = buffer;
this.target = target;
this.turn = turn;
}
//Run method iterates through input data and counts matching characters,
//then calls addToResults
public synchronized void run()
{
//Thread t = Thread.currentThread();
String name = this.getName();
int runTurn = this.turn;
System.out.println(name + " running - Turn " + runTurn);
Integer count = 0;
for (int i = 0; i < inputData.length(); i++) {
if (inputData.charAt(i) == target) {
count ++;
}
}
ResultsEntry newResult = new ResultsEntry(count, target);
SharedResults.addToResults(newResult, turn);
}
}
SharedResults class adds results to Array. addToResults method performs this action and controls the synchronization.
public class SharedResults extends Thread{
//Code that creates array
//Code for SharedResults constructor
public synchronized static void addToResults(ResultsEntry newResult, int turn)
{
Integer resultsCount = newResult.getCount();
char resultsTarget = newResult.getTarget();
Thread t = Thread.currentThread();
/*
* Turn number is compared to the size of the results array to control the
* order of execution.
*/
while (turn != results.size()){
try {
System.out.println("Wait printout");
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(t.getName() + " is adding " + newResult);
SharedResults input = new SharedResults(resultsCount, resultsTarget);
System.out.println("Cumulative Results are " + results);
t.notifyAll();
}
Here's what I'm seeing when I watch this step through in Debug.
-Input executes and all LongTask threads start
(Thread_a should be the first thread to execute addToResults)
-Some threads (not Thread_a) hit the while evaluation of addToResults and do not proceed
-Thread_a hits the while evaluation and fully executes. (Now it should be Thread_b's turn)
-Thread_e executes the "Wait printout" (just a debugging feature that tells me when a thread is waiting) and then the program hangs.
It looks to me like I haven't set up wait correctly. The program actually worked (or appeared to be) correctly until I added in the sysout. Any ideas what's going on here?
To answer my own question,
This was covered in this thread.
The problem is that notifyAll() notify all of that object's threads that are waiting. Because I was calling wait() on each thread, the notifyAll() from another thread wasn't notifying any of the other threads.
As suggested by the linked post, I created a static synchronized object and called the wait and notify methods on that object. The resulting code looked like this:
private static final Object LOCK = new Object();
public static void addToResults(ResultsEntry newResult, int turn)
{
Integer resultsCount = newResult.getCount();
char resultsTarget = newResult.getTarget();
Thread thread = Thread.currentThread();
/*
* Turn number is compared to the size of the results array to control the
* order of execution.
*/
synchronized (LOCK){
while (turn != results.size()){
try {
System.out.println(thread.getName() + " is waiting");
LOCK.wait();
} catch (InterruptedException e) {}
}
System.out.println(thread.getName() + " is adding " + newResult);
SharedResults input = new SharedResults(resultsCount, resultsTarget);
System.out.println("Cumulative Results are " + results);
LOCK.notifyAll();
}
}
Thanks to all who commented!
So I have been trying to sort this out for a couple of hours now and I'm sure its something really simple or just a simple mistake i am missing but i have a three class program, control, account and MyThreads.
Im trying to have multipule threads(cards) modify the single account, i'm trying to use a monitor so only one thread can make changes at a time, this is not what i have archived I have somehow just allowed the one thread to access the account class and no others, they seem to just disappear, i assume they are just all on wait but refuse to wake up... any help before i implode?
account code:
class account{
private static int value = 0;
private static int cards = 0;
private static int count = 0;
private static int lock = 0;
public void setValue(int temp){
value = temp;
}
public int getValue(){
// while(lock == 1){
// try{
// wait();
// }catch (InterruptedException e){
// }
// }
return value;
}
synchronized public void withdraw(int temp, String tempID){
while(lock == 1){
try{
wait();
}catch (InterruptedException e){}
}
lock=1;
value= value - temp;
count++;
System.out.println(count + "(" + tempID +")"+" "+temp+" - "+value);
lock = 0;
this.notifyAll();
}
synchronized public void deposit(int temp, String tempID){
while(lock == 1){
try{
wait();
}catch (InterruptedException e){}
}
lock=1;
value= value + temp;
count++;
System.out.println(count + "(" + tempID +")"+" - "+temp+" "+value);
lock = 0;
this.notifyAll();
}
public void setCards(int temp){
cards = temp;
}
public int getCards(){
return cards;
}
public int getCount(){
return count;
}
}
control code:
class control{
public static void main(String [] args){
account acc = new account();
acc.setValue(1000);
acc.setCards(5);
// if(args.length > 0){
// try{
// int tempCards = Integer.parseInt(args[0]);
//
// }catch (NumberFormatException e) {
// System.err.println("Number of Cards : " + args[0] + " must be an integer.");
// System.exit(1);
// }
// try{
// int tempVal = 0;
// tempVal = Integer.parseInt(args[1]);
// acc.setValue(tempVal);
// }catch (NumberFormatException e) {
// System.err.println("Account Value : " + args[1] + " must be an integer.");
// System.exit(1);
// }
// }else{
// System.err.println("No values found, please start program with the number of Cards and Bank Account Value, both in integer format");
// System.exit(1);
// }
System.out.println("Transaction Withdrawal Deposit Balance");
System.out.println(" " + acc.getValue());
for(int i=0; i<=((acc.getCards())-1); i++){
new MyThreads(Integer.toString(i+1));
}
}
}
MyThreads code:
class MyThreads implements Runnable{
private String ID;
private Thread t;
account acc = new account();
MyThreads(String tempID){
ID = tempID;
t = new Thread(this, ID);
t.start();
}
public void run(){
try{
for (int i = 0; i < 20; i++){
if(Math.random()>0.5){
int tempW = 0;
tempW = ((int)(Math.random()*100));
acc.withdraw(tempW, this.ID);
//System.out.println(acc.getCount() + "(" + this.ID +")"+" "+tempW+" -"+acc.getValue());
}else{
int tempD = 0;
tempD = ((int)(Math.random()*100));
acc.deposit(tempD, this.ID);
//System.out.println(acc.getCount() + "(" + this.ID +")"+" "+" - "+tempD+" "+acc.getValue());
}
t.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Thread " + ID + " interrupted.");
}
System.out.println("Thread " + ID + " exiting.");
}
}
I know its a mess, forgive me im lazy.
Have a look at the definition of a Monitor in Java. In your code, you use the keyword synchronized for two methods, which are the same as:
public void XX(){
lock.lock(); // lock is a private variable
try {
// code here
} finally {
lock.unlock();
}
}
In short, It is a shorthand for explicit locking and will prevent multiple threads to access the methods concurrently.
So, just remove the lock part (i.e. the while(lock==1) block) inside your synchronized methods and it will work. Also, if in other codes you need a real lock, use the Lock class, not an integer.
For more information, there are a lot of good introduction to multithreading on the web, for example this one.
Your question, and thus answer, is a wonderful mixture of static synchronized and wait-notify that's neve being called. Why use static? sounds like a magic word? skip static and make life easier.
Also note that a wait-notify is related to a specific object; if wait-notify are related to different objects they will not communicate. Have a single object that they all synchronize around.
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("------------------------------");
}
}
I am new to java concurrency and working with locks.
I am trying to solve the dining problem and I don't have deadlocks yet only one thread gets actual runtime.
Can anyone tell me what am I doing wrong?
class Philosopher implements Runnable {
private Random numGenerator = new Random();
private int id;
private ChineseStick minChopstick;
private ChineseStick maxChopstick;
public Philosopher (int id, ChineseStick leftChopstick, ChineseStick rightChopstick) {
this.id = id;
if(leftChopstick.getNumber() > rightChopstick.getNumber()){
this.minChopstick = rightChopstick;
this.maxChopstick = leftChopstick;
}
else{
this.minChopstick = leftChopstick;
this.maxChopstick = rightChopstick;
}
}
/**
* Repeatedly think, pick up chopsticks, eat and put down chopsticks
*/
public void run() {
try {
while (true) {
pickUpLeftChopstick();
pickUpRightChopstick();
eat();
putDownChopsticks();
think();
}
} catch (InterruptedException e) {
System.out.println("Philosopher " + id + " was interrupted.\n");
}
catch(Exception e){
System.out.println("Philosopher " + id + " raised exceptions.\n");
}
}
/**
* Lets a random amount of time pass to model thinking.
* #throws InterruptedException
*/
private void think() throws InterruptedException {
System.out.println("Philosopher " + id + " is thinking.\n");
System.out.flush();
Thread.sleep (numGenerator.nextInt(10));
}
/**
* Locks the left chopstick to signify that this philosopher is holding it
* #throws InterruptedException
*/
private void pickUpLeftChopstick() throws InterruptedException {
while(!minChopstick.lock.tryLock()){
synchronized(minChopstick.lock){
minChopstick.lock.wait();
}
}
minChopstick.lock.lock();
System.out.println("Philosopher " + id + " is holding " + this.minChopstick.getNumber() + " chopstick.\n");
System.out.flush();
}
/**
* Locks the right chopstick to signify that this philosopher is holding it
* #throws InterruptedException
*/
private void pickUpRightChopstick() throws InterruptedException {
while(!maxChopstick.lock.tryLock()){
synchronized(maxChopstick.lock){
maxChopstick.lock.wait();
}
}
maxChopstick.lock.lock();
System.out.println("Philosopher " + id + " is holding " + this.maxChopstick.getNumber() + " chopstick.\n");
System.out.flush();
}
/**
* Lets a random amount of time pass to model eating.
* #throws InterruptedException
*/
private void eat() throws InterruptedException {
System.out.println("Philosopher " + id + " is eating.\n");
System.out.flush();
Thread.sleep (numGenerator.nextInt(10));
}
/**
* Releases the locks on both chopsticks to model putting them down so the
* other philosophers can use them.
*/
private void putDownChopsticks() {
minChopstick.lock.unlock();
synchronized(minChopstick.lock){
minChopstick.lock.notifyAll();
}
maxChopstick.lock.unlock();
synchronized(maxChopstick.lock){
maxChopstick.lock.notifyAll();
}
}
}
The stick class:
public class ChineseStick {
public ChineseStick(int number){
this.lock = new ReentrantLock();
this.number = number;
}
public Lock lock;
private int number;
public int getNumber(){
return this.number;
}
}
The Main is very simple:
public static void main (String[] args) {
// Model each chopstick with a lock
//Lock[] chopsticks = new ReentrantLock[NUM_PHILOSOPHERS];
ChineseStick[] sticks = new ChineseStick[NUM_PHILOSOPHERS];
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sticks[i] = new ChineseStick(i);
}
// Create the philosophers and start each running in its own thread.
Philosopher[] philosophers = new Philosopher[NUM_PHILOSOPHERS];
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
philosophers[i] = new Philosopher(i, sticks[i], sticks[(i+1)%NUM_PHILOSOPHERS]);
new Thread(philosophers[i]).start();
}
}
You are trying to get the lock inside this loop: while(!maxChopstick.lock.tryLock()) …
This loop ends when tryLock returns true which implies that you have the lock. But you invoke lock additionally after the loop thus have locked twice. So a single unlock invocation does not release the lock then.
But your code contains other problems as well. You are swapping the left and right sticks in the Philosopher’s constructor based on the stick numbers which makes no sense as the main method already provides the stick instances correctly for a round table. It is correct for the last person to have a left item with a higher number than the right item as you wrap-around the array combining the last item with the first one. Swapping them then breaks what was initially correct.
Once you have fixed that, you will have the same problem as explained in this answer: If all eaters grab the left stick first, you can reach the situation that all eaters have locked their left stick and no one can proceed.
Additional coding style odds: you have a ChineseStick class but instead of providing a grab operation there, you implement it in the Philosopher with duplicate code for left and right sticks. If ChineseStick had a single pickUp() method, the Philosopher needed only invoke minChopstick.pickUp() and maxChopstick.pickUp() respectively rather than implementing two pickUp…Chopstick() methods containing the same code.
And you are mixing up Lock and synchronized. This is a perfect way to create unmaintainable, confusing code.
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.