Java – Concurrency: how do I use incoming and outgoing queues to implement an executable?

It is well known that ThreadPoolExecutor uses some blockingqueues as queues for incoming tasks What I want is for the ThreadPoolExecutor to prepare a second queue for the task result I want to use this queue as a source for input / output services that send or store these results

Why should I create a separate queue? Because I want to separate the sending of results from the action of obtaining results In addition, I don't think any exceptions and delays accompanying I / O operations should affect the ThreadPoolExecutor I'm calculating the result

I have created some naive implementations I'd like to make some criticism Perhaps, can it be implemented with more out of the box Java classes? I use Java 7

public class ThreadPoolWithResultQueue {
    interface Callback<T> {
        void complete(T t);
    }
    public abstract static class CallbackTask<T> implements Runnable {
        private final Callback callback;   
        CallbackTask(Callback callback) {
            this.callback = callback;
        }    
        public abstract T execute();   
        final public void run() {
            T t = execute();
            callback.complete(t);
        }
    }   
    public static class CallBackTaskString extends CallbackTask<String> {
        public CallBackTaskString(Callback callback) {
            super(callback);
        }
        @Override
        public String execute() {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
            return hashCode() + "-" + System.currentTimeMillis();
        }
    }    
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
        final BlockingQueue<String> resultQueue = new LinkedBlockingQueue<String>();
        Callback<String> addToQueueCallback = new Callback<String>() {
            @Override
            public void complete(String s) {
                System.out.println("Adding Result To Queue " + s);
                resultQueue.add(s); //adding to outgoing queue. some other executor (or same one?) will process it
            }
        };
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3,5,1000l,TimeUnit.DAYS,workQueue);
        for (int i = 0; i <= 5; i++) {
            executor.submit(new CallBackTaskString(addToQueueCallback));
        };
        System.out.println("All submitted.");
        executor.shutdown();
        executor.awaitTermination(10l,TimeUnit.SECONDS);
        System.out.println("Result queue size " + resultQueue.size());
    }
}

Solution

To make kinf a library component, you have to wrap things up

You can extend the thread pool executor, which has many methods to intercept submitted tasks, so you can queue things to the queue passed in the constructor

This is basically executorcompletionservice, but you can allow users to insert into a queue instead of displaying as a queue

Otherwise, this is a typical agent for a task Fair work

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>