3、线程休眠、join与中断线程

3、线程休眠

public static void sleep(long millis)
throws InterruptedException使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行),释放CPU
的时间片,具体取决于系统定时器和调度程序的精度和准确性。 线程不会丢失任何显示器的所有权。参数
millis - 以毫秒为单位的睡眠时间长度

异常
IllegalArgumentException - 如果 millis值为负数
InterruptedException - 如果任何线程中断当前线程。 当抛出此异常时,当前线程的中断状态将被清除。
public static void sleep(long millis,int nanos)throws InterruptedException 毫秒,纳秒
static Thread currentThread() 返回对当前正在执行的线程对象的引用。

*线程的休眠
在当前线程的执行中,暂停指定的毫秒数,释放CPU的时间片

 */
public class ThreadDemo {
    public static void main(String[] args){
        MyThread mt=new MyThread();
        mt.start();//启动线程

        //推荐此方法
        MyRunable mr=new MyRunable();
        Thread t2=new Thread(mr);
        t2.start();

    }
}

/*
实现线程的第一种方法:继承Thread类
 */
class  MyThread extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            //获取当前线程对象名称Thread.currentThread().getName()
            System.out.println(Thread.currentThread().getName()+"-"+i);
            try {
                //休眠500毫秒
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


/*
实现线程的第二种方法:实现Runnable接口
 */
class MyRunable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            System.out.println(Thread.currentThread().getName()+"-"+i);
            try {
                //休眠500毫秒
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

线程休眠,目的是让出CPU执行的时间片,让其他工作的线程可以执行,但不会释放对象锁(后面讲)

join与中断线程

public final void join() throws InterruptedException
等待这个线程死亡。
调用此方法的行为方式与调用完全相同
join (0)


/*
join方法
加入线程,让调用的线程线程先执行指定时间或者执行完毕
 */
public class ThreadDemo1 {
    public static void main(String[] args){
        MyRunable2 me2=new MyRunable2();
        Thread t=new Thread(me2);
        t.start();

        for (int i = 0; i < 50; i++) {
            System.out.println(Thread.currentThread().getName()+"--"+i);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(i==20){
                try {
                    t.join();//让T线程执行完毕
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}

class MyRunable2 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            System.out.println(Thread.currentThread().getName()+"--"+i);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

异常 InterruptedException - 如果任何线程中断当前线程。 当抛出此异常时,当前线程的中断状态将被清除
public void interrupt()
中断这个线程。
除非当前线程中断自身,这是始终允许的

public static boolean interrupted()
测试当前线程是否中断。 该方法可以清除线程的中断状态 。 换句话说,如果这个方法被连续调用两次,那么第二个调用将返回false(除非当前线程再次中断,在第一个调用已经清除其中断状态之后,在第二个调用之前已经检查过)。
忽略线程中断,因为线程在中断时不存在将被该方法返回false所反映。

/*
join方法
加入线程,让调用的线程线程先执行指定时间或者执行完毕
 */
public class ThreadDemo1 {
    public static void main(String[] args){
        MyRunable2 me2=new MyRunable2();
        Thread t=new Thread(me2);
        t.start();

        for (int i = 0; i < 50; i++) {
            System.out.println(Thread.currentThread().getName()+"--"+i);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            t.interrupt();//中断线程,只是做了一个中断标记
            }

        }

    }
}

class MyRunable2 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            //测试当前线程是否中断。interrupted.此方法会把中断状态清楚
            if(Thread.interrupted()) {
                break;
            }
            System.out.println(Thread.currentThread().getName()+"--"+i);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
                //再加一个标记
                Thread.currentThread().interrupt();
            }
        }
    }
}

自定义标记中断线程

*
join方法
加入线程,让调用的线程线程先执行指定时间或者执行完毕

中断线程:
(1)使用interrupted方法来中断线程,设置一个中断状态(标记)
(2)自定义标记的方法
 */
public class ThreadDemo1 {
    public static void main(String[] args){
        MyRunable2 me2=new MyRunable2();
        Thread t=new Thread(me2);
//        t.start();

        MyRunable3 mr3=new MyRunable3();
        Thread t2=new Thread(mr3);
        t2.start();

        for (int i = 0; i < 50; i++) {
            System.out.println(Thread.currentThread().getName()+"--"+i);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

           mr3.flag=false;//中断线程
            }

        }

    }
class MyRunable3 implements Runnable {
    public boolean flag = true;
    public MyRunable3() {
        flag = true;
    }
    @Override
    public void run() {
        int i = 0;
        while (flag) {
            System.out.println(Thread.currentThread().getName() + "===" + (i++));
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

想要中断线程,使用自定义标志的方法实现或许比interr方法更加实用和优雅

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值