public class MyThread extends Thread { public void run() { System.out.println("MyThread is running"); } } public static void main(String[] args) { MyThread t = new MyThread(); t.start(); }
ExecutorService executor = Executors.newFixedThreadPool(2); Runnable task1 = new Runnable() { public void run() { System.out.println("Task 1 executed"); } }; Runnable task2 = new Runnable() { public void run() { System.out.println("Task 2 executed"); } }; executor.execute(task1); executor.execute(task2); executor.shutdown();In this example, we create a thread pool with a fixed number of threads using the Executors.newFixedThreadPool() method. We then create two Runnable tasks and submit them to the thread pool using the execute() method. Finally, we shutdown the thread pool using the shutdown() method. Package library: java.util.concurrent