开发者

Threads going into deadlock despite synchronized keyword

开发者 https://www.devze.com 2023-03-11 17:01 出处:网络
I tried to make a event dispatcher in Java that will dispatch events as threads. So all the EventListener classes are essentially implemented the Runnable class. Like how firing of events work traditi

I tried to make a event dispatcher in Java that will dispatch events as threads. So all the EventListener classes are essentially implemented the Runnable class. Like how firing of events work traditionally, a method in the event dispatcher class loops through a list of EventListeners and then invoke their handler method, except that this time, I invoke these handler as threads by putting the listeners into new Thread(handlerObject).start(). The actual handling is done in the run() method in the EventListener.

So it looks something like:

for(EventListener listener : listenerList) {
   if(listener instanceof Runnable)
      new Thread(listener).start();
}

So all instructions to handle the event in the liste开发者_开发技巧ner are put inside the run() method, which will be executed when the thread.start().

But the problem is the threads often go into a situation where one of the threads got stuck somewhere and didn't manage to continue. Sometimes, several threads may also get stuck while some managed to run through all instructions in the run() method in the listener. I looked up and this sounds like what it is called a deadlock.

I tried to put the "synchronized" modifier to all my methods but it still has this problem. I thought the synchronized keyword would simply just queue any threads trying to run a similar method until a current thread running the method has finished. But this doesn't solve the problem still. Why doesn't synchronized solve the problem especially when I already have it on all my methods and it should queue any concurrent access that may potentially cause a deadlock? I didn't use any wait() or notify() methods. Just a simple event dispatcher that attempts to run its event listener as a thread.

I am pretty new to threads but have found it very difficult to even debug it because I don't know where has gone wrong.

Thanks for any help.


Deadlock is something along the lines of this:

A needs iron to make tools, asks B for iron
B needs tools to make iron, asks A for tools

Neither will complete. Just because you've put the syncronized key word around them does not guarantee that you're going to run into a logical impossibility. You have to judge when one thing will be able to move forward and when it won't.


Never just add synchronized to all methods, this solves nothing - you will effectively make your program single-threaded.

When you think you have a deadlock, you can take a thread dump and analyze the output to understand what each thread is executing, which locks (if any) they are holding, what locks they are waiting for, etc.

Unfortunately without specific code or understanding the actual synchronization going on in your application, the only advice that can be given is general like this.


I don't know what you mean by 'deadlock despite synchronized keyword'. The `synchronized' keyword doesn't prevent deadlocks. It can cause them, if you have two threads that acquire locks in different orders. Solution: don't.


Your real problem is that you don't understand concurrency well enough to understand why your program is not working, let alone how to solve this. (FWIW - adding synchronized to all of your methods is only making the problem worse.)

I think that your best plan is take time out to do some reading on concurrency in Java. Here are a couple of good references:

  • The Java Concurrency Tutorial Stream.
  • Java Concurrency in Practice by Brian Goetz et al.

@wheaties has a micro-explanation of what a deadlock is, and @matt_b offers useful advice on how to diagnose a deadlock. However, these won't help a lot unless you know the right way to design and write your multi-threaded code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号