aspose file tools
The moose likes Java in General and the fly likes how do i call a method simultaneously using thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "how do i call a method simultaneously using thread" Watch "how do i call a method simultaneously using thread" New topic
Author

how do i call a method simultaneously using thread

ashish kulkarni
Ranch Hand

Joined: Aug 15, 2002
Posts: 130
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


A$HI$H
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

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.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
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.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
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

Joined: Aug 05, 2001
Posts: 2545
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?

karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
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
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 965

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


Please ignore post, I have no idea what I am talking about.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 965

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: how do i call a method simultaneously using thread
 
Similar Threads
Using throws IOException
Starting to open the connection at the beginning of the class
when extend a class
Static Inheritance
Error message