|
|
Java Threading Question
|
|
|
|
Mac Enthusiast
Join Date: Jan 2007
Status:
Offline
|
|
I am writing a pretty simple program that runs multiple calculations each in their own thread.
Code:
// run threads in reverse (starting with calcu
while (completedThreads < totalThreads) {
if ((activeThreads < concurrentThreads) && (threadLevel > 0)) {
calculations[threadLevel - 1].start();
threadLevel--;
activeThreads++;
} else {
try {
// wait((long)1000);
} catch (java.lang.InterruptedException exception) {
}
}
}
The code works fine. However, since each calculation takes several seconds/minutes to run, I want to execute the above if loop every second or so and not continuously, though uncommenting the line gives me the following error.
Code:
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at BoxCount.calculate(BoxCount.java:211)
I've also tried sleep(long), this.sleep(), this.wait() without any success. Any ideas?
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status:
Offline
|
|
Is BoxCount a GUI object?
You should create an array of work and then start n threads who don't finish till the array is empty/complete each thread looping while theres items left in the array. You'll need a Timer to periodically poll the array to update the status in the gui.
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jan 2007
Status:
Offline
|
|
BoxCount is the main program. It contains the main function.
I'm not terribly concerned with the GUI being updated periodically. And I basically have an array of work ("calculations" in the code above, which is each a Thread object).
Could I make a Timer object which calls the above posted code every second?
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Feb 2004
Status:
Offline
|
|
Your code looks very odd and there are most likely better ways to implement it what you're trying. Anyways, to answer your question: Use either Thread.sleep(long) which is equivalent to Thread.currentThread().sleep(long) or use the wait-method: synchronized(this) { wait(long); } (Read also the JavaDoc, I think it says clearly you must hold a lock on the Object on which you're calling wait()).
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jan 2007
Status:
Offline
|
|
What is odd about my code?
I have between 100 and 200 calculation threads that need to be run by this app. And that value can change greatly depending on the dataset. Launching all the calculations at once would be a bad thing. I've found that 8 concurrent threads works well for most computers as far as speed/efficiency.
Is there an elegant way to do what I need?
After each calculation finishes, it reports back and the activeThreads-- and completedThread++. What is a better way to limit the number of concurrent threads?
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status:
Offline
|
|
MaxThreads = CPUCores + Disk-io ? 1 : 0;
But you don't have 4 or 8 threads you have 200 and you start them 8 at a time.
You should create an n lenght array of data and pass n / threads of them to each thread. That way you minimize the amount of threads created and context switches between threads.
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jan 2007
Status:
Offline
|
|
Thanks for the suggestion. I see what you are saying.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Aug 2001
Status:
Offline
|
|
Probably my all time favorite threading pattern is a threadsafe queue of tasks, with a pool of worker threads popping things off of it until it's empty. Doesn't apply to everything, but when it does it's sooooo easy.
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jan 2007
Status:
Offline
|
|
Originally Posted by Catfish_Man
Probably my all time favorite threading pattern is a threadsafe queue of tasks, with a pool of worker threads popping things off of it until it's empty. Doesn't apply to everything, but when it does it's sooooo easy.
This approach would probably work better for my program. At least easier to implement. Thanks guys!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|