This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Threads and Synchronization and the fly likes my thread and synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "my thread and synchronized " Watch "my thread and synchronized " New topic

my thread and synchronized

jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
I have a threadded server application.

Once my server accepts a connection, a thread is started to work on the client data, while the server returns to accept futher connections.

This is working pretty well...

until yesterday, that is.

I needed to modify my "worker" thread. It now has included a class that is not threaded and needs to invoke one of that classes non-threadded methods.

Something like this:

Now the problem I'm seeing is that myOtherCLass.doSomeWork is being inundated with "clientData" from the multitude of worker threads.

I thought that all I needed to do was to modify the doSomeWork method
from this:

to this:

But this has had no affect.

Any suggestions?
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209

Is there any particular reason why you care if the static method is being invoked by many threads?. As long as the method is operating on the variables local to that method, it should be fine. Do you feel that the method is not thread safe and hence you want to make it synchronized?

I mean the fact that the method is being accessed by multiple threads simultaneously s'dn't bother you as long as it is thread safe.
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by jefff willis:

You must keep in mind that local variables are thread safe, while static and instance fields are not.


blog -
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
doSomeWork() will update some statistics on the screen.

Depending on which thread invokes doSomeWork(), different stats, and different graphics are updated on a panel.

The problem has been solved since I posted my original note. I was so convinced that the porblem had to deal with synchronizing my method that I overlooked proplems elsewhere in my code.

Thanks, for the input.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: my thread and synchronized