Tom Jenkinson
Tom Jenkinson

Reputation: 1470

Resuming a thread and passing in an object in Java

I'm relatively new to threads in java. I was wondering if the following is possible.

Thanks!

Upvotes: 0

Views: 107

Answers (4)

Martín Schonaker
Martín Schonaker

Reputation: 7305

I think that what you're looking for is java.util.Callable and java.util.Executors.

This is the shortest code that I could write was:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestCallable {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        ExecutorService executor = Executors.newSingleThreadExecutor();
        try {
            Integer five = executor.submit(new Callable<Integer>() {

                @Override
                public Integer call() throws Exception {
                    ExecutorService executor = Executors.newSingleThreadExecutor();
                    try {
                        return executor.submit(new Callable<Integer>() {

                            @Override
                            public Integer call() throws Exception {
                                return 5;
                            }

                        }).get();
                    } finally {
                        executor.shutdown();
                    }
                }

            }).get();

            System.out.println(five); // prints 5

        } finally {
            executor.shutdown();
        }
    }
}

Upvotes: 0

Boris the Spider
Boris the Spider

Reputation: 61158

There are many ways to do this.

This following example is usually the best way to accomplish the tasks. It does some work in the main thread then passes a Callable to an ExecutorService which does some work in another thread. The call to future.get blocks until the second thread is done and returns the Object that the Callable returns.

private static final ExecutorService executorService = Executors.newFixedThreadPool(1);

public static void main(String[] args) {
    //do some stuff       
    final Future<Object> future = executorService.submit(new MyOtherWork());
    final Object object;
    try {
        object = future.get();
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    } catch (ExecutionException ex) {
        //handle exception
    }       
    //do some other stuff

}

private static class MyOtherWork implements Callable<Object> {

    public Object call() throws Exception {
        //do stuff in another thread
    }
}

Note that as the ExecutorService uses non daemon threads by default your application will not exit until it is shut down.

Upvotes: 2

flup
flup

Reputation: 27104

How about a SynchronousQueue that thread a is waiting to take() from and thread b put()s its result on?

Synchronous queues are similar to rendezvous channels used in CSP and Ada. They are well suited for handoff designs, in which an object running in one thread must sync up with an object running in another thread in order to hand it some information, event, or task.

If you want to add timeouts, let thread a poll() for the result and thread b offer() it.

Upvotes: 0

cosmin.danisor
cosmin.danisor

Reputation: 963

If you want to make your life easier with Threads in java don't use wait(), notify() you will have to use synchronized methods and statements, my suggestion is to use a ReentrantLock and Condition that you pass from the main thread to the secondary threads

Here you can read more about it

Upvotes: 1

Related Questions