1. Write a program create a thread using the Runnable interface to print numbers from 1 to 5?
class MyRunnable implements Runnable { public void run() { for (int i = 1; i <= 5; i++) { System.out.println(i); } } } public class Main { public static void main(String[] args) { MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); thread.start(); } } |
Output :
1 2 3 4 5 |
2. Write a program to demonstrate thread synchronization using synchronized keyword?
class Counter { private int count = 0; public synchronized void increment() { count++; } public synchronized int getCount() { return count; } } public class Main { public static void main(String[] args) throws InterruptedException { Counter counter = new Counter(); Thread t1 = new Thread(() -> { for (int i = 0; i < 1000; i++) { counter.increment(); } }); Thread t2 = new Thread(() -> { for (int i = 0; i < 1000; i++) { counter.increment(); } }); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(“Final count: ” + counter.getCount()); } } |
Output :
Final count: 2000 |
3. Write a program demonstrating the use of a daemon thread?
class DaemonThread extends Thread { public void run() { while (true) { System.out.println(“Daemon thread is running…”); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println(e); } } } } public class Main { public static void main(String[] args) throws InterruptedException { DaemonThread daemonThread = new DaemonThread(); daemonThread.setDaemon(true); daemonThread.start(); Thread.sleep(5000); // Main thread sleeps for 5 seconds System.out.println(“Main thread ends”); } } |
Output :
Daemon thread is running… Daemon thread is running… Daemon thread is running… Daemon thread is running… Daemon thread is running… Main thread ends |
4. Write a program to Implement a program to demonstrate thread communication using wait(), notify(), and notifyAll() methods?
class PrintOddEven { private int number = 1; private final Object lock = new Object(); public void printOdd() { synchronized (lock) { while (number <= 10) { if (number % 2 == 0) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(“Odd: ” + number++); lock.notify(); // Notify the even thread } } } public void printEven() { synchronized (lock) { while (number <= 10) { if (number % 2 != 0) { try { lock.wait(); // Wait until the number is even } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(“Even: ” + number++); lock.notify(); } } } } public class Main { public static void main(String[] args) { PrintOddEven printOddEven = new PrintOddEven(); Thread oddThread = new Thread(() -> printOddEven.printOdd()); Thread evenThread = new Thread(() -> printOddEven.printEven()); oddThread.start(); evenThread.start(); } } |
Output :
Odd: 1 Even: 2 Odd: 3 Even: 4 Odd: 5 Even: 6 Odd: 7 Even: 8 Odd: 9 Even: 10 Odd: 11 |
5. Write a program to demonstrate a deadlock situation?
class A { synchronized void methodA(B b) { b.last(); } synchronized void last() {} } class B { synchronized void methodB(A a) { a.last(); } synchronized void last() {} } public class Main { public static void main(String[] args) { A a = new A(); B b = new B(); Thread t1 = new Thread(() -> a.methodA(b)); Thread t2 = new Thread(() -> b.methodB(a)); t1.start(); t2.start(); } } |
Output :
No output – threads are stuck waiting on each other forever. |
6. Write a program to demonstrate thread pooling using ExecutorService?
class Task implements Runnable { private final int taskId; public Task(int taskId) { this.taskId = taskId; } public void run() { System.out.println(“Task ” + taskId + ” is executed by ” + Thread.currentThread().getName()); } } import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Main { public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(3); for (int i = 1; i <= 5; i++) { executor.submit(new Task(i)); } executor.shutdown(); } } |
Output :
Task 1 is executed by pool-1-thread-1 Task 2 is executed by pool-1-thread-3 Task 3 is executed by pool-1-thread-2 Task 4 is executed by pool-1-thread-1 Task 5 is executed by pool-1-thread-3 |
7. Write a program to demonstrate the use of the Thread.sleep() method, where two threads print numbers with a delay?
class PrintNumbers implements Runnable { public void run() { for (int i = 1; i <= 5; i++) { try { Thread.sleep(1000); System.out.println(i); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { PrintNumbers printNumbers = new PrintNumbers(); Thread thread1 = new Thread(printNumbers); Thread thread2 = new Thread(printNumbers); thread1.start(); thread2.start(); } } |
Output :
1 1 2 2 3 3 4 4 5 5 |
8. Write a program to create a thread by extending the Thread class to print even numbers from 2 to 10?
class EvenNumbers extends Thread { public void run() { for (int i = 2; i <= 10; i += 2) { System.out.println(i); } } } public class Main { public static void main(String[] args) { EvenNumbers evenThread = new EvenNumbers(); evenThread.start(); } } |
Output :
2 4 6 8 10 |
9. Write a program where the main thread waits for two threads to complete before it terminates?
class Task1 extends Thread { public void run() { try { Thread.sleep(2000); System.out.println(“Task 1 completed”); } catch (InterruptedException e) { e.printStackTrace(); } } } class Task2 extends Thread { public void run() { try { Thread.sleep(3000); System.out.println(“Task 2 completed”); } catch (InterruptedException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) throws InterruptedException { Task1 task1 = new Task1(); Task2 task2 = new Task2(); task1.start(); task2.start(); task1.join(); task2.join(); System.out.println(“Main thread ends after task completion”); } } |
Output :
Task 1 completed Task 2 completed Main thread ends after task completion |
10. Write a program to demonstrate how to interrupt a thread?
class InterruptTask extends Thread { public void run() { for (int i = 1; i <= 10; i++) { if (Thread.interrupted()) { System.out.println(“Thread interrupted”); break; } System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println(“Thread interrupted during sleep”); break; } } } } public class Main { public static void main(String[] args) throws InterruptedException { InterruptTask thread = new InterruptTask(); thread.start(); Thread.sleep(3000); thread.interrupt(); } } |
Output :
1 2 3 Thread interrupted during sleep |
11. Write a program where two threads print odd and even numbers alternately?
class OddEvenPrinter { private int number = 1; private final Object lock = new Object(); public void printOdd() { synchronized (lock) { while (number <= 10) { if (number % 2 == 0) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(“Odd: ” + number++); lock.notify(); } } } public void printEven() { synchronized (lock) { while (number <= 10) { if (number % 2 != 0) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(“Even: ” + number++); lock.notify(); } } } } public class Main { public static void main(String[] args) { OddEvenPrinter oddEvenPrinter = new OddEvenPrinter(); Thread oddThread = new Thread(() -> oddEvenPrinter.printOdd()); Thread evenThread = new Thread(() -> oddEvenPrinter.printEven()); oddThread.start(); evenThread.start(); } } |
Output :
Odd: 1 Even: 2 Odd: 3 Even: 4 Odd: 5 Even: 6 Odd: 7 Even: 8 Odd: 9 Even: 10 Odd: 11 |
12. Write a program to create a thread group and execute multiple threads in the same group?
class MyTask extends Thread { public MyTask(ThreadGroup group, String name) { super(group, name); } public void run() { System.out.println(Thread.currentThread().getName() + ” is executing”); } } public class Main { public static void main(String[] args) { ThreadGroup group = new ThreadGroup(“MyThreadGroup”); MyTask task1 = new MyTask(group, “Thread-1”); MyTask task2 = new MyTask(group, “Thread-2”); MyTask task3 = new MyTask(group, “Thread-3”); task1.start(); task2.start(); task3.start(); } } |
Output :
Thread-1 is executing Thread-2 is executing Thread-3 is executing |
13. Write a program to create a countdown timer that prints numbers from 10 to 1 using a thread?
class CountdownTimer extends Thread { public void run() { for (int i = 10; i > 0; i–) { System.out.println(i); try { Thread.sleep(1000); // Sleep for 1 second } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(“Countdown Finished!”); } } public class Main { public static void main(String[] args) { CountdownTimer countdown = new CountdownTimer(); countdown.start(); } } |
Output :
10 9 8 7 6 5 4 3 2 1 Countdown Finished! |
14. Write a program to use a CyclicBarrier to synchronize multiple threads, where each thread performs a task before proceeding?
import java.util.concurrent.*; class Task implements Runnable { private final int taskId; private final CyclicBarrier barrier; public Task(int taskId, CyclicBarrier barrier) { this.taskId = taskId; this.barrier = barrier; } public void run() { try { System.out.println(“Task ” + taskId + ” is performing some work.”); barrier.await(); // Wait for other threads to reach the barrier System.out.println(“Task ” + taskId + ” is continuing after barrier.”); } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) throws InterruptedException { int numberOfThreads = 3; CyclicBarrier barrier = new CyclicBarrier(numberOfThreads, () -> System.out.println(“All tasks reached the barrier, now continuing…”)); for (int i = 1; i <= numberOfThreads; i++) { new Thread(new Task(i, barrier)).start(); } } } |
Output :
Task 3 is performing some work. Task 2 is performing some work. Task 1 is performing some work. All tasks reached the barrier, now continuing… Task 1 is continuing after barrier. Task 3 is continuing after barrier. Task 2 is continuing after barrier. |