Thread
A thread is a piece of that executes independently. Every program contains at least one thread I.e. main thread. This thread default name is main only.
Multithreading:
Execution of more than one thread at a time is called as Multithreading.
-A thread will automatically destroyed when the run() method has completed. But it might be require to kill/stop a thread before it has completed its life cycle.
Using a boolean flag: We can define a boolean variable which is used for stopping/killing threads say ‘exit’. Whenever we want to stop a thread, the ‘exit’ variable will be set to true.
class NewThread implements Runnable { private boolean exit; private String name; Thread t; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); exit = false; t.start(); } public void run() { int i = 0; while (!exit) { System.out.println(name + ": " + i); i++; try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println("Caught:" + e); } } System.out.println(name + " Stopped."); } public void stop() { exit = true; } } public class Main { public static void main(String args[]) { NewThread t1 = new NewThread("First thread"); NewThread t2 = new NewThread("Second thread"); try { Thread.sleep(500); t1.stop(); t2.stop(); Thread.sleep(500); } catch (InterruptedException e) { System.out.println("Caught:" + e); } System.out.println("Exiting the main Thread"); } }
Output:
New thread: Thread[#20,First thread,5,main]
New thread: Thread[#21,Second thread,5,main]
First thread: 0
Second thread: 0
First thread: 1
Second thread: 1
First thread: 2
Second thread: 2
First thread: 3
Second thread: 3
First thread Stopped.
Second thread Stopped.
Exiting the main Thread
Using Thread.interrupt() method: Whenever an interrupt has been sent to a thread, it should stop whatever task it is performing.
It is very likely that whenever the thread receives an interrupt, it is to be terminated. This action can be done by using the interrupt() method.
Whenever Thread.interrupt() is called, it sets a flag known as the interrupt status to true.
This means that the thread has to stop performing further execution. The default value of this flag is false
class MyThread implements Runnable { Thread t; MyThread() { t = new Thread(this); System.out.println("New thread: " + t); t.start(); } public void run() { while (!Thread.interrupted()) { System.out.println("Thread is running"); } System.out.println("Thread has stopped."); } } public class Main { public static void main(String args[]) { MyThread t1 = new MyThread(); try { Thread.sleep(1); t1.t.interrupt(); Thread.sleep(5); } catch (InterruptedException e) { System.out.println("Caught:" + e); } System.out.println("Exiting the main Thread"); } }
Output:
New thread: Thread[#20,Thread-0,5,main]
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread is running
Thread has stopped.
Exiting the main Thread