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?
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)
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 ?
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.
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 ... ...
BEE MBA PMP SCJP-6
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop