Java线程中的sleep方法详解

1、关于线程的sleep方法

static void sleep(long mills)

  • 1、静态方法:Thread.sleep(1000);
  • 2、参数是毫秒
  • 3、作用:让当前线程进入休眠,进入“阻塞”状态,放弃占有CPU时间片,让给其他线程使用
public class ThreadTest06 {
    
    
    public static void main(String[] args) {
    
    
        //让当前线程进入休眠,睡眠5秒
        //当前线程是主线程!!!
        try {
    
    
            Thread.sleep(1000*5);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }

		//5秒之后执行这里的代码
        System.out.println("hello world!");
    }
}

2、分析哪个线程休眠的

以下代码是让main方法休眠

public class ThreadTest06 {
    
    
    public static void main(String[] args) {
    
    
        Thread t = new MyThread();
        t.setName("t");
        t.start();
        //调用sleep方法
        try {
    
    
            //
            t.sleep(1000*5);//在执行过程中还是会被转换为:Thread.sleep(1000*5);
            //这行代码的作用是:让当前的线程进入休眠,也就是说说main线程进入休眠。
            //这样代码出现在main方法中,main线程休眠。
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println("hello world!");
    }
}
class MyThread extends Thread{
    
    
    @Override
    public void run() {
    
    
        for (int i = 0; i < 1000; i++) {
    
    
            System.out.println(Thread.currentThread().getName() + "-->" + i);
        }
    }
}

3、终止线程的休眠

sleep睡眠太久了,如果希望半道上醒来,可以使用interrupt()方法
注意:这个不是终断线程的执行,是终止线程的睡眠

public class ThreadTest07 {
    
    
    public static void main(String[] args) {
    
    

        Thread t = new Thread(new MyRunable2());
        t.setName("t");
        t.start();

        //希望5秒以后,t线程醒来(5秒以后主线程的活干完了)
        try {
    
    
            Thread.sleep(1000*5);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }

        //终断t线程的睡眠(这种终断睡眠的方式依靠了java的异常处理机制。)
        t.interrupt();//干扰,一盆冷水过去!!
    }
}

class MyRunable2 implements Runnable{
    
    
    //重点:run()当中的异常不能throws,只能try..catch
    //因为run()方法在父类中没有抛出异常,子类不能比父类抛出更多异常。
    @Override
    public void run() {
    
    
        System.out.println(Thread.currentThread().getName() + "--> begin");
        try {
    
    
            //睡眠1年
            Thread.sleep(1000*60*60*24*365);
        } catch (InterruptedException e) {
    
    
            //打印异常信息
            e.printStackTrace();
        }
        //一年之后才会执行这里
        System.out.println(Thread.currentThread().getName() + "--> end");
    }
}

在这里插入图片描述

4、终止线程

强行终止线程

stop()方法,有一个很大的缺点:容易丢失数据。因为这种方式是直接将线程杀死了。
线程没有保存的数据将会丢失。不建议使用。

合理的终止线程

打一个boolean标记

public class ThreadTest08 {
    
    
    public static void main(String[] args) {
    
    
        Thread t = new Thread();
        MyRunnable4 r = new MyRunnable4();
        t.setName("t");
        t.start();

        //模拟5秒
        try {
    
    
            Thread.sleep(1000 * 5);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }

        //终止线程
        //你想要什么时候终止t的执行,那么你把标记修改为false,就结束了。
        r.run = false;
    }
}
class MyRunnable4 implements Runnable{
    
    

    //打一个boolean标记
    boolean run = true;

    @Override
    public void run() {
    
    
        for (int i = 0; i < 1000; i++) {
    
    
            if (run){
    
    
                System.out.println(Thread.currentThread().getName() + "-->" + i);
                try {
    
    
                    Thread.sleep(1000 );
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }

            }else {
    
    
                //return就结束了,在结束之前还有什么没有保存的。
                //可以在这里保存
                //save....
                return;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq2632246528/article/details/114801566