• Post Reply Bookmark Topic Watch Topic
  • New Topic

how do i call a method simultaneously using thread  RSS feed

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a static method in a class which i need to test if i have to synchronize it or not,
so i want to write a code to call this method at the same time to see how it behaves,
Can any one provide some code for it
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a static method in a class which i need to test if i have to synchronize it or not,
so i want to write a code to call this method at the same time to see how it behaves,
Can any one provide some code for it

Here it is:

In this example, the incrementCounter() method does need to be synchronized.
Before you do any testing, you should do some analysis to determine what you need to synchronize.
Eugene.
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ashish kulkarni:
so i want to write a code to call this method at the same time to see how it behaves,
Can any one provide some code for it

Eugene's code is certainly fine. But I am not sure you can write a code to call a synchronized method at the same time for multiple times, since you can't exactly control the progress of a thread, at least in theory. Only thing close I can think of is that maybe you can use two real time signals. But I doubt that is what you want.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don wrote:
Only thing close I can think of is that maybe you can use two real time signals.
That sounds interesting Don. Can you please explain in more detail?
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean you can connect two external signals to two serial ports.
A while back, I have done some work on a firework controling program. What the program does is: people control the sequency and timing of different firework in a show.
So when people plan the show, they can listen to the firework background music and use mice to record timing he feel a firework should be fired. After he done all the fireworks, he can save the entire sequency into a file, so later on, people can just play the file through a computer loaded with this program in a real firework show.
The control is done through sending signal through serial ports to external device to ignite the firework. So I think, on the reverse order, an external signal can be received by computer program. If we can send two external signals at the same time to a program, maybe we can simulate the calling of synchronized method. Otherwise, how can we call the same method from two threads at the same time?
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ashish kulkarni:
Hi,
I have a static method in a class which i need to test if i have to synchronize it or not,
so i want to write a code to call this method at the same time to see how it behaves,
Can any one provide some code for it

So the requirement is: 'write a code to call this method at the same time', in the code below, assume incrementCounter() is the synchrosized method. Then in first for loop, 5 client were created. Each client goes through 2nd for loop, within each cycle, incrementCounter() is called.
There are plenty of calls to incrementCounter(), but there is no guaranttee that two calls are made at the exactly same time. Is there?

 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

don:
i dont see how two external signals can change anything.

you should not test if some method has to be synchronized (by try'n'error). analyse the method and then this should tell you if you would need to sync it.
testing later will (probably) tell you if your analysis was correct.
k
 
Ranch Hand
Posts: 1050
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with karl, you are going about this the wrong way. You don't write code to test if some thing needs to be synchronized. You analyze the task to make the decision. If you have a method which will be called by more than one thread, and there are non-local variables, than make it synchronized. IMHO
 
William Barnes
Ranch Hand
Posts: 1050
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the book Java Threads by Oaks & Wong concerning performance issues of synchronization:

...it is easyer to solve performance problems whe they occur than to find bugs caused by a lack of data synchronization.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!