I have been working on sudoku solver/generator to get started with Java. The solving and generation is done in background using SwingWorker
. Now I want to have batch generation, where the program would generate many sudokus (tens or hundreds). Therefore, I want to use as many threads on a computer as possible, not just one. I would then need as many SwingWorker
instances as there are threads available. Which will cause problems since SwingWorker
is limited to 10 instances at a time. Therefore I want another solution to this problem.
I have looked into multithreading options and there seems to be too many of them: Thread, Runnable, Callable, ExecutorService, SwingWorker,...
. I want to know, which one is best suited for my purpose.
I have also read about implementing a Runnable
is better than extending a Thread
.
I need to pass data, then start the execution on another thread, and when it finishes, I want to notify interested listeners, that can then request data. I was thinking I could create a Runnable
, using a volatile
variable I could also cancel the execution, and at the end of the execution, notify listeners using SwingUtilities.invokeLater()
. I also want to restart this cycle as many times as needed.
SwingWorker
is out because it is limited in number of instances and cannot be restarted.
I have the sudoku algorithms working fine, I just need to wrap the generation algorithms in something to make it support multithreading.