java


Thread condition never wakes


I have a thread that when it runs, it creates a Message object which is put into a linked list in a monitor. At the same time, two other threads wait for this list to get a Message object in it, in a while(linkedlist.isEmpty()) canGetMessage.await(); loop, but even though I'm sending a canGetMessage.signalAll(); command when putting the object into the list, the two other threads never wake.
public void deliverMessage(Message m){
lock.lock();
try{
linkedlist.add(m);
canGetMessage.signalAll();
}finally{
lock.unlock();
}
}
public Message getMessage(){
lock.lock();
try{
while(linkedlist.isEmpty()){
canGetMessage.await();
}
return linkedlist.remove(); //returns the item that has been in the list the longest
}
catch (InterruptedException e) { } //not required to handle these
finally{
lock.unlock();
}
I have println to see when it gets out of the while loop, but this never happens, and I don't know why...
Edit: Just to specify, the lock I'm using is a ReentrantLock
Guessing; you are having problems here:
lock.lock();
In case sender/receiver use the same lock object, this can't work. Receivers acquire the lock; and will not release it until the sender has added a new entry to the list.
But the sender can't get to that part; because the sender tries to lock on the already locked lock. So the locked lock will block.
The "correct" answer is to use a BlockingQueue to connect sender / receivers - instead of re-inventing something "like a queue". See here for more material how to use BlockingQueue.
My apologies to everyone, it turns out the issue lay in the creation of the threads themselves, I had inadvertently been making separate monitors for each thread instead of them all using the same one...

Related Links

mysql odbc executeBatch takes long time
D3 not taking the database values when i call it for 2nd time
Does Java have a `::` operator? [duplicate]
Can't run GAE Shiro demo source code out of the box
REST - how to handle needless requests?
How to send an SMS programmatically using java?
JDK 8 Memory Layout and garbage collection
URL decoding in Java for non-ASCII characters
Add node to xml file with properties through java
What is the result of these type conversions and why?
Catching the wrong exception
Understanding applets
how to avoid warning for the Base 64?
How to get InputStream back from String
java.lang.NoClassDefFoundError: javax/ejb/EJBObject
What is the easiest way to sort an ArrayList of strings

Categories

HOME
gitlab
listview
gerrit
drivers
pycharm
google-play
smarty
iot
dot
spagobi
xmpp
webpack-2
in-app-purchase
podio
elasticsearch-hadoop
nstableview
gz
clearcase-ucm
text-rendering
predix
cloudhub
web-sql
pass-by-reference
autosys
icloud-api
cas
underflow
django-storage
subdomains
tasklet
frame
language-concepts
http-digest
gesture
android-browser
hilbert-curve
jna
opshub
greenrobot-eventbus
particles.js
scrollable
filepicker
jquery-validate
clean-architecture
powercli
jxcore
forever
font-size
media-player
namecoin
cudafy.net
bind9
django-scheduler
webdriverjs
watchconnectivity
time-and-attendance
medium.com
database-optimization
blackberry-10
yaws
ios4
goose
xpath-1.0
energy
browser-link
cartesian-product
metaclass
browser-bugs
event-bubbling
mono-embedding
angular-local-storage
geonetwork
ng-animate
android-imagebutton
asp.net-web-api-odata
onactivityresult
uitouch
odata4j
bulkloader
flash-builder4.5
html4
chronoforms
sharp-repository
drools-planner
jelly
buildr
pendrive
work-stealing
pyinotify
iphone-web-app
mongomapper
perfect-hash
firefox4
project-hosting
thread-local-storage
gacutil
.net-1.0
misv

Resources

Encrypt Message