原创 

java小代码:中断停止线程

分类:    489人阅读    IT小君  2017-10-19 22:07
package mutilThreadDemo;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by damon on 2017/10/17.
 */
public class App3 {

    /**
     * 中断
     */
    @Test
    public void testInterrupte()throws Exception{
        Thread r = new Thread(){
            public void run(){
                try {
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    for (int i = 0; i < 100; i++) {
                        System.out.printf("%s:%s--Running!times:%srn", df.format(new Date()), this.getName(), i);
                    }
                }catch (Exception e){}
            }
        };
        r.start();
        r.interrupt();
        System.out.println("interrupt execute");
        System.out.printf("%s:is Interrupted %srn",r.getName(), r.isInterrupted());
        System.out.printf("%s:is Interrupted %srn",r.getName(), r.isInterrupted());
        Thread.currentThread().interrupt();
        System.out.printf("%s:is Interrupted %srn",Thread.currentThread().getName(), Thread.interrupted());
        System.out.printf("%s:is Interrupted %srn",Thread.currentThread().getName(), Thread.interrupted());
        Thread.sleep(6000);
    }

    /**
     * 中断
     */
    @Test
    public void testStopThread()throws Exception{

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Thread r = new Thread(){
            public void run(){
                try {
                    for (int i = 0; i < 100; i++) {
                        System.out.printf("%s:%s--Running!times:%srn", df.format(new Date()), this.getName(), i);
                        if(this.interrupted()) {
                            System.out.println("I am interrupted,I will exitrn");
                            break;
                        }
                    }
                }catch (Exception e){}
            }
        };

        Thread r1 = new Thread(){
            public void run(){
                try {

                    for (int i = 0; i < 100; i++) {
                        System.out.printf("%s:%s--Running!times:%srn", df.format(new Date()), this.getName(), i);
                        if(this.interrupted()) {
                            System.out.println("I am interrupted,I will exitrn");
                            throw new InterruptedException("线程中断结束运行");
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };

        Thread r2 = new Thread(){
            public void run(){
                try {
                    System.out.printf("%s:%s--I am sleep!rn", df.format(new Date()), this.getName());
                    Thread.sleep(20000);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };


        r.start();
        Thread.sleep(2);
        r.interrupt();
        Thread.sleep(2000);
        r1.start();
        Thread.sleep(3);
        r1.interrupt();
        Thread.sleep(2000);
        r2.start();

        try {
            Thread.sleep(200);
            r2.interrupt();
        }catch (InterruptedException ex){
            System.out.printf("%s:%s--Interrupted Exception!rn", df.format(new Date()), r2.getName());
        }
        Thread.sleep(2000);
        System.out.println("main end");
    }
}
支付宝打赏 微信打赏

如果文章对你有帮助,欢迎点击上方按钮打赏作者

 工具推荐 更多»