• Post Reply Bookmark Topic Watch Topic
  • New Topic

what to luse as mutex in synchronized block

 
Bucsie Dusca
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a thread that I need executed a number of times, in an app. after all threads are completed i want to display a dialog box saying that all threads have executed correctly.
so i have the MyThread class, with two static variables:

static int numberOfThreadsCompleted;
static int numberOfThreadsToComplete;

in my run() method at the end, i have this piece of code:

synchronized (this) {
numberOfThreadsCompleted++;
if (numberOfThreadsCompleted == numberOfThreadsToComplete) {
JOptionPane.showMessageDialog(mainFrame, "A number of "
+ numberOfThreadsToComplete
+ " threads have been executed");
}
}

in the listener of some button i have

MyThread.numberOfThreadsToComplete = x; (value from UI)
for(i=0;i<MyThread.numberOfThreadsToComplete;i++){
MyThread t = new MyThread();
t.start();
}


the question is: is "this" a proper mutex for the synchronized block?
my guess is no, because the application freezes; it displayes an empty JOptionPane and doesn't respond anymore

how should i choose this mutex?

thank you
 
Henry Wong
author
Sheriff
Posts: 22526
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to choosing a mutex is simple -- just make sure that the same object is used for all threads accessing the same data. Unfortunately, for this case, it will not work. The "this" variable is different for every MyThread object. It is up to you to choose a common object... but... historically, the class object is used to protect static variables. In this case, it would be the MyThread.class object.

Now... having stated this... I don't think that synchronization is the root cause to your problem. There are probably better Swing experts on this site, so correct me if I am mistaken... but I don't think the dialog stuff is thread safe, even for creation -- you may have to insure that the popup is called from the event dispatching thread. (see SwingUtilities class)

Hope this helps,
Henry
 
Bucsie Dusca
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

I used the .class as you said and it no longer freezes, but indeed there is another problem with the JOptionPane message displaying. I looked at SwingUtilities, but still I have no idea what to do to make it display correctly. usually the message isn't displayed at first try, but it is displayed at the second/third try. Any suggestions?

Never mind, I figured it out: code that has anything to do with UI should be "invokeLater"-ed, like this:
SwingUtilities.invokeLater(new Runnable(){
public void run(){
// code goes here: JOptionPane.show....();
}});
[ October 19, 2004: Message edited by: Diana Pop ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should take a look at http://spin.sf.net/ which is much easier to use IMHO.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spin still amaze me with its simplicity and high usability when talking to GUI applications.

./pope
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!