Thread Termination and Demon Thread

 

Thread Termination and Demon Thread

 

Thread Termination:

Why :

Because Thread are Consuming Resources : Like Memory, Kernel Recourses, cpu Cycle and cache memory

When:

1. if thread finished its work, so we need to clean up the thread's resources

2. if thread misbehaving 

3. By default application will not stope the thread as long as at least one thread is still running 


Therad.intrrupt():

1 if the thread is executing a method that throws an intrrupedException

2. if the thread's code is handling the interrupt signal explicitly 

3. If any one of the thread is in sleeping or waiting state (i.e. sleep() or wait() is invoked), calling the Thread.interrupt() method on the thread, breaks out the sleeping or waiting state throwing InterruptedException. If the thread is not in the sleeping or waiting state, calling the interrupt() method performs normal behavior and doesn't interrupt the thread but sets the interrupt flag to true

 




Daemon Thread:

Daemon threads are thread that do not prevent the application from exiting if the main thread terminates

Example 1: Background tasks, that should not block our application from termination
File Saving thread in a Text Editor application
like save our work in every seconds so that thread should be as Daemon Thread

Example 2: Code in a worker thread is not under our control, and we do not want it to block our application from terminating

worker thread that uses an external Library

Summary:

1. Learned how to stop thread gracefully by calling the thread.interrupt()
2. If the method does not respond to the interrupt signal by throwing the the interruptedException, we
need to check that signal and handle it ourselves
3. We also learn how to set up thread to not block our application from exiting

Blocking Task Example: Code

public class IntrruptClassBlcoking {


public static void main(String[] args) {
Thread t1 = new Thread(new BlockingTask());
t.start();
t.interrupt();
    }
    private static class BlockingTask implements Runnable {
    @Override
public void run() {
try {
System.out.println("Before Sleeping");
Thread.sleep(10000);
System.out.println("After Sleeping");
} catch (InterruptedException e) {
System.out.println("Exiting Blocing Thread");
throw new RuntimeException(e);
}
}
}
}


By taking long to calculate something in multithreading:

public class IntrruptClassLongComputation {


public static void main(String[] args) {
Thread t2 = new Thread(new LongComputaion(new BigInteger("2000000"), new BigInteger("4000000")));
t.start();
t.interrupt();
}


private static class LongComputaion implements Runnable {
private BigInteger base;
private BigInteger power;
public LongComputaion(BigInteger base, BigInteger power) {
this.base = base;
this.power = power;
}

@Override
public void run() {
System.out.println("Before Calculation");
System.out.println(base+" ^ "+power+ " = "+getPower(base,power));
System.out.println("After Calculation");
}
private BigInteger getPower(BigInteger base, BigInteger power) {
BigInteger result = BigInteger.ONE;

for(BigInteger i = BigInteger.ZERO ; i.compareTo(power) != 0 ; i= i.add(BigInteger.ONE)) {
if(Thread.currentThread().isInterrupted()) {
System.out.println("Thread is intrrupted from outside");
return BigInteger.ZERO;
}
result = result.multiply(base);
}
return result;
}
}
}


Comments