1、示意图

AsyncDispatcher模拟.png

2、代码

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class AsyncDispatcher {

    private final BlockingQueue<TaskEventType> eventQueue;

    private Thread eventHandlingThread;

    private EventHandler handlerInstance = new GenericEventHandler();

    protected Map<TaskEventType, EventHandler> eventDispatchers;

    public AsyncDispatcher(BlockingQueue<TaskEventType> eventQueue) {
        this.eventQueue = eventQueue;
        this.eventDispatchers = new HashMap<TaskEventType, EventHandler>();
    }

    Runnable createThread() {
        return new Runnable() {
            @Override
            public void run() {
                while (true) {
                    TaskEventType event;
                    try {
                        event = eventQueue.take();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    dispatch(event);
                }
            }
        };
    }

    public void start() {
        eventHandlingThread = new Thread(createThread());
        eventHandlingThread.setName("async-dispatcher");
        eventHandlingThread.start();
    }

    protected void dispatch(TaskEventType event) {
        EventHandler handler = eventDispatchers.get(event);
        if(handler != null) {
            handler.handle(event);
        }
    }

    public EventHandler getEventHandler() {
        return handlerInstance;
    }

    interface EventHandler {
        void handle(TaskEventType var1);
    }

    class GenericEventHandler implements EventHandler {

        @Override
        public void handle(TaskEventType event) {
            try {
                eventQueue.put(event);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void register(TaskEventType taskEventType, EventHandler handler) {
        eventDispatchers.put(taskEventType, handler);
    }

    static class TaskDispatchEventHandler implements EventHandler {

        @Override
        public void handle(TaskEventType event) {
            System.out.println("事件类型 : " + event);
            System.out.println("分发任务...");
        }
    }

    static class TaskKillEventHandler implements EventHandler {

        @Override
        public void handle(TaskEventType event) {
            System.out.println("事件类型 : " + event);
            System.out.println("kill任务...");
        }
    }

    enum TaskEventType {
        DISPATCH,
        KILL
    }


    public static void main(String[] args) throws Exception {
        AsyncDispatcher asyncDispatcher = new AsyncDispatcher(new LinkedBlockingQueue<>());
        asyncDispatcher.start();

        asyncDispatcher.register(TaskEventType.DISPATCH, new TaskDispatchEventHandler());
        asyncDispatcher.register(TaskEventType.KILL, new TaskKillEventHandler());
        asyncDispatcher.getEventHandler().handle(TaskEventType.DISPATCH);
        asyncDispatcher.getEventHandler().handle(TaskEventType.KILL);

    }
}

3、输出结果

事件类型 : DISPATCH
分发任务...
事件类型 : KILL
kill任务...


journey
32 声望23 粉丝