Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Java Threading Question

Java Threading Question
Thread Tools
Warren Pease
Mac Enthusiast
Join Date: Jan 2007
Status: Offline
Reply With Quote
Feb 2, 2007, 03:44 PM
 
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?
     
BLAZE_MkIV
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Feb 2, 2007, 07:19 PM
 
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.
     
Warren Pease  (op)
Mac Enthusiast
Join Date: Jan 2007
Status: Offline
Reply With Quote
Feb 2, 2007, 11:39 PM
 
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?
     
bone666
Fresh-Faced Recruit
Join Date: Feb 2004
Status: Offline
Reply With Quote
Feb 4, 2007, 11:28 AM
 
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()).
     
Warren Pease  (op)
Mac Enthusiast
Join Date: Jan 2007
Status: Offline
Reply With Quote
Feb 5, 2007, 11:20 PM
 
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?
     
BLAZE_MkIV
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Feb 6, 2007, 09:33 PM
 
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.
     
Warren Pease  (op)
Mac Enthusiast
Join Date: Jan 2007
Status: Offline
Reply With Quote
Feb 7, 2007, 11:53 AM
 
Thanks for the suggestion. I see what you are saying.
     
Catfish_Man
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Feb 7, 2007, 12:16 PM
 
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.
     
Warren Pease  (op)
Mac Enthusiast
Join Date: Jan 2007
Status: Offline
Reply With Quote
Feb 7, 2007, 02:39 PM
 
Originally Posted by Catfish_Man View Post
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:29 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,