This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Use 2 Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Use 2 Threads" Watch "Use 2 Threads" New topic
Author

Use 2 Threads

Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hello Ranchers,

I am trying to improve the performance of iterating through a collection object. Lets assume there is a List<String> with 100 objects. I want to loop through it and do some operation based on the String object. How can I use 2 threads to do loop through the List object.



How can I use 2 threads to go through the list object using 2 threads?

Any help/guidance is appreciated.

Thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

For a list of size S, and assuming that the size of the list does not change during the process, one possibility would be to assign a thread to loop through 0 to S/2, and another thread to loop through S/2 to S. But first, do you understand how to create a single thread ? Try to make a thread which loop through the whole list. Once you've done that, tell the thread to loop from which index to which index. Then, you can make as many threads as you want.

You are calling methods a(), b() in your loop. Be careful that if you make this process multi-threaded, you may have concurrency issues coming in.You must be careful that this in a() won't interfere with things in b() (and vice-versa)


[My Blog]
All roads lead to JavaRanch
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Christophe,

Thanks for your reply. I tried to put your suggestion in code but was struck as there is no overloaded run() in Thread class.

This is the code that I wrote.



How can I make the result object to be printed by 2 threads?

Many Thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

This is a good start. There are different ways to make threads, one of which being to implement the Runnable interface. You decided to make your main class implement it. Why not ? Now you need to find a way to make the list accessible to both threads. In your code, you are making a new list for each thread. Can you figure out how ?
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
I think it synchronization is the way to share an object between multiple threads.

But this code doesn't print anything:



What do you think?

Thanks>
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

1. getList is still making a new list.
2. Make it step by step. Go back to the following code:

There are different ways to access a common list from different threads. One of them is to pass the reference of the list to the thread.

1. Make a new Runnable class and try to find a way to refer to the referList variable.
2. Change ThreadTest. Don't make it Runnable. In the main method, use the following class instead :
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Matt : You might also consider "Queue x = new LinkedList()" with 'x' shared by
any number of threads. Synchronize on 'x' and use x.peek() / x.poll() in each
output thread to pull an item to process.

Jim ... ...


BEE MBA PMP SCJP-6
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
@ Christophe

I am slightly confused. Do you mean I should put ProcessingThread class inside the main method of ThreadTest class?

I was looking at the API there is no overloaded method of run() that takes parameter. In such case, how can I pass the reference of the list object to the run method?

Thanks
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Typically you will pass shared objects (a queue) to the Runnable class when you initialize it, before
calling Thread.start(), something like, "new Thread(new MyRunnable(sharedStuff)).start()".

Jim ... ...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Jim explained well what I was trying to make you figure out

Matt, are you still stuck ?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Thanks Christophe. I should have studied your posts above more carefully.

Jim ... ...
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Thanks Jim and Christophe!

But I am still finding it difficult to print one list object using 2 threads. Based on Jim's suggestion, when making the instance of thread, I am passing the reference of the list object. But how can I pass the reference of the list object in the run method.

Here is my code:


Am I doing something wrong in here?

Thanks>
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Matt : Let's go back to the beginning. Now that you have studied the problem
and some suggestions, can you please describe again what your requirements
are - what you are trying to accomplish? Then we'll take another swing at it.

Jim ... ...
 
Consider Paul's rocket mass heater.
 
subject: Use 2 Threads
 
Similar Threads
How to retrieve metadata in ibatis
Colon : in for{} loop?
Struts with Javascript problem
Which are the differences between the three (for, forEach and While)?
From Array of Objects to HashMap