aspose file tools*
The moose likes Threads and Synchronization and the fly likes looking for help about using multiple threads to process several objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "looking for help about using multiple threads to process several objects" Watch "looking for help about using multiple threads to process several objects" New topic
Author

looking for help about using multiple threads to process several objects

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 550
Hi
Thank you for reading my post
I have a question about threading , i now how to initiate a thread and use it , but what i do not know is :
I have an array of objects (i am not allowd to change those objects class structure) i want to use several threads to process all this objects instead of using main method of application.

what i can not understand is :
how to pass my object to thread to process it ? should i implements Runnable interface ?

secondly :

Imagine that i create 5 threads to process these objects so each thread can proccess one object at a time and if i have 250 objects each thread should process 50 objects.

how i can understand when i thread finishes its job ?
should i create a secondary array that indicate that each object of main array is procceed or not ?
how i can understand that all my objects are gone under process ?

Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Question 1: there are lots of ways to do it, but one easy to understand thing is to store the array in a member variable of the Runnable and initialize it in the constructor:



Second question:

Again, there are lots of ways to do it, but here's one: imagine you want to create ten threads, each of which should process 50 of the objects. Add constructor arguments and member variables to the class above to hold the index of the first and last element to process. Then create the ten threads and store them in an array:

Thread[] threads = new Thread[10];
for (int i=0; i<500; i+=50) {
threads[i/50] = new Thread(new MyRunnable(theArray, i, i+50);
threads[i/50].start();
}
[/code]

Now, you want to know when all the threads are done, right? Imagine that each run() method returns when all 50 elements are processed. So what you want is for all 10 run() methods to return. The Thread.join() method waits for run() to return, so that's perfect. You just write another loop to call join() on each thread; when the loop completes, all 500 elements have been processed.


[Jess in Action][AskingGoodQuestions]
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 550
Hi
Thank you for all hints.
I used your method and it looks to be OK , but i can not test it because
I implements the Runnable interface in my thread class , and now my thread objects (which i create them from that class which implements the runnable interface) does not have a join() method .

maybe i am mistaken i tried to use
mythread[i].join() and it looks that those objects has no join method , so i can not find when all threads finished.

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: looking for help about using multiple threads to process several objects
 
Similar Threads
Are local objects thread-safe?
MultiThreading and Sharing Resources
Thread to return a value
How to correctly use a fixed size thread pool?
volatile and static keywords