Java single worker thread for SQL update statements

自作多情 提交于 2019-12-04 16:32:25

No need for an explicit blocking queue. You can have a worker thread and a work queue encapsulated by an ExecutorService. As for the values, you can use generics:

class ThreadTask<T> implements Runnable {
    private T value;

    public ThreadTask(T value) {
        this.value = value;
    }

    public void run() {
        // update based on value
    }
}

...

ExecutorService exec = Executors.newSingleThreadExecutor();
exec.submit(new ThreadTask<String>("asdf"));
exec.submit(new ThreadTask<Integer>(1));

The single thread executor is simply a worker thread waiting on a queue and executing the submitted tasks in order. No need for other explicit management.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!