We have a system in which each thread (maybe tens of them) works as a separate agent. It has its own internal variables and objects, and it controls the objects of other threads, as well as its own) to make decisions. Unfortunately, the system quite often comes to a standstill.
Looking through the java tutorial (http://download.oracle.com/javase/tutorial/essential/ concurrency / index.html) and through other topics here in stackoverflow, I managed to avoid some of these deadlocks by synchronizing the methods and using the monitor, how in:
Producer-> Monitor-> Consumer.
However, not all communications between threads can be modeled this way. As I mentioned earlier, at the moment, one thread must have access to the objects (variables, lists, etc.) of other threads. The way this is done now is that each thread has a list with pointers to each other thread, forming a network. By going through this list, one thread can read all the information it needs from all the others. Despite the lack of a record (there should not be any problems with data corruption), it still blocks.
My question is: is there already a known way to solve this problem? A standard template such as a monitor solution? Please let me know if the question needs a more detailed explanation and I will edit the message.
Thank you in advance!
-Edit ----
After receiving these answers, I studied more about java.concurrency, as well as 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 he may back away from trying to acquire locks, he seems to have no problem waiting for them forever. I also began to introduce an alternative version, following the actor’s model, as this seems like an interesting solution for this case.
My main mistakes were:
-Enable Trusted Sync
-When the textbook says that “the lock is on the object”, what they really mean is the whole object that executes the thread (in my case), and not the object that I would like to access.
Thank you all for your help!