Running a Java callback in a new thread

In this project, a Manager executes an event queue, and a callback is used to return the result of the event (the callback does not extend to Runnable ). The manager is launched in a separate thread, sending events. As soon as events are completed, the same thread calls back callbacks. This means that the next event will not be dispatched until the callback to the previous event ends. To avoid this, I would like the manager to create a new thread for each callback and make callbacks. How good is the solution in terms of design, and is there a better way to achieve this?

+3
source share
2 answers

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<Long> 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>{ 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 your preferred choice.
  • Or you can pass the Callaback object to the Callable/Runnable object. Or you can create a Callback object inside a Callable/Runnable . In my example, I explicitly passed the Callback object to the Callable .
  • Before returning the result, the Callable object calls the Callback method. If you want to block further actions, if you do not receive a response from the current event, just uncomment the lines below.

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

I think you are avoiding this, and therefore keep reading above the line. You do not need to create a new thread to call the Callback method. If you want to handle the Callback event asynchronously, you can create another ExecutorService and dispatch the event.

+5
source

I would have a thread that does the task, and also execute a callback. Instead of creating a thread every time, I suggest you use the ExecutorService.

 public static <T> void submit(ExecutorService service, Callable<T> callable, Consumer<T> callback) { service.submit(() -> { try { callback.accept(callable.call()); } catch (Throwable t) { // log the Throwable } }); } 
+2
source

Source: https://habr.com/ru/post/1269441/


All Articles