Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to stabilize direct communication between threads in java?

We have a system in which each thread (there can be dozens of them) works as an individual agent. It has its own inner variables and objects, and it monitors other threads' objects as well as its own) in order to make decisions. Unfortunately the system is deadlocking quite often.

Going through java tutorial (http://download.oracle.com/javase/tutorial/essential/concurrency/index.html) and through other topics here at stackoverflow, I managed to avoid some of these deadlocks by synchronizing the methods and using a monitor, as in:

Producer->monitor->Consumer.

However, not all communication between threads can be modeled like this. As I've mentioned before, at a given time one thread must have access to the objects (variables, lists, etc) of the other threads. The way it's being done now is that each thread has a list with pointers to every other thread, forming a network. By looping through this list, one thread can read all the information it needs from all the others. Even though there is no writing involved (there shouldn't be any problems with data corruption), it still deadlocks.

My question is: is there an already known way for dealing with this sort of problem? A standard pattern such as the monitor solution? Please let me know if the question needs more explanation and I'll edit the post.

Thank you in advance!

-Edit----

After getting these answers I studied more about java.concurrency and also the actor model. At the moment the problem seems to be fixed by using a reentrant lock:

http://download.oracle.com/javase/tutorial/essential/concurrency/newlocks.html

Since it can back out from an attempt to acquire the locks, it doesn't seem to have the problem of waiting forever for the them. I also started implementing an alternate version following the actor model since it seems to be an interesting solution to this case.

My main mistakes were:

-Blindly trusting synchronize

-When in the tutorial they say "the lock is on the object" what they actually mean is the whole object running the thread (in my case), not the object I would like to access.

Thank you all for the help!

like image 843
Kirus Avatar asked Dec 17 '22 10:12

Kirus


2 Answers

Look at higher-level concurrency constructs such as the java.util.concurrent package and the Akka framework/library. Synchronizing and locking manually is a guaranteed way to fail with threads in Java.

like image 82
Ryan Stewart Avatar answered Dec 18 '22 23:12

Ryan Stewart


I would recommend to apply Actor model here (kind of share nothing parallelism model).

Using this model means that all your thread don't interrupt each other explicitely and you don't need to do any synchronization at all.

Instead of making synchronization you'll use messages. When one Actor (thread) needs to get info about another Actor, it just asynchronously send a correspondent message to that Actor.

Each Actor can also respond to messages of certain types. So, when a new message comes, Actor analyses it and sends a response (or does any other activity). The key point here is that processing of incoming messages is being done synchronously (i.e. it's the only point where you need the simplest way of synchronization - just mark the method which processes messages with synchronized modifier).

like image 34
Roman Avatar answered Dec 18 '22 23:12

Roman