Results 1 to 7 of 7
Thread: Multithreading-in-Java
- 01-19-2011, 01:53 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Multithreading-in-Java
Hello,
I am looking at second example in Page 9 - Multithreading in Java
and i don't get expected output:
Put: 0
Get: 0
Put: 1
Get: 1
Put: 2
Get: 2
Put: 3
Get: 3
Put: 4
Get: 4
I get
run:
Put: 0
Get: 0
Put: 1
Get: 1
Put: 2
Get: 2
Get: 2
Get: 2
Put: 3 (Force STOP)
or
run:
Put: 0
Get: 0
Get: 0
Get: 0
Get: 0
Get: 0
Put: 1
BUILD STOPPED (total time: 2 seconds)
Maybe my neatbeans 7 beta has something to do with this?
Java Code:class Queue { int exchangeValue; boolean busy = false; synchronized int get() { if (!busy) try { wait(); } catch (InterruptedException e) { System.out.println( "Get: InterruptedException"); } System.out.println("Get: " + exchangeValue); notify(); return exchangeValue; } synchronized void put (int exchangeValue) { if (busy) try { wait(); } catch (InterruptedException e) { System.out.println( "Put: InterruptedException"); } this.exchangeValue = exchangeValue; busy = true; System.out.println("Put: " + exchangeValue); notify(); } } class Publisher implements Runnable { Queue q; Publisher(Queue q) { this.q = q; new Thread (this, "Publisher").start(); } public void run() { for (int i = 0; i < 5; i++){ q.put(i); } } } class Consumer implements Runnable { Queue q; Consumer (Queue q) { this.q = q; new Thread (this, "Consumer").start(); } public void run() { for (int i = 0; i < 5; i++){ q.get(); } } } class Demo { public static void main(String args []) { Queue q = new Queue (); new Publisher (q); new Consumer (q); } }
- 01-19-2011, 02:48 PM #2
- 01-19-2011, 02:55 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
i get this:
Put: 0
Get: 0
Get: 0
Get: 0
Get: 0
Get: 0
Put: 1
BUILD STOPPED (total time: 3 seconds)
And i have to manually kill program. What IDE are you using? I will try uninstall 7 netbeans and install 6.9.1
- 01-19-2011, 03:07 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Your put() and get() methods aren't symmetrical: the busy variable should keep track whether or not a value has been put there (true); after it is read, the value should be false; change those two methods to:
kind regards,Java Code:synchronized int get() { if (!busy) try { wait(); } catch (InterruptedException e) { System.out.println("Get: InterruptedException"); } int exchangeValue= this.exchangeValue; busy= false; System.out.println("Get: " + exchangeValue); notify(); return exchangeValue; } synchronized void put (int exchangeValue) { if (busy) try { wait(); } catch (InterruptedException e) { System.out.println("Put: InterruptedException"); } this.exchangeValue = exchangeValue; busy = true; System.out.println("Put: " + exchangeValue); notify(); }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-19-2011, 03:08 PM #5
Sometime in code is deadlock.
Deadlock (The Java™ Tutorials > Essential Classes > Concurrency)Skype: petrarsentev
http://TrackStudio.com
- 01-19-2011, 03:20 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Thanks Josah,
everything works correctly now. Maybe the author of this example wanted to demonstrate one deadlock too :).
- 01-19-2011, 03:44 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
If you have copied that example verbatim it was definitely incorrect. As you know now it has nothing to do with NetBeans; it just ran your (incorrect) code. It wasn't deadlock either, that variable was never set to false so the producer assumed that it had to wait (forever).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Java Multithreading Problem
By avirit1983 in forum Threads and SynchronizationReplies: 3Last Post: 12-03-2010, 10:18 PM -
Multithreading Gui
By BUGSIE91 in forum Threads and SynchronizationReplies: 7Last Post: 10-13-2010, 02:20 PM -
Multithreading in java
By MuslimCoder in forum New To JavaReplies: 5Last Post: 02-20-2010, 08:16 PM -
multithreading
By shilpa.krishna in forum New To JavaReplies: 2Last Post: 06-27-2008, 04:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks