Java stop (terminate) thread detailed version

Unstoppable thread

Example 1

package ch14;
public class MyThread13 extends Thread
{
    @Override 
    public void run()
    { 
        super.run(); 
        for (int i=0;i<10000;i++)
        { 
            System.out.println("i="+(i+1)); 
        } 
    } 
}
package ch14;
public class Test17
{
    public static void main(String[] args)
    { 
        try
        { 
            MyThread13 thread=new MyThread13();      //创建MyThread13线程类实例
            thread.start();    //启动线程
            Thread.sleep(100);    //延时100毫秒
            thread.interrupt();    //停止线程
        }
        catch(InterruptedException e)
        { 
            System.out.println("main catch"); 
            e.printStackTrace(); 
        } 
    }
}
i=1
i=2
...
i=9999
i=10000

Determine whether the thread is stopped

Example 2

package ch14;
public class MyThread14 extends Thread
{
    @Override 
    public void run()
    { 
        super.run(); 
        for(int i=0;i<10000;i++)
        { 
            System.out.println("i="+(i+1)); 
        } 
    } 
}
package ch14;
public class Test18
{
    public static void main(String[] args)
    {
        try
        {
            MyThread14 thread=new MyThread14();
            thread.start();    //启动线程
            Thread.sleep(100);    //延时100毫秒
            thread.interrupt();    //停止线程
            //Thread.currentThread().interrupt();
            System.out.println("是否停止1?="+thread.interrupted());
            System.out.println("是否停止2?="+thread.interrupted());
        }
        catch(InterruptedException e)
        {
            System.out.println("main catch");
            e.printStackTrace();
        }
        System.out.println("end!");
    }
}
i=1
i=2
...
i=9999
i=10000
是否停止1?=false
是否停止2?=false
end!
thread.interrupt();
System.out.println("是否停止 1 ? ="+thread.interrupted());
System.out.println("是否停止 2 ? ="+thread.interrupted());
public static void main(String[] args)
{
    Thread.currentThread().interrupt();
    System.out.println(" 是否停止 1 ? ="+Thread.interrupted());
    System.out.println(" 是否停止 2 ? ="+Thread.interrupted());
    System.out.println("end!");
}
是否停止 1 ? =true
是否停止 2 ? =false end!
public boolean isInterrupted()
package ch14;
public class Test18
{
    public static void main(String[] args)
    {
        try
        {
            MyThread14 thread=new MyThread14();
            thread.start();
            Thread.sleep(100);
            thread.interrupt();
            System.out.println("是否停止1?="+thread.isInterrupted());
            System.out.println("是否停止2?="+thread.isInterrupted());
        }
        catch(InterruptedException e)
        {
            System.out.println("main catch");
            e.printStackTrace();
        }
        System.out.println("end!");
    }
}
i=498
是否停止1?=true
i=499
是否俜止2?=true
i=500
end!
i=501
i=502

Exception method to stop thread

Example 3

package ch14;
public class MyThread15 extends Thread
{
    @Override
    public void run()
    {
        super.run();
        for(int i=0;i<500000;i++)
        {
            if(this.interrupted())
            {    //如果当前线程处于停止状态
                System.out.println("已经是停止状态了!我要退出了!");
                break;
            }
            System.out.println("i="+(i+1));
        }
    }
}
package ch14;
public class Test19
{
    public static void main(String[] args)
    {
        try
        { 
            MyThread15 thread=new MyThread15(); 
            thread.start();    //启动线程
            Thread.sleep(2000);    //延时2000毫秒
            thread.interrupt();    //停止线程
        }
        catch(InterruptedException e)
        {    //捕捉线程停止异常
            System.out.println("main catch"); 
            e.printStackTrace(); 
        } 
        System.out.println("end!");    //主线程结束时输出
    }
}
......
i=271597
i=271598
已经是停止状态了!我要退出了!
end!
public void run()
{ 
    super.run(); 
    for(int i=0;i<500000;i++)
    { 
        if(this.interrupted())
        { 
            System.out.println("已经是停止状态了!我要退出了!"); 
            break; 
        } 
        System.out.println("i="+(i+1)); 
    } 
    System.out.println("我被输出,如果此代码是for又继续运行,线程并未停止!"); 
}
......
i=233702
i=233703
end!
已经是停止状态了!我要退出了!
我被输出,如果此代码是for又继续运行,线程并未停止!
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("进MyThread15.java类run方法中的catch了!"); 
        e.printStackTrace(); 
    } 
}
......
i=251711
i=251712
i=251713
已经是停止状态了!我要退出了!
end!
进MyThread15.java类run方法中的catch了!
java.lang.InterruptedException
    at text.MyThread.run(MyThread.java:16)

Stop in hibernation

Example 4

package ch14;
public class MyThread16 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 ch14;
public class Test20
{
    public static void main(String[] args)
    {
        try
        {
            MyThread16 thread=new MyThread16();
            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 text.MyThread.run(MyThread.java:12)

Example 5

package ch14;
public class MyThread17 extends Thread
{
    @Override
    public void run()
    {
        super.run();
        try
        {
            for(int i=0;i<1000;i++)
            {
                System.out.println("i="+(i+1));
            }
            System.out.println("run begin");
            Thread.sleep(200);
            System.out.println("run end");
        }
        catch(InterruptedException e)
        {
            System.out.println("先停止,再遇到了sleep!进入catch!");
            e.printStackTrace();
        }
    }
}
package ch14;
public class Test21
{
    public static void main(String[] args)
    {
        MyThread17 thread=new MyThread17();
        thread.start();
        thread.interrupt();
        System.out.println("end!");
    }
}
end!
i=1
i=2
i=3
i=4
i=5
i=6
......
......
i=999
i=1000
run begin
先停止,再遇到了sleep!进入catch!
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at text.MyThread.run(MyThread.java:16)

Force thread stop

package ch14;
public class MyThread18 extends Thread
{
    private int i=0;
    @Override
    public void run()
    {
        try
        {
            while (true)
            {
                i++;
                System.out.println("i=" + i);
                Thread.sleep(1000);
            }
        }
        catch(InterruptedException e)
        {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
package ch14;
public class Test22
{
    @SuppressWarnings("deprecation")
    public static void main(String[] args)
    {
        try
        {
            MyThread18 thread=new MyThread18();
            thread.start();
            Thread.sleep(8000);
            thread.stop();
        }
        catch(InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9

Adverse consequences of releasing the lock

Example 6

package ch14;
public class SynchronizedObject
{
    private String username="root";
    private String password="root";
    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 printString(String username,String password)
    {
        try
        {
            this.username=username;
            Thread.sleep(100000);
            this.password=password;
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
package ch14;
public class MyThread19 extends Thread
{
    private SynchronizedObject object;
    public MyThread19(SynchronizedObject object)
    {
        super();
        this.object=object;
    }
    @Override
    public void run()
    {
        object.printString("admin","123456");
    }
}
package ch14;
public class Test23
{
    public static void main(String[] args)
    { 
        try
        { 
            SynchronizedObject object=new SynchronizedObject(); 
            MyThread19 thread=new MyThread19(object); 
            thread.start(); 
            Thread.sleep(500); 
            thread.stop(); 
            System.out.println("用户名:"+object.getUsername());
            System.out.println("密码:"+object.getpassword()); 
        }
        catch(InterruptedException e)
        { 
            e.printStackTrace();
        } 
    } 
}
用户名:admin
密码:root

Use return to stop the thread

Example 7

package ch14;
public class MyThread20 extends Thread
{
    @Override
    public void run()
    {
        while (true)
        {
            if (this.isInterrupted())
            {
                System.out.println("停止了!");
                return;
            }
            System.out.println("timer="+System.currentTimeMillis());
        }
    }
}
package ch14;
public class Test24
{
    public static void main(String[] args) throws InterruptedException
    {
        MyThread20 t=new MyThread20();
        t.start();
        Thread.sleep(2000);
        t.interrupt();
    }
}
......
timer=1540977194784
timer=1540977194784
timer=1540977194784
timer=1540977194784
timer=1540977194784
停止了!
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>