Skip to content

Instantly share code, notes, and snippets.

@sithu
Last active December 27, 2015 05:39
Show Gist options
  • Save sithu/7275635 to your computer and use it in GitHub Desktop.
Save sithu/7275635 to your computer and use it in GitHub Desktop.
Processing a task using a background thread via ExecutorService
package example.pubsub;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BackgroundThread {
public static void main(String[] args) {
int numThreads = 1;
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
Runnable backgroundTask = new Runnable() {
@Override
public void run() {
System.out.println("Hello World");
}
};
System.out.println("About to submit the background task");
executor.execute(backgroundTask);
System.out.println("Submitted the background task");
executor.shutdown();
System.out.println("Finished the background task");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment