java 线程执行完后回调,在新线程上执行Java回调

On this project, a Manager performs event queuing, and to return the result of the event a callback is used (the callback does not extend Runnable). The manager runs on a separate thread, dispatching the events. Once the events terminate, this same thread calls the callbacks. This means that the next event will not be dispatched before the callback of the previous event terminates. In order to avoid this, I though about having the manager create a new thread for each callback, and executing the callbacks there. How good is this solution in terms of design practices, and is there a better way to achieve this?

解决方案

A simple Callback code:

import java.util.concurrent.*;

import java.util.*;

public class CallBackDemo{

public CallBackDemo(){

System.out.println("creating service");

ExecutorService service = Executors.newFixedThreadPool(10);

try{

for ( int i=0; i<10; i++){

Callback callback = new Callback(i+1);

MyCallable myCallable = new MyCallable((long)i+1,callback);

Future future = service.submit(myCallable);

//System.out.println("future status:"+future.get()+":"+future.isDone());

}

}catch(Exception err){

err.printStackTrace();

}

service.shutdown();

}

public static void main(String args[]){

CallBackDemo demo = new CallBackDemo();

}

}

class MyCallable implements Callable{

Long id = 0L;

Callback callback;

public MyCallable(Long val,Callback obj){

this.id = val;

this.callback = obj;

}

public Long call(){

//Add your business logic

System.out.println("Callable:"+id+":"+Thread.currentThread().getName());

callback.callbackMethod();

return id;

}

}

class Callback {

private int i;

public Callback(int i){

this.i = i;

}

public void callbackMethod(){

System.out.println("Call back:"+i);

// Add your business logic

}

}

output:

creating service

Callable:1:pool-1-thread-1

Call back:1

Callable:2:pool-1-thread-2

Call back:2

Callable:8:pool-1-thread-8

Call back:8

Callable:3:pool-1-thread-3

Call back:3

Callable:10:pool-1-thread-10

Callable:4:pool-1-thread-4

Call back:10

Callable:7:pool-1-thread-7

Call back:7

Callable:6:pool-1-thread-6

Call back:6

Callable:9:pool-1-thread-9

Callable:5:pool-1-thread-5

Call back:9

Call back:4

Call back:5

Summary:

Replace Manager with ExecutorService of your preferred choice.

Either your can pass Callaback object to Callable/Runnable object Or you can create Callback object inside Callable/Runnable. In my example, I have explicitly passed Callback object to Callable.

Before returning the result, Callable object invokes Callback method. If you want to block on proceeding further unless you get response from current event, just uncomment below line.

System.out.println("future status:"+future.get()+":"+future.isDone());

I think you are going to avoid it and hence keep above line commented. You don't have to create new thread for Callback method invocation. If you want to process Callback event asynchronously, you can create one more ExecutorService and submit the event.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值