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) called countValue
. Both must be declared as volatile
. When the counter thread is done, put the count in countValue
. Then set hasValue
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 as final Object
. When it finishes the initial work, have it check hasValue
. If it's true, grab the value and continue. If it's false, call the wait
method on countMonitor
and continue when notified. The counter thread, when done, should always call the notifyAll
method on countMonitor
after putting values in hasValue
and countValue
.
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.)