| | |
Threading problemos - yet again! (almost done though)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2008
Posts: 38
Reputation:
Solved Threads: 0
Hey there good folks! 
I am working on a thread task called, Lab Alarm, and I am nearly done with it. We're suppoosed to work with the "producer-consumer" problem and It's just teacher code that one is supposed to modify and add some own to it. Now, I am almost done with this thing - but I keep getting some errors - which I don't fully comprehend. If anyone could shed a little light over it, and what next step I should take, I would be grateful.
The lab consists of 7 classes - they are not big ones though - and I will post the links to them right here. Any help is welcome. Best wishes to everyone!
The Task: http://pastebin.com/m2be4c209
LabAlarm: http://pastebin.com/m145e30c4
AlarmBuffer: http://pastebin.com/m46757e06
AlarmClient: http://pastebin.com/m3c686f6
AlarmConsumer: http://pastebin.com/m1ecf2a7a
AlarmMessage: http://pastebin.com/m1ddc4861
AlarmProducer: http://pastebin.com/m2133e47d
AlarmServer: http://pastebin.com/m4ec4f42f

I am working on a thread task called, Lab Alarm, and I am nearly done with it. We're suppoosed to work with the "producer-consumer" problem and It's just teacher code that one is supposed to modify and add some own to it. Now, I am almost done with this thing - but I keep getting some errors - which I don't fully comprehend. If anyone could shed a little light over it, and what next step I should take, I would be grateful.

The lab consists of 7 classes - they are not big ones though - and I will post the links to them right here. Any help is welcome. Best wishes to everyone!
The Task: http://pastebin.com/m2be4c209
LabAlarm: http://pastebin.com/m145e30c4
AlarmBuffer: http://pastebin.com/m46757e06
AlarmClient: http://pastebin.com/m3c686f6
AlarmConsumer: http://pastebin.com/m1ecf2a7a
AlarmMessage: http://pastebin.com/m1ddc4861
AlarmProducer: http://pastebin.com/m2133e47d
AlarmServer: http://pastebin.com/m4ec4f42f
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
All fields inside classes private, non static.
My concept of view to the architecture of program.
I don't sure you happy, if I post whoole code.
I'm not sure if it would be correct in the learning process.
My concept of view to the architecture of program.
I don't sure you happy, if I post whoole code.
I'm not sure if it would be correct in the learning process.
Java Syntax (Toggle Plain Text)
package ops023_lab01_uppgift_jc3; import java.awt.Point; public class LabAlarm { //make sure that they have a common buffer AlarmBuffer to write and read data static AlarmBuffer ab = new AlarmBuffer(); // static AlarmClient client1 = new AlarmClient("client1", new Point(600, 100)); static AlarmClient client2 = new AlarmClient("client2", new Point(600, 200)); static AlarmClient client3 = new AlarmClient("client3", new Point(600, 300)); static AlarmClient client4 = new AlarmClient("client4", new Point(600, 400)); // static AlarmProducer producer1 = new AlarmProducer("producer1", ab, client1); static AlarmProducer producer2 = new AlarmProducer("producer2", ab, client2); static AlarmProducer producer3 = new AlarmProducer("producer3", ab, client3); //version with more then one registered clients static AlarmProducer producer4 = new AlarmProducer("producer4", ab, client1, client2, client3, client4); // static AlarmConsumer consumer1 = new AlarmConsumer("consumer1", new Point(10, 0), ab); static AlarmConsumer consumer2 = new AlarmConsumer("consumer2", new Point(10, 400), ab); public LabAlarm() { Thread runnableProducer1 = new Thread(producer1); Thread runnableProducer2 = new Thread(producer2); Thread runnableProducer3 = new Thread(producer3); Thread runnableProducer4 = new Thread(producer4); // Thread runnableConsumer1 = new Thread(consumer1); Thread runnableConsumer2 = new Thread(consumer2); runnableConsumer1.start(); runnableConsumer2.start(); // runnableProducer1.start(); runnableProducer2.start(); runnableProducer3.start(); runnableProducer4.start(); } public static void main(String[] args) { new LabAlarm(); } }
Java Syntax (Toggle Plain Text)
package ops023_lab01_uppgift_jc3; //class for a Buffer in the producer consumer problem //look in lecture slides for hint on how to implement it public class AlarmBuffer { private AlarmMessage message; private boolean empty; public void TheBuffer() { System.out.println("TheBuffer "); this.empty = true; } public synchronized void insert(AlarmMessage message) { System.out.println("i1");//for test while (!empty) { try { System.out.println("i2"); wait(); System.out.println("i3"); } catch (InterruptedException e) { } }//end of while-loop this.message = message; this.empty = false; System.out.println("i4"); notify(); System.out.println("i5"); }//end of insert-method // who uses this ? public synchronized AlarmMessage remove() { System.out.println("r1"); while (empty) { try { System.out.println("r2"); wait(); System.out.println("r3"); } catch (InterruptedException e) { } }//end of while-loop this.empty = true; System.out.println("r4"); notify(); System.out.println("r5"); return message; }//end of remove-method //diagnostic public synchronized boolean isEmpty(String who) {//3. System.out.println(who + " ask-->AlarmBuffer: isEmpty() " + empty + "\n"); return empty; } public synchronized boolean isEmpty() { return empty; } }//end of class
Java Syntax (Toggle Plain Text)
package ops023_lab01_uppgift_jc3; public class AlarmMessage { private String fromClient; private String message; private long alarmTime; AlarmMessage(String name, String messageText) { fromClient = name; message = messageText; alarmTime = System.currentTimeMillis(); } @Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append("[fromClient: "); sb.append(fromClient); sb.append(",message: "); sb.append(message); sb.append(",alarmTime: "); sb.append(alarmTime); sb.append("]\n"); return sb.toString(); } }
•
•
Join Date: Aug 2008
Posts: 38
Reputation:
Solved Threads: 0
Heya,
I redid the coding and I got it working. The problems I encounter now is that on the AlarmServer GUI - it doesn't show the correct client number when printed on the GUI. And, in AlarmConsumer (Line 42) - I don't know how I would make it so that the values in the buffer is correctly compared to an AlarmClient's message. Those two are the things that annoy me at the time, and I would like to know what I need to do?
Here's the new code:
LABALARM: http://pastebin.com/m71a2a1e7
ALARMBUFFER: http://pastebin.com/m4b820dc3
ALARMCONSUMER: http://pastebin.com/m1597cf76
ALARMPRODUCER: http://pastebin.com/m5d8f6b6
ALARMCLIENT: http://pastebin.com/m558010a1
ALARMSERVER: http://pastebin.com/mb144582
ALARMMESSAGE: http://pastebin.com/m7514dfaf
I redid the coding and I got it working. The problems I encounter now is that on the AlarmServer GUI - it doesn't show the correct client number when printed on the GUI. And, in AlarmConsumer (Line 42) - I don't know how I would make it so that the values in the buffer is correctly compared to an AlarmClient's message. Those two are the things that annoy me at the time, and I would like to know what I need to do?
Here's the new code:
LABALARM: http://pastebin.com/m71a2a1e7
ALARMBUFFER: http://pastebin.com/m4b820dc3
ALARMCONSUMER: http://pastebin.com/m1597cf76
ALARMPRODUCER: http://pastebin.com/m5d8f6b6
ALARMCLIENT: http://pastebin.com/m558010a1
ALARMSERVER: http://pastebin.com/mb144582
ALARMMESSAGE: http://pastebin.com/m7514dfaf
Last edited by Ajantis; Oct 1st, 2009 at 10:51 am. Reason: - Forgot to specify one thing :)
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
Flow:
Human --> AlarmClient
Human: (enter text, press button)
AlarmClient: on press button creates message, setups flag
AlarmProducer --> AlarmClient
AlarmProducer: checks AlarmClient is flag on? , if Yes - gets the message
AlarmProducer --> AlarmBuffer
AlarmProducer: inserts message into AlarmBuffer
AlarmBuffer: stores message, setups flag , sends signal notify()
------------------------------------------------------------------
AlarmConsumer --> AlarmBuffer
AlarmConsumer: checks AlarmBuffer isEmpty , if not empty - gets the message
AlarmBuffer: setups flag , sends signal notify(), returns message
AlarmConsumer --> AlarmServer
AlarmConsumer: sends message to AlarmServer
AlarmServer --> Human
AlarmServer: displays the message
Human: reads this message
Conclusion:
No - AlarmProducer producerX = new AlarmProducer(ab, as);
but - AlarmProducer producerX = new AlarmProducer(ab);
Make all fields private, for access to the fields, use the set() , get() , is() methods (public)
Compiler will show some errors, that you remove yourself, or you asking a question.
In first phase of develop let's only AlarmClient generate the message (instance of AlarmMessage). AlarmProducer,AlarmBuffer,AlarmConsumer,AlarmServer only transport this one to the another.
Later you can extend the message-format , to store for example: time , where the message is inside paricular parts of the chain.
Do not extend program if basic things not run properly.
Even if that part is over, you will need to carefully look at threads.(maybe use threadpool).
Human --> AlarmClient
Human: (enter text, press button)
AlarmClient: on press button creates message, setups flag
AlarmProducer --> AlarmClient
AlarmProducer: checks AlarmClient is flag on? , if Yes - gets the message
AlarmProducer --> AlarmBuffer
AlarmProducer: inserts message into AlarmBuffer
AlarmBuffer: stores message, setups flag , sends signal notify()
------------------------------------------------------------------
AlarmConsumer --> AlarmBuffer
AlarmConsumer: checks AlarmBuffer isEmpty , if not empty - gets the message
AlarmBuffer: setups flag , sends signal notify(), returns message
AlarmConsumer --> AlarmServer
AlarmConsumer: sends message to AlarmServer
AlarmServer --> Human
AlarmServer: displays the message
Human: reads this message
Conclusion:
No - AlarmProducer producerX = new AlarmProducer(ab, as);
but - AlarmProducer producerX = new AlarmProducer(ab);
Make all fields private, for access to the fields, use the set() , get() , is() methods (public)
Compiler will show some errors, that you remove yourself, or you asking a question.
In first phase of develop let's only AlarmClient generate the message (instance of AlarmMessage). AlarmProducer,AlarmBuffer,AlarmConsumer,AlarmServer only transport this one to the another.
Later you can extend the message-format , to store for example: time , where the message is inside paricular parts of the chain.
Do not extend program if basic things not run properly.
Even if that part is over, you will need to carefully look at threads.(maybe use threadpool).
![]() |
Similar Threads
- how would you apply threading to a web server application (C++)
- good threading tutorials (C++)
- Threading Issue (C#)
- pre-threading and forking (Linux Servers and Apache)
- Threading (multi threading) in Linux (C)
- Multi-threading with C++.NET (C++)
Other Threads in the Java Forum
- Previous Thread: Memory Game
- Next Thread: Help on Pesudorandom Java Code
| Thread Tools | Search this Thread |
.net ajax android apple applet apps array assignment automation bank binary browser bug byte c++ capture class classes code collections component consumer convert converter coordinates cypresssemiconductor database db design development developmenthelp display draw eclipse eclipsedevelopment economy electronics error event ext firefox fractal game google gui guidancer gxt hardware hql html image input integer intellijidea8 java javafx javascript jetbrains jsp julia laptop linux loop method microsoft myregfun mysql netbeans newbie news pcworld php poi printf problem producer programming projects property python repair search security set software sort state string sun swing technology threads time travel tree uk video warranty web windows






