Results 1 to 5 of 5
- 03-24-2012, 07:17 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Threads synchronization with wait() & notify() problem
Hello there,
I could really appreciate if any of you could help me with this piece of code. I cannot figure out the error. I'm trying to fix it since hours, but I guess I am just too stucked in and I could really use some help.
I have something like a producer - consumer structure. You cannot consume, unless it was previously produced. Therefore, I have used 2 synchronized threads in order to fullfill this requirement.
I also have some brief code comments, in order to be more easy to understand.
Your help will be highly appreciated. Thank you :)Java Code:import java.util.ArrayList; public class Test { public static void main(String[] args){ Buffer b = new Buffer(); Producer pro = new Producer(b); Consumer c = new Consumer(b); Consumer c2 = new Consumer(b); Thread t1 = new Thread(pro); t1.start(); c.start(); c2.start(); } } /*Producer class.I generate inside a infinite loop numbers of type double and they are added into a Buffer object (see below Buffer class), using put method, each 1 second.*/ class Producer implements Runnable { private Buffer bf; private Thread thread; Producer(Buffer bf) { this.bf=bf; } public void start() { if (thread==null) { thread = new Thread(this); thread.start(); } } public void run() { while (true) { bf.put((double)(Math.random())); //add in buffer. System.out.println("Written in buffer."); try {Thread.sleep(1000);}catch(Exception e){} //add in buffer each 1 second. } } } //Consumer class. Inside a infinite loop we read elements of type Buffer. class Consumer extends Thread { private Buffer bf; Consumer(Buffer bf) { this.bf=bf; } public void run() { while (true) { System.out.println("Read from buffer: "+this+" >> "+bf.get()); } } } class Buffer { ArrayList content;// we use it in order to store double elements. /*The method put() - used to add elements in content. The method get() - used to extract elements from content. Those are synchronized methods - when we put a number in buffer (put()) - we can perform the method get() (which was previously on wait). */ synchronized void put(double d) { content.add(new Double(d)); notify(); } synchronized double get() { double d=-1; try { if(content.isEmpty()) wait(); // the buffer is empty - get() is on hold. d = (((Double)content.get(0))).doubleValue(); content.remove(0); // the element was removed from the buffer. }catch(Exception e){e.printStackTrace();} return d; } }Last edited by Norm; 03-24-2012 at 09:03 PM. Reason: added code tags
- 03-24-2012, 09:05 PM #2
Re: Threads synchronization with wait() & notify() problem
Can you explain what the error is?I cannot figure out the error.
Copy and paste here the debug print outs from when you execute the code that shows the execution flow and sequences of events.If you don't understand my response, don't ignore it, ask a question.
- 03-24-2012, 10:49 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Re: Threads synchronization with wait() & notify() problem
Thank you, Norm, for wanting to help me. I have already corrected my error:
I have used ArrayList<Double> content = new ArrayList<Double>() insetad of ArrayList content = new ArrayList(); and also, I' ve replaced double d=-1; with Double d = new Double(-1); and I just made the small modifications that derive from this and everything works just fine now :)
Thanks. Have a good day :)
- 03-24-2012, 10:52 PM #4
Re: Threads synchronization with wait() & notify() problem
That was an easy one.
- 03-25-2012, 10:44 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 41
- Rep Power
- 0
Re: Threads synchronization with wait() & notify() problem
class Consumer extends thread
//---------------------------------- public void run is used only when Runnable Interface is implemented , run() from class Thread simply returns alike sleep(Xnsec); from a costructor in thread
//---------------------------------- or if you say this.run() in the Consumer constructor. However, if you want to say this(); as the first line of the constructor for Consumer then this.run()
//---------------------------------- should apply the overridden custom run() method without ambiguity.
public void run()
{
while (true)
{
System.out.println("Read from buffer: "+this+" >> "+bf.get());
}
}
}
Similar Threads
-
Wait() and Notify()
By SiX in forum New To JavaReplies: 15Last Post: 07-28-2011, 04:29 PM -
wait() and notify()
By jomypgeorge in forum New To JavaReplies: 4Last Post: 02-15-2011, 08:58 AM -
Need help with wait and notify
By mityay in forum Threads and SynchronizationReplies: 3Last Post: 01-06-2011, 04:24 PM -
Need help with wait() and notify()
By Mkaveli in forum Threads and SynchronizationReplies: 2Last Post: 03-30-2010, 11:58 AM -
Wait() notify() implementation
By georges in forum Advanced JavaReplies: 4Last Post: 02-05-2010, 01:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks