Skip to content

Instantly share code, notes, and snippets.

@surabhisuman
Created April 2, 2023 13:01
Show Gist options
  • Save surabhisuman/8eafe62f577bea7d94b8b77a6a1bda9f to your computer and use it in GitHub Desktop.
Save surabhisuman/8eafe62f577bea7d94b8b77a6a1bda9f to your computer and use it in GitHub Desktop.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPool {
private static class DoSomething implements Runnable {
private int taskId;
public DoSomething(int taskId) {
this.taskId = taskId;
}
@Override
public void run() {
System.out.println("Currently running task " + taskId + " by " + Thread.currentThread().getName());
}
}
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(10); // creating a fixed size thread pool
for (int i = 1; i <= 100; i++) {
service.submit(new DoSomething(i));
}
service.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment