You don't have to use a thread pool in Java if all you want is a thread.* (Assuming you want to use threads at all **) Pretty much the simplest way to create a thread looks like this:
Thread t = new Thread(() -> {
...code to be executed in the new thread goes here...
});
t.start();
...do other stuff concurrently with the new thread...
try {
t.join();
} catch (InterruptedException ex) {
// If your program doesn't use interrupts then this should
// never happen. If it happens unexpectedly then, Houston! We
// have a problem...
ex.printStackTrace();
}
* You might want to use a thread pool if your program otherwise would create many short-lived threads. The purpose of a thread pool, just like any other kind of "xxxx pool," is to re-use threads instead of continually creating and destroying them. Creating and destroying threads is relatively expensive compared to the cost of the tasks that some programs want to run in those threads.
Pretty much the simplest way to use a thread pool looks like this:
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
final int N_THREADS = ...however many worker threads you think you need...;
final int PRACTICALLY_FOREVER = 999;
ExecutorService thread_pool = Executors.newFixedThreadPool(N_THREADS);
while (...still have work to do...) {
thread_pool.submit(() -> {
...task to be executed by a worker thread goes here...
});
}
thread_pool.shutdown();
try {
thread_pool.awaitTermination(PRACTICALLY_FOREVER, TimeUnit.DAYS);
} catch (InterruptedException ex) {
// If your program doesn't use interrupts then this should
// never happen...
ex.printStackTrace();
}
** Some people think that threads are old-fashioned and/or low-level. Java has a whole other model for concurrency. You might want to take some time to learn about parallel streams.