Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

my thread and synchronized

 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jefff willis:


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

./pope
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic