• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Client background threads & thread safety

 
rhya smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have always heard that you put possible long running tasks in a background thread to keep the GUI responsive. However, now you can have multiple competing threads started, and you need to think about thread safety. I think I am spiraling into a black hole, but here it goes.

The specifics are:
I have a class, ResultModel, that extends AbstractTableModel and is responsible for the data displayed in a JTable. This class has a Runnable which is started when the user requests a search on a set of criteria. This Runnable clears the results from a previous search, tells any listeners that he is starting a search, issues the search request and gets new results, tells any listeners he has finished searching, and processes the new results.

The problem is that in doing these steps my Runnable accesses and manipulates several data members of the enclosing class, ResultModel. When I look within ResultModel, I see several other methods that are accessing and manipulation these same data members. To data protect these members, I started making these methods synchronized as well as having a synchronized block within the run method of my Runnable. The problem is that pretty soon most every method in the class is synchronized. This seems to defeat the objective of having a responsive GUI, which was the purpose of the Runnable in the first place.

I am thinking that when multiple threads can access a data member you need to synchronize around everywhere this member is being used. Is this the correct way to think about making things thread safe?

An alternative to making the methods synchronized is to put sensitive data manipulation or access in a synchronized block with the data member object as the lock. However, there are 4 such objects and that seems to ask for deadlock issues.

Any advice?

Thanks.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Rhya -

You may want to delete one of your duplicate posts.

Thanks.
 
rhya smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I am new here. I tried the delete, but I don't seem to have permission.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic