The document discusses 5 examples of Java thread programming - extending Thread class, implementing Runnable interface, anonymous Runnable implementation, using ExecutorService for thread pool, and using Callable with Future for thread result.
The document discusses 5 examples of Java thread programming - extending Thread class, implementing Runnable interface, anonymous Runnable implementation, using ExecutorService for thread pool, and using Callable with Future for thread result.
Roll no : CB.SC.U4AIE23348 Section: D 1. Extending Thread class: java public class ThreadExample extends Thread { public void run() { for (int i = 1; i <= 10; i++) { System.out.println("Thread: " + i); } }
public static void main(String[] args) {
ThreadExample thread = new ThreadExample(); thread.start(); } }
2. Implementing Runnable interface:
java public class RunnableExample implements Runnable { public void run() { for (int i = 1; i <= 10; i++) { System.out.println("Runnable: " + i); } }
public static void main(String[] args) {
Thread = new Thread(new RunnableExample()); thread.start(); } }
3. Anonymous Runnable implementation:
java public class AnonymousRunnableExample { public static void main(String[] args) { Thread thread = new Thread(new Runnable() { public void run() { for (int i = 1; i <= 10; i++) { System.out.println("Anonymous Runnable: " + i); } } }); thread.start(); } }