About the listeners - coool -.
I need something like singleThreadExecutor because the speak() freetts method is blocking, and i don't want to block the edt, or spam swingworkers (that would not be deterministically ordered anyway).
About the queuedTasks ... i introduced it since ExecutorService interface returned by Executors.newSingleThreadExecutor() has no size method (understandable in a concurrent queue), but since i'm going to use it only in a single thread, i have no such scruples.
This is used only for exposing the
1 2 3
| public boolean isSpeaking() { return queuedTasks.get() != 0; } |
Functionality. To make it "Work" i need to fiddle with it on cancellation (-cancelledTasks), successful task execution and RejectedExecutionException.
Looking at the code in Executors.newSingleThreadExecutor() it instanciates a ThreadPoolExecutor. Looking at threadPoolExecutor.getTaskCount(), doesn't do what i want (just to eventually know that there is nothing executing) but count all tasks that were executed, and threadPoolExecutor.getActiveCount() is looking at Thread counts and moreover both methods have the scary word "approximately" in the javadoc.
Do you think threadPoolExecutor.getQueue().getSize() would work? I don't think it works to just use queue directly because the speak method being synchronous as explained above.
The index is a sum of the String lengths spoken since the start, cancellation or the overiden speak method. It's there mostly to be sent in events. I'm considering just erasing it and simplifying the speak interface to
speak(String)
and
speak(Iterator)
People that need to keep indexes should just use the second and keep it internally in the iterator - i think this makes client code more organized too.
I think you're right about how cancel should be blocking, its just that
1) It runs on the edt - i guess no way around it.
2) The AssertionError i'm throwing in killSingleThread(true) worries me, since just being late should probably not kill the calling thread.
You'd recommend a cycle instead?
What do you think?