相关疑难解决方法(0)

使用SingleThreadExecutor在任务之间等待

我试图(简单地)创建一个阻塞线程队列,当提交任务时,方法等待直到完成执行.困难的部分是等待.

这是我的12:30 AM代码,我认为是矫枉过正:

public void sendMsg(final BotMessage msg) {
    try {
        Future task;
        synchronized(msgQueue) {
            task = msgQueue.submit(new Runnable() {
                public void run() {
                    sendRawLine("PRIVMSG " + msg.channel + " :" + msg.message);
                }
            });
            //Add a seperate wait so next runnable doesn't get executed yet but
            //above one unblocks
            msgQueue.submit(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(Controller.msgWait);
                    } catch (InterruptedException e) {
                        log.error("Wait to send message interupted", e);
                    }
                }
            });
        }
        //Block until done
        task.get();
    } …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading

2
推荐指数
1
解决办法
6642
查看次数

标签 统计

concurrency ×1

java ×1

multithreading ×1