I am trying to (simply) make a blocking thread queue, where when a task is submitted the method waits until its finished executing. The hard part though is the wait.
Here’s my 12:30 AM code that I think is overkill:
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();
} catch (ExecutionException e) {
log.error("Couldn't schedule send message to be executed", e);
} catch (InterruptedException e) {
log.error("Wait to send message interupted", e);
}
}
As you can see, there’s alot of extra code there just to make it wait 1.7 seconds between tasks. Is there an easier and cleaner solution out there or is this it?
As suggested here, I am posting this as the answer since its the cleanest solution I have come up with, but not an actual copy of any of the answer’s posted here.
Thanks for all the help