标签:核心技术 tac lin style ++ override 静态方法 exception file
stop() 强制停止线程;已经弃用;可能造成数据不一致等问题。
interrupt() 在线程中做停止标记,并非真的停止线程。
this.interrupted()测试当前线程是否中断,并清除中断状态。
this.isInterrupted() 测试线程是否已经中断。
方法声明:
public static boolean interrupted() 静态方法
public boolean isInterrupted() 非静态方法
例子:
package ThreadTest2;
public class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
for(int i=0;i<500000;i++){
// System.out.println(Thread.currentThread().getName());
// System.out.println(Thread.currentThread().isInterrupted());
System.out.println("i="+(i+1));
}
}
}
package ThreadTest2;
public class Run {
public static void main(String[] args){
try{
MyThread thread=new MyThread();
thread.start();
Thread.sleep(1000);
thread.interrupt();
//当前正在执行的线程是main,因此没有停止
System.out.println("是否停止了1?=" + Thread.interrupted());
System.out.println("是否停止了2?=" + Thread.interrupted());
//终止main方法。
//interrupted()方法会清除线程的中断状态,所以4的时候是false
Thread.currentThread().interrupt();
System.out.println("是否停止了3?=" + Thread.interrupted());
System.out.println("是否停止了4?=" + Thread.interrupted());
}catch(InterruptedException e){
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}
结果:
i=4997
i=4998
i=4999
i=5000
是否停止了1?=false
是否停止了2?=false
是否停止了3?=true
是否停止了4?=false
end!
package ThreadTest2;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class Run3 {
public static void main(String[] args) throws Exception{
try{
MyThread thread=new MyThread();
thread.start();
Thread.sleep(1000);
thread.interrupt();
PrintStream ps = new PrintStream("e:/log.txt");
System.setOut(ps);
//当前正在执行的线程是main,因此没有停止
System.out.println(thread.getName() + "是否停止了5?=" + thread.isInterrupted());
System.out.println(thread.getName() + "是否停止了6?=" + thread.isInterrupted());
}catch(InterruptedException e){
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}
/**
*1 interrupt()并非立即停止线程。run方法还在继续执行,因此说interrupt()只是打了个标记。
*
* */
结果:
。。。。。。
i=126220
Thread-0是否停止了5?=true
i=126221
Thread-0是否停止了6?=true
i=126222
i=126223
以上两个例子,线程都没有停止,仍继续执行。
用interrupt()停止线程。
1 通过抛出异常的方式,中止线程
package ThreadTest3;
public class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
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类catch方法");
e.printStackTrace();
}
}
}
package ThreadTest3;
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!");
}
}
结果:
i=256841
i=256842
i=256843
end!
线程已停止,准备退出!
MyThread类catch方法
java.lang.InterruptedException
at ThreadTest3.MyThread.run(MyThread.java:13)
注意:interrupt() 和sleep(),先interrupt再sleep或者先sleep再interrupt都会抛出java.lang.InterruptedException。
stop()强制中止线程。(弃用,造成数据不一致)
package threadTest4;
public class MyThread extends Thread{
private SynchronizedObject object;
public MyThread(SynchronizedObject object){
super();
this.object=object;
}
@Override
public void run() {
object.printStriing("b", "bb");
}
}
package threadTest4;
public class Run {
public static void main(String[] args){
try{
SynchronizedObject object=new SynchronizedObject();
MyThread thread = new MyThread(object);
thread.start();
Thread.sleep(500);
thread.stop();
System.out.println(object.getUsername() + " " +object.getPassword());
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
package threadTest4;
public class SynchronizedObject {
private String username="a";
private String password="aa";
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username=username;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
synchronized public void printStriing(String username,String password){
try{
this.username=username;
Thread.sleep(100000);
this.password=password;
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
结果:
b aa
interrupt() 和 return()结合。
package threadTest5;
public class MyThread extends Thread{
@Override
public void run() {
while(true){
if(this.interrupted()){
System.out.println("线程停止");
return;
}
System.out.println("timer= " + System.currentTimeMillis());
}
}
}
package threadTest5;
public class Run {
public static void main(String[] args){
try{
MyThread thread = new MyThread();
thread.start();
Thread.sleep(2000);
thread.interrupt();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
结果:
。。。。。。
timer= 1581934919550
timer= 1581934919550
timer= 1581934919550
timer= 1581934919550
线程停止
参考:《Java多线程编程核心技术》 高洪岩
线程停止(stop() interrupt() interrupted() isInterrupted())
标签:核心技术 tac lin style ++ override 静态方法 exception file
原文地址:https://www.cnblogs.com/perfumeBear/p/12322932.html