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?
UPDATE
After lots of weird code changes and other fluff I have settled on modifying ArrayBlockingQueue so that Put blocks until done. Thanks for all of the help!
public final CustBlockingQueue<BotMessage> msgQueue = new CustBlockingQueue<BotMessage>(1);
// A consumer thread
new Thread(new Runnable() {
public void run() {
while (true)
try {
// Blocks until there is something in the queue
BotMessage msg = msgQueue.take();
sendRawLine("PRIVMSG " + msg.getChannel() + " :" + msg.getMessage());
//Release lock so that put() unblocks
msgQueue.lock.lockInterruptibly();
msgQueue.doneProcessing.signal();
msgQueue.lock.unlock();
//Wait before continuing
Thread.sleep(Controller.msgWait);
} catch (InterruptedException e) {
log.error("Wait for sending message interrupted", e);
}
}
}).start();
public class CustBlockingQueue<E> extends ArrayBlockingQueue<E> {
public ReentrantLock lock = new ReentrantLock(false);
public Condition doneProcessing = lock.newCondition();
public CustBlockingQueue(int capacity) {
super(capacity);
}
public void put(E e) throws InterruptedException {
lock.lockInterruptibly();
super.put(e);
doneProcessing.await();
lock.unlock();
}
}
At least this actually does make sense. Unless someone has a really really good idea, this is what I'm sticking with