2

What's the correct way to create and use threads in a loop?

If I want to process a million items I'll create a loop to do that. So, for efficiency's sake I'll make it multi-threaded and allocate each item to a thread. Let's say I create five threads to handle this. How do I allocate the work to the threads?

What if one thread has a smaller item to process so then it's free to process another – how can I allocate the next item in the loop to that thread?

Do I create the threads outside of the loop and then use them within?

Here's an example I'm working on – it's missing the creation of object to work with and only uses two threads, but I think people here are smart enough to know what I mean :)

public class App 
{
    public static void main( String[] args )
    {
        App a = new App();
        a.doTest();
    }

    private void doTest() {
        Count c = new Count(21);
        Count c2 = new Count(7);
        Thread t = new Thread(c);
        Thread t2 = new Thread(c2);
        t.start();
        t2.start();
        for (int f = 0; f < 10; f++) {
                //here - how do I select which thread to send the work to?
                // ?????.processThis(nextObject);          //how do I send this to the right thread (one that is idle or has least work?)
        }

    }

    public class Count implements Runnable {

        private int count;

        public void processThis(Object someItemToProcess) {
            //here I'll set the object to process and call the method to process it    
        }

        public Count(int count) {
            this.count = count;
        }

        @Override
        public void run() {


            //for illustration purposes
//            for (int i = 1; i < count; i++) {
//                System.out.println("Thread " + Thread.currentThread().getId()  + " Count = " + i);
//            }

        }
    }
}
Andrew Lygin
  • 6,077
  • 1
  • 32
  • 37
Ken Alton
  • 686
  • 1
  • 9
  • 21
  • Did you try learning? Read a book? Tutorial? Watch a video? Did you see any example of using threads? What was the solution there?? – Amit Sep 04 '16 at 18:36
  • Hi Almitey - yes, I tried learning I'm in the process of doing so but all tutorials/videos etc. I've seen speak about creating a thread to count to 10 or concurrency issues rather than actual allocation of work. – Ken Alton Sep 04 '16 at 18:58
  • Sounds like you know the answer to my question and find it rather trivial so could you please answer it or refrain from further critical comments? Thanks! – Ken Alton Sep 04 '16 at 18:59
  • Related useful question: http://stackoverflow.com/questions/33596079/how-to-properly-use-java-executor/33596230#33596230 – Ravindra babu Sep 05 '16 at 10:40

1 Answers1

5

Such problems are usually solved by means of thread pools to which you feed your tasks and wait for the completion. In Java, there're several implementations of thread pools, you can start your learning with the one of a fixed size:

// Create a thread pool
ExecutorService executor = Executors.newFixedThreadPool(threadsCount);

// Submit your tasks to the pool:
for (Count c: myCounts) {
    executor.submit(c);
}

// Shutdown the pool when you don't need it
executor.shutdown();

The pool will take care about executing your tasks in parallel. If you need to control the execution of the tasks, to wait for their completion, to cancel them, to obtain the results of their work etc, just use Future objects returned by the submit() method. You can use the pool as long as you need it, but don't forget to shut it down when you don't need it anymore.

Learn more about various Executor implementations and their capabilities to be able to find the one that fits your requirements the best way.

Andrew Lygin
  • 6,077
  • 1
  • 32
  • 37
  • 1
    Upvoted :) Just a quick reminder that in Java 8 has been introduced an enhancement of Future: CompletableFuture which allows a simpler and much powerful handling of asynchronous job synchronization. – Uluaiv Sep 05 '16 at 12:24