InterviewSolution
Saved Bookmarks
| 1. |
Write a program in Java to show isAlive() and join() operations in multithreading. |
|
Answer» The isAlive() method tells whether a thread is alive or terminated. These alive and terminated are the states of a thread in Java. Also, the join() operation joins a thread to another. This means that the thread will wait for the complete execution of the thread to which it is joined even if its own work is completed. Then, they both will terminate together. Java Code to show isAlive() and join() operations class DemoThread extends Thread {public DemoThread(String name) { super(name); setPriority(MAX_PRIORITY); } } class DemoThread2 extends Thread { public void run() { int count = 1; while (true) { System.out.println(count); count++; try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(e); } } } } public class Main { public static void main(String[] args) { DemoThread t = new DemoThread("Thread 1"); System.out.println("ID " + t.getId()); System.out.println("NAME " + t.getName()); System.out.println("PRIORITY " + t.getPriority()); t.start(); System.out.println("STATE " + t.getState()); System.out.println("ALIVE " + t.isAlive()); DemoThread2 t2 = new DemoThread2(); try { Thread.sleep(100); } catch (Exception e) { } t2.setDaemon(true); t2.start(); // t2.interrupt(); Thread mainThread = Thread.currentThread(); try { mainThread.join(); // Now main will not terminate till the daemon thread is terminated } catch (Exception e) { } } } Output ID 13NAME Thread 1 PRIORITY 10 STATE RUNNABLE ALIVE false 1 2 3 4 5 6 7 |
|