• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the best thread safty method in this case?

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
sorry for my bad English .
i will try to explain my problem .i have java application that suppose to be
Multithreaded it receive calls from the client side and returns answer back.
now i tried to put "Synchronized" on the first method the client side invoked and as a result the application kept to fail and client wasn't served as they suppose to be served. and the application become slow .
my application is build as this i will put here simple abstraction classes
to represent the general idea :



as i said i put "Synchronized" in the public static int Invoke() .
but it is no good .
my question is from your experience what is the best method in my case to handle this to prevent threading problem .
thanks allot .
[ September 05, 2008: Message edited by: Meir Yan ]
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as i said i put "Synchronized" in the public static int Invoke() .
but it is no good .
my question is from your experience what is the best method in my case to handle this to prevent threading problem .


There is no magical keyword that will convert a single threaded application to a multithreaded application -- in Java you just have tools that will help with problems, when you try to call thread unsafe methods in parallel.

You need to understand what doesn't work, why it doesn't work, and if it is caused by threads, used the right tools to fix it. There is no "add synchronized and we're done" here.

In your example....

Convert uses local variables only. It is thread safe.

For ProcessArgs, each thread has there own copy, and it uses local variables only. So, it is thread safe.

For API, again, it uses local variables only. So, it is thread safe.


For this example, there is nothing to do here. It will work in a threaded fashion as it is.

Henry
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the fast reply
well as i said this is only sample , not the real code the real code contains
a lot of sources .
i have question about the static methods in multi threaded application
is class using other classes static methods can it to cause threaded problem ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods present no special synchronization issues; it's only access to data that matters.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see thanks for the reply .
ok so i know i have some kind of problem in my code somewhere
that makes it slow and prevent clients to invoke methods om parallel mode
what will be the first thing to do to check threaded problem ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In any situation where you have a performance problem, the first thing to do is always to take measurements. Get a profiler, learn to use it, and run some tests. Find out where the time is going -- and then you'll know what to do.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much one last question.
do you know good tool that will help me profile the application ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic