Java多线程学习之sleep()和wait()详解及实例(四)

JAVA线程sleep()和wait()详解及实例

sleep

1.sleep是Thread的一个静态(static)方法。使得Runnable实现的线程也可以使用sleep方法。而且避免了线程之前相互调用sleep()方法,引发死锁。

2.sleep()执行时需要赋予一个沉睡时间。在沉睡期间(阻塞线程期间),CPU会放弃这个线程,执行其他任务。当沉睡时间到了之后,该线程会自动苏醒,不过此时线程不会立刻被执行,而是要等CPU分配资源,和其他线程进行竞争。

3.此外如果这个线程之前获取了一个机锁,在沉睡期间,这个机锁不会释放。其他等待这个机锁的程序,必须等待这个线程醒来,且执行完后才能运行。

sleep相关代码

package com.xu.chap3;

public class MyThread {

    public static void main(String[] args) {
        
        System.out.println("begin our test");
        ThreadSleep sleep = new ThreadSleep();
        try {
            Thread thread1 = new Thread(sleep, "路人甲");
            Thread thread2 = new Thread(sleep, "路人乙");
            thread1.start();
            thread2.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("test is over");
    }


}

class ThreadSleep implements Runnable {

    int count = 0;

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
        count();

    }

    public void count() {
        while (count < 20) {
            System.out.println(Thread.currentThread().getName() + " say : count is " + count);
            try {
                count++;
                Thread.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

 输出日志

通过日志可以看出,基本是线程甲在执行,这是因为sleep时,机锁一直在线程甲上,所以线程乙只能一直等待直到线程甲释放锁。 

wait

1.wait()是Object类的一个方法。当调用wait()方法时,该线程会进入和该对象相关的等待池中,并释放它所拥有的机锁。

2.执行wait()后,必须使用notify()方法或notifyAll()方法或设置等待时间(wait(long time))唤醒在等待线程池中的线程。

3.wait()必须放在synchronized block中,否则会在运行时报“java.lang.IllegalMonitorStateException”异常

wait相关代码

package com.xu.chap3;

public class MyThread {

    public static void main(String[] args) {
        System.out.println("begin our test");
        ThreadSleep sleep = new ThreadSleep();
        try {
            Thread thread1 = new Thread(sleep, "路人甲");
            Thread thread2 = new Thread(sleep, "路人乙");
            thread1.start();
            thread2.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("test is over");
    }
}

class ThreadSleep implements Runnable {

    int count = 0;

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
        count();

    }

    public void count() {
        while (count < 20) {
            synchronized (this) {
                System.out.println(Thread.currentThread().getName() + " say : count is " + count);
                try {
                    count++;
                    this.wait(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

通过日志可以发现在wait的情况下,线程主动让出锁,让别的线程优先执行。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易雪寒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值