Java 多執行緒 學習筆記(二)停止執行緒的幾種方法

極客on之路發表於2016-07-03

1.異常法:

package test;

import exthread.MyThread;

import exthread.MyThread;

public class Run {

	public static void main(String[] args) {
		try {
			MyThread thread = new MyThread();
			thread.start();
			Thread.sleep(2000);
			thread.interrupt();
		} catch (InterruptedException e) {
			System.out.println("main catch");
			e.printStackTrace();
		}
		System.out.println("end!");
	}

}

package exthread;


public class MyThread extends Thread {
<span style="white-space:pre">	</span>@Override
<span style="white-space:pre">	</span>public void run() {
<span style="white-space:pre">		</span>super.run();
<span style="white-space:pre">		</span>for (int i = 0; i < 500000; i++) {
<span style="white-space:pre">			</span>if (this.interrupted()) {
<span style="white-space:pre">				</span>System.out.println("已經是停止狀態了!我要退出了!");
<span style="white-space:pre">				</span>break;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>System.out.println("i=" + (i + 1));
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>System.out.println("我被輸出,如果此程式碼是for又繼續執行,執行緒並未停止!");
<span style="white-space:pre">	</span>}
}

執行結果:

......

i=255229
i=255230
i=255231
i=255232
i=255233
i=255234
i=255235
i=255236
i=255237
i=255238
i=255239
i=255240
i=255241
i=255242
i=255243
i=255244
i=255245
i=255246
i=255247
i=255248
i=255249
i=255250
i=255251
i=255252
i=255253
i=255254
i=255255
i=255256
i=255257
i=255258
i=255259
i=255260
i=255261
i=255262
i=255263
i=255264
i=255265
i=255266
已經是停止狀態了!我要退出了!
end!
我被輸出,如果此程式碼是for又繼續執行,執行緒並未停止!
已經是停止狀態了!我要退出了!    雖然停止,但是for迴圈以後的程式碼還是會執行的



正確的退出方式

package exthread;

public class MyThread extends Thread {
	@Override
	public void run() {
		super.run();
		try {
			for (int i = 0; i < 500000; i++) {
				if (this.interrupted()) {
					System.out.println("已經是停止狀態了!我要退出了!");
					throw new InterruptedException();
				}
				System.out.println("i=" + (i + 1));
			}
			System.out.println("我在for下面");
		} catch (InterruptedException e) {
			System.out.println("進MyThread.java類run方法中的catch了!");
			e.printStackTrace();
		}
	}
}

2.在沉睡中停止

package exthread;

public class MyThread extends Thread {
	@Override
	public void run() {
		super.run();
		try {
			System.out.println("run begin");
			Thread.sleep(200000);
			System.out.println("run end");
		} catch (InterruptedException e) {
			System.out.println("在沉睡中被停止!進入catch!"+this.isInterrupted());
			e.printStackTrace();
		}
	}
}

package test;

import exthread.MyThread;

import exthread.MyThread;

public class Run {

	public static void main(String[] args) {
		try {
			MyThread thread = new MyThread();
			thread.start();
			Thread.sleep(200);
			thread.interrupt();
		} catch (InterruptedException e) {
			System.out.println("main catch");
			e.printStackTrace();
		}
		System.out.println("end!");
	}

}
執行結果:

run begin
end!
在沉睡中被停止!進入catch!false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at exthread.MyThread.run(MyThread.java:9)


另外一種情況是先停止後sleep 



3.暴力停止: 


Thread.stop() 這種方法存在非執行緒安全問題。所以暫時不考慮



4.使用return 停止執行緒



package test.run;

import extthread.MyThread;

public class Run {

	public static void main(String[] args) throws InterruptedException {
		MyThread t=new MyThread();
		t.start();
		Thread.sleep(2000);
		t.interrupt();
	}

}

package extthread;

public class MyThread extends Thread {

	@Override
	public void run() {
			while (true) {
				if (this.isInterrupted()) {
					System.out.println("停止了!");
					return;
				}
				System.out.println("timer=" + System.currentTimeMillis());
			}
	}

}



相關文章