randomafk
randomafk

Reputation: 783

Multithreading java

I'm trying to figure out how to multithread in java. Right now, my program works fine with no concurrency but I want to implement multithreading to help speed it along.

The program runs several objects of a separate sub class and 50% of the time evaluation for each of those objects is spent in a process which only utilizes one core rather than the 8 available. These objects are completely independent of one another until but are used as inputs in the program.

I am trying to multithread this by having the subclass implement Runnable and then have my program use a thread for each such object. Would this be the correct way?

However, how are threads in java handeled? Would I need to dispose of the threads after each run? How does join work?

thanks

Upvotes: 3

Views: 797

Answers (4)

EleetGeek
EleetGeek

Reputation: 121

This is a good way to start multithreading.

public class ThreadExample {
    public static void main(String[] args) {
        //Main thread
        System.out.println("Main thread");
        new Thread(new Runnable() {
            @Override
            public void run() {
                //This thread is independent of the main thread
                System.out.println("Inner Thread");
            }
        }).start();
    }
}

Upvotes: 0

Esben Skov Pedersen
Esben Skov Pedersen

Reputation: 4517

Take a look at http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html

The constructor takes the number of threads you want. In this case the same as your number of cores.

ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(8);
List<Future> futures = new ArrayList<Future>();
foreach(...something...)
    futures.add(s.submit(new MyCallable()));
foreach(Future f : futures)
    f.get(); // Result of computation
System.out.println("Done");

Upvotes: 3

djna
djna

Reputation: 55897

You're pretty much on track. You'll create a Thread object

 Runnable r = new MyClassImplementingRunnable();
 Thread t =  new Thread(p);
 t.start();
 t.join(); // wait for thread to die

The Thread object is garbage collected like any other object, the thread itself dies when the run method completes. The key thing is that your Runnable's run method really must guarantee to return, your design cannot depend on being able to kill thread from the outside.

If you are going to have lots of threads you need to wait for them all to finish, so you can either keep a collection of the threads you've started and then use t.join( smallNumberOfMillis) to see which of them has finished. That's a little inefficient so there are other techniques for allowing threads to communicate with each other, I'd suggest reading this article about them.

@denis also mentions that the Executor and related classes provides a nicer abstraction above Threads. If you have an interest in learning the background then manually managing Threads is interesting. If you just want to get the job done, follow Denis' suggestion.

Upvotes: 5

denis.solonenko
denis.solonenko

Reputation: 11765

Don't manage threads manually, take a look at executors and thread pools in java

Upvotes: 7

Related Questions