|
-
September 16th, 2008, 12:15 PM
#1
Thread pausing
Hi,
I would like to measure how much time would be required when 2 threads are suspended by using wait() method and wake them up by using notify() method.
But I get different nanoseconds everytime I run, so must be something wrong.
Is anybody see what I made a mistakes?
Last edited by mcbi; September 22nd, 2008 at 11:09 AM.
-
September 16th, 2008, 12:41 PM
#2
Re: Thread pausing
But I get different nanoseconds everytime I run, so must be something wrong.
The first thing that is wrong is this code doesn't even compile so it can't be the code you are running.
As for the different times, this is quite normal (assuming they are not widly different) as there probably are many threads/processes running on your computer as well the threads you are creating. Therefore, if one or more of these other threads runs during the time your threads are stopped it will effect the timing.
-
September 16th, 2008, 12:51 PM
#3
Re: Thread pausing
String name; // name of thread
First off, you can call myThread.getName(); instead of having to do this.
I would like to measure how much time would be required when 2 threads are suspended by using wait() method and wake them up by using notify() method.
To be honest, I am not sure exactly what you are trying to measure. The test doesn't seem like it really measures anything viable or useful. Can you elaborate on what you are trying to accomplish?
As for the different times, this is quite normal (assuming they are not widly different) as there probably are many threads/processes running on your computer as well the threads you are creating. Therefore, if one or more of these other threads runs during the time your threads are stopped it will effect the timing.
Exactly. No two processes will EVER take exactly the same amount of time to complete. Well, if they do then it is just a random occurrence.
-
September 16th, 2008, 02:08 PM
#4
Re: Thread pausing
Thanks you for your replies.
What I would like to measure is,
1. Create 2(or more) threads first
2. Make them in wait status by using wait() method
3. wake them up by using nofigy() method
4. Now, would like to measure the time of step3 that how long it will take to wake them up by using nofify() method
I am kind of new for using thread, so I just trying to make some linew but seems not working.
Can anyone change the code of what I wrote and explain the way to measure the time?
Thanks in advance.
Last edited by mcbi; September 16th, 2008 at 03:58 PM.
-
September 16th, 2008, 02:50 PM
#5
Re: Thread pausing
Part of the problem with the way you have designed the code is that all you do is call the wait() method in a try/catch block. All this does is release any locks and exit the current execution of the thread. It is possible once you start introducing more than one thread into the mix that they will NOT wake up in the order that you think they will. It is possible that thread _a_ will be woken up before any notify method is ever called from thread _b_.
Also, you are calling thread.wait and thread.resume (your methods) from main instead of performing those actions from within the actual run() method of the individual threads. You should really start up each thread and do what you need to do inside of their individual run methods. You can introduce static variables that can store the time from the individual threads.
-
September 16th, 2008, 04:48 PM
#6
Re: Thread pausing
Where are ct1 and ct2 defined?
Do you have code that compiles? That we can see?
Not sure how this code works. The wait and notify are not executed on the same object.
Last edited by Norm; September 16th, 2008 at 04:51 PM.
Norm
-
September 16th, 2008, 05:05 PM
#7
Re: Thread pausing
CreateThread.java
public class CreateThread implements Runnable{
String name; // name of thread
boolean pausingFlag;
//Thread t;
CreateThread(String threadname) {
name = threadname;
Thread t = new Thread(this, name);
System.out.println("New thread: " + t);
pausingFlag = false;
t.start(); // Start the thread
}
synchronized void threadwait(){
pausingFlag = true;
//wait();
System.out.println("thread wait() called");
}
synchronized void threadresume(String msg){
pausingFlag = false;
notify();
System.out.println(msg+ " resumed");
}
public void run(){
threadwait();
threadresume("");
}
}
---------------------
PausingExecution .java
public class PausingExecution {
public static void main(String args[]){
CreateThread ct1 = new CreateThread("Thread-1"); // start threads
CreateThread ct2 = new CreateThread("Thread-2"); // start threads
long startTime, endTime ;
startTime = System.nanoTime();
ct1.threadwait();
System.out.println("Thread-0 suspend");
ct2.threadwait();
System.out.println("Thread-1 suspend");
ct1.threadresume("Thread-0");
ct2.threadresume("Thread-1");
System.out.println("Threads are resumed");
endTime = System.nanoTime();
System.out.println("startTime is " + startTime);
System.out.println("endTime is " + endTime);
long elapsedInNanoSeconds = endTime - startTime ;
System.out.println("The programme was executed in " + elapsedInNanoSeconds + " nanoseconds.");
//}
}
}
=> THis code works without error. But not correctly works obviously...
-
September 17th, 2008, 04:50 AM
#8
Re: Thread pausing
You haven't explained why you want to do this measurement, but you should be aware that Sun recommend that you use the Executor services in the concurrent package instead of threads and wait/notify, etc. This is because the new package makes managing thread concurrency very safe and simple, whereas concurrency with 'raw' threads is extremely difficult to handle safely and correctly.
Controlling complexity is the essence of computer programming...
B. Kernighan
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 17th, 2008, 07:07 AM
#9
Re: Thread pausing
I know java provide different ways of doing this kind of jobs, but I would like to get some datas so that I can compare even though it's not really meaningful.
Well, wait() and notify() are also the basic methods that java provides for pausing a thread's execution and relese.
Can anyone tell me how to simply measure these methods?
-
September 17th, 2008, 07:29 AM
#10
Re: Thread pausing
Not sure what you are trying to measure. Or what meaning the results will be.
You need to add some debug code to your program to see which thread is doing what. Add: Thread.currentThread() to the println()s so you can see which thread is calling wait and notify. And add some more printlns before as well as after the wait and notify to see when and what.
Also your naming is inconsistant. You use three different names -0, -1 and -2
Have you tried timing a task run in a single thread and then split the task into parts and run the parts in separate threads to see if it takes longer to do as separate threads than as a single thread?
Last edited by Norm; September 17th, 2008 at 07:33 AM.
Norm
-
September 17th, 2008, 08:02 AM
#11
Re: Thread pausing
If you want to measure the execution time of those thread-related methods, you need to be really sure of what you're doing, because it's not as simple as it may appear. Even Java gurus argue about the validity of code timings. The time taken for a small piece of code to run varies with processor loading, OS caching, JVM caching, time of day, hair color, etc., etc. At the very least, you should take the average of as many repeated runs as possible (e.g. time how long it takes to run the code a million times and divide by a million). In practice, you are unlikely to learn anything useful from trying to measure the performance of individual methods of this kind.
If you are concerned about the performance of these methods for some practical reason, write a test-rig/simulation of the code you will be using in practice and measure its overall performance with real data over as many runs as possible. Then try rewriting the code with the recommended concurrency package classes and measuring its performance.
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil...
D. Knuth
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 17th, 2008, 08:06 AM
#12
Re: Thread pausing
 Originally Posted by mcbi
I would like to get some datas so that I can compare even though it's not really meaningful.
I just noticed this... You want to waste time comparing meaningless data?
Good luck
Premature optimization is the root of all evil in programming...
C.A.R. Hoare
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 19th, 2008, 09:34 PM
#13
Re: Thread pausing
may be it because of the spead of your computer.
----------------------------------------
www.codeuu.com
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|