I need to find a way to spin off a thread from a static call and not wait for the thread to complete. Basically, a "fire and forget" approach. Can someone supply me with a simple example of how this can be accomplished?
If it's a long-lived thread that has a similar lifecycle to your app itself, and is going to be spending a lot of its time waiting on other threads:
new Thread(new yourLongRunningProcessThatImplementsRunnable()).start();
If it's a short-lived, CPU-bound task:
ExecutorService es = Executors.newFixedThreadPool(Runtime.availableProcessors());
es.submit(new yourTaskThatImplementsRunnable());
Though, in most cases like this, you will be submitting a number of tasks to that same ExecutorService
.
Thread t = new Thread(new YourClassThatImplementsRunnable());
t.start();
// JDK 8
new Thread(() -> methodYouWantToRun()).start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With