Java multi-threaded programming interruptException

The following is the exception handling in java multithreading:


package multithread;

public class InterruptException {

	public static void main(String[] args) throws InterruptedException {
		Thread thread1 = new InterruptThread1();
		
		Thread thread2 = new InterruptThread2();
		thread1.start();
		thread2.start();
		
		Thread.sleep(1000);
		thread1.interrupt();
		
		Thread.sleep(1000);
		
		thread2.interrupt();
	}

	private static class InterruptThread1 extends Thread {
		public void run() {
			while (!Thread.currentThread().isInterrupted()) {
				try {
					Thread.sleep(100);
					System.out.println("running 1");
				} catch (InterruptedException e) {
					System.out.println("InterruptedException 1");
					Thread.currentThread().interrupt();
				}
			}
		}
	}

	private static class InterruptThread2 extends Thread {
		public void run() {
			try {
				while (!Thread.currentThread().isInterrupted()) {
					Thread.sleep(100);
					System.out.println("running 2");
				}
			} catch (InterruptedException e) {
				System.out.println("InterruptedException 2");
				Thread.currentThread().interrupt();
			}
		}
	}
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326916047&siteId=291194637