• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use 2 Threads

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Matt Thomassan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ... ...
 
Matt Thomassan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim explained well what I was trying to make you figure out

Matt, are you still stuck ?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Christophe. I should have studied your posts above more carefully.

Jim ... ...
 
Matt Thomassan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ... ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic