Share on Google+Share on Google+

Interrupting a thread.

This tutorial demonstrate how a thread is interrupted through code.


The interrupt() method exists in the java.lang.Thread class. This method is used to interrupt the current thread. If current thread is blocked by wait(), join() or sleep() methods then its interrupt status will be cleared and it will receive and InterrrupedException. Interrupting a thread that is not alive have no effect.


class MyThread1 implements Runnable {

  public void run() {
    try {
    catch (Exception exc) {
          " is interrupted.");
    System.out.println(Thread.currentThread().getName() + " is exiting.");

public class ThreadInterrupt1 {
  public static void main(String args[]) throws Exception {
    Thread th1 = new Thread(new MyThread1(), "First Thread");
    Thread th2 = new Thread(new MyThread1(), "Second Thread");


Download this code



Posted on: June 25, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.