I have two SwingWorker class: FileLineCounterThread
and FileDivisionThread
I will execute the two threads. When the lines counting thread finishes, it will pass the result to File Division thread.
I do not have an idea on how to pass the result to started thread.
I am not sure this is a solution you should use, and it undermines the simplicity and safety you get from using SwingWorker, but I'll mention it for completeness.
Put two fields where both threads can see them: one boolean, called
hasValue
, initialized to false, and one int (or long) calledcountValue
. Both must be declared asvolatile
. When the counter thread is done, put the count incountValue
. Then sethasValue
to true. The division thread can then check `hasValue' periodically and grab the count when it is available.If the division is providing values that will be more accurate once it gets the count, this will do. More likely, it is doing some work, then waiting for the count. In this case, set up a third field called
countMonitor
, defined asfinal Object
. When it finishes the initial work, have it checkhasValue
. If it's true, grab the value and continue. If it's false, call thewait
method oncountMonitor
and continue when notified. The counter thread, when done, should always call thenotifyAll
method oncountMonitor
after putting values inhasValue
andcountValue
.I've left out a bit here. The javadoc for
Object
will tell you about needed synchronization and checked exceptions. Your design is straightforward enough that you won't be troubled with the usual supernatural horror stories multi-threading generates. I hope. But you might want to do a bit of research if you go this route. (If you repeat the whole process in the same session, you will definitely want to do a lot of research.)PipedReader/Writer for character data & PipedInput/OutputStream for binary data
in java.io.
Regards, Stéphane
never hands up, never surrender its possible with Executor and SwingWorker
1/ bug for Executor and SwingWorker
2/ hold and check number of thread started by Executor and live SwingWorkers threads with intentions to avoid caught above mentioned bug
3/ check maximum numbers for Executor or restict that to final munber
EDIT changed by OP's requirements
SwingWorker.execute()
is buggy and will only execute tasks serially. UseExecutorService.execute()
for concurrency: