java多线程-Sleep

线程休眠 Sleep-放大问题的发生性

要点:

(1)sleep(时间)此时间指的是当前线程阻塞的毫秒数
(2)sleep存在异常interruptedException,所以代码需要捕获并抛出异常
(3)sleep时间结束后线程进入就绪状态
(4)sleep可以模拟网络延时和倒计时等
(5)每一个对象都有一把锁,sleep不会释放锁(抱着锁睡觉)

案例(抢票)代码:

(一)没有加入sleep

package com.heima.Multithreading;

//模拟多个不同个体抢票(票一个对象进入多个线程)
//模拟网络延时:放大问题的发生性
public class Sleep implements Runnable{

    //票数
    private int ticketNumber = 10;
    @Override
    public void run() {
        while(true){
            if (ticketNumber<=0){
                break;
            }

            System.out.println(Thread.currentThread().getName()+"--->拿到第"+ticketNumber--+"张票");
        }
    }

    public static void main(String[] args) {
        Sleep sleep = new Sleep();
        new Thread(sleep,"宏哥").start();
        new Thread(sleep,"肇庆彭于晏").start();
        new Thread(sleep,"黄牛党").start();
    }
}

在这里插入图片描述
(二)加入sleep的

package com.heima.Multithreading;

//模拟多个不同个体抢票(票一个对象进入多个线程)
//模拟网络延时:放大问题的发生性
public class Sleep implements Runnable{

    //票数
    private int ticketNumber = 10;
    @Override
    public void run() {
        while(true){
            if (ticketNumber<=0){
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
            System.out.println(Thread.currentThread().getName()+"--->拿到第"+ticketNumber--+"张票");
        }
    }

    public static void main(String[] args) {
        Sleep sleep = new Sleep();
        new Thread(sleep,"宏哥").start();
        new Thread(sleep,"肇庆彭于晏").start();
        new Thread(sleep,"黄牛党").start();
    }
}

在这里插入图片描述
所以很明显,这样执行多线程是存在问题的,都是如果你不使用sleep进行延时处理,根本发现不了,因为cpu执行过快.

案例(倒计时)
package com.heima.Multithreading;

public class Sleep2 {

    public static void main(String[] args) {
        try {
            tenDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void tenDown() throws InterruptedException {
        int num = 10;
        while(true){
            Thread.sleep(1000);
            System.out.println(num--);
            if (num<=0){
                break;
            }
        }
    }
}

在这里插入图片描述

案例(打印当前系统时间)
package com.heima.Multithreading;

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

//打印当前时间
public class Sleep3 {
    public static void main(String[] args) {
        Date startTime = new Date(System.currentTimeMillis());//获取系统当前时间
        while(true){
            try {
                Thread.sleep(1000);
                System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
                startTime = new Date(System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值