• Post Reply Bookmark Topic Watch Topic
  • New Topic

Current thread now owner  RSS feed

 
Nikhil Jain
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell me what is wrong in this code. I am trying to implement producer consumer problem.



[ Editied to use code tags - Paul Sturrock ]
[ June 19, 2007: Message edited by: Paul Sturrock ]
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell me what is wrong in this code. I am trying to implement producer consumer problem.


Well, there are actually two *major* problems with this code.

The first is the synchronization. When you have producers and consumers trying to send notifications to each other, you need to make sure that notifications are not lost. If you look at your code, the while loop is actually outside of synchronization. This means that there are two points where you give up the lock. If the notification is sent while the thread is in the wait(), then fine. However, if all the consumers are at the top/bottom of the loop, where it is not synchronized, then that notification from the producer will be lost.

The second problem, and the reason you are getting the "current thread not owner" problem, is the object you are synchronizing on. Synchronization is with objects, not references. You have to synchronize on the same object. And autoboxing uses different objects when you increment / decrement an integer.

Henry
[ June 19, 2007: Message edited by: Henry Wong ]
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are also two *minor* problems with the code.

First, you are only checking to see if the buffer is within range *before* you wait(). You need to do that after the wait too -- as it is possible to get notifications for conditions that have already been taken. This can be solved by changing the "if" to a "while".

Second, you are using the lock to wait for 2 possible condtions -- this is why you have to use notifyAll(), as you can't control who will get the notification. This is incredibly wasteful. I would recommnend using the ReentrantLock class instead, as it can have two different condition variables bound to it -- one for each waiting condition. This will allow you to wakeup only one thread to resolve the condition.

Henry
 
Nikhil Jain
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,

Could you please give me example of sync block. I could not get sync. part. I though of I was using syn block on the object?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shashank Tilwalli:

Could you please give me example of sync block. I could not get sync. part. I though of I was using syn block on the object?


Not sure what you are asking -- which of the 4 issues that I referred to are you asking about? .... Now, assuming you mean the second issue...

It is related to this code:



Specifically this line...



Is done with autoboxing. It translates to this:



This changes the value of the sharedVariable reference to a different object, which means that different threads are synchronizing on different objects, which means that they are not thread safe.

Henry
[ June 19, 2007: Message edited by: Henry Wong ]
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I'm currently in a good mood...

Here is your program, with all four of the issues that I mentioned, addressed...

Henry

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!