aspose file tools*
The moose likes Threads and Synchronization and the fly likes If I don't need simultanius threads of the same object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "If I don Watch "If I don New topic
Author

If I don't need simultanius threads of the same object?

Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

Hello folks,

I am new to thread programming.

I have an object that implements Runnable and I create a new thread with this object by pressing a button on my Swing form. The thread does some stuff in the background and then updates my Swing form through the AWT Event Queue. I don't ever need to create multiple threads that run simultaneously, only one at a time. If I click the button multiple times very fast it will of course create multiple threads and it causes some problems with unsynchronized updates. So I am thinking I can lock the button and wait for the thread to finish before it can be pressed again or prevent new threads from being created until the previous ones are done.

I really don't need to created multiple threads that wait for one another, so locking the button would be fine.

Should I implement both in case later I don't want to lock the button? Is locking a button acceptable or is it bad programming practice?

Thanks!

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2052
    
  22

You can use Single Threaded pool

Creating a SIngle threaded pool will create a single thread that waits for runnables to appear on a queue. You submit your runnable to the queue, and it will execute it and go back to wait for the next runnable.
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

Thanks Jayesh!

It works, but I still had to synchronize some methods to prevent the same NPE, but what I have is that each SingleThreadExecutor thread invokes the AWT Event Queue with a new runnable that access the same ArrayList to read values as the SingleThreadExecutor thread, which it is invoked from, writes too.

So it appears as soon as the SingleThreadExecutor calls the AWT Event Queue, which is the last call in the thread, the thread is considered done. I wasn't taking that into consideration.
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

I wonder if I should use Collections.synchronizedList instead of synchronizing the methods that access the ArrayList? Is there a performance difference?
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2052
    
  22

If your thread is writing to your array list and event handler is reading from it, you will have to protect the list. Event handler is called on an event queue thread that is started By java.

Remember that synchronized list will only synchronize on individual method calls, not across calls. So, if for example, you are iterating over list, you will need to synchronize yourself. Also, remember that the list won't protect the objects inside the list. So, if your background thread gets an object from the list and starts modifying it, the synchronized list won't protect you because the call to the objects will be outside the list's methods. You will have to protect those calls yourself

A little advanced concepts in case you are worried about contention between the 2 threads: depending on how long you lock the list, you might run into problems where the UI freezes while the runnable is executing.

What are you doing in the background thread? If your runnable is very light, like let's say it only adds an element to the list, you might want to take a look at copyonwritearraylist. It makes a copy of the list every time you modify it. It's been a while since I've used it, but I think it will work well if number of changes to the list are much lower than number of times the list is read

OTH, if you build the list completely in your runnable, then consider double buffering. Keep one list for reading; awt event queue will use it. In your runnable create a new list and populate it. After you populate it, simply switch the 2 lists; the list that your runnable created becomes the list that awt event queue will use. This way you will only need to put a lock when you switch the lists, not when you are operating on the list. This reduces the amount of contention to almost nothing.
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

I am going through a list of words in the background thread and using regex to match all cases in a text file of the words, then highlighting them. It is very inefficient for this I know (O(n^2) ), and it takes about 30 or 40 seconds to do a text file the size of a book like "Through the Looking Glass", but for now I am doing it this way and then I will switch to using a hash table and my own string matching so it is fast. Anyway, as I match the words, I update a counter and I update an ArrayList that holds the indexes for the highlighting. When done, I invoke the AWT Event Queue with a worker that updates the Swing form with the count and paints the highlighting on the JTextArea text using the index ArrayList for each start and stop point.

I was thinking of using 2 lists, but I think just synchronizing the 2 read & write methods is efficient. I actually don't even need the executor and I have no NPEs if I just synchronize the 2 methods for now. I may get into a situation later where I want to make a copy.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If I don't need simultanius threads of the same object?
 
Similar Threads
URLyBird locking mechanism
Spring batch: write as many files as the result from the data base
Using == for String Equality?
Partial documents with JAXB?
multiple action listeners .setActionCommand Vs. addActionListener(new comboListListener())