zl程序教程

您现在的位置是:首页 >  后端

当前栏目

08 线程状态 五大状态 流程图 线程方法 停止线程 代码 线程休眠 代码:模拟网络延时 代码:打印当前系统时间

2023-09-11 14:16:44 时间

线程状态

五大状态

  • new --> 创建状态
  • start() --> 就绪状态
  • CPU调度 --> 运行状态
  • sleep() --> 阻塞状态
  • 正常执行完 --> 死亡状态

在这里插入图片描述

流程图

在这里插入图片描述

线程方法

在这里插入图片描述

停止线程

  • 不推荐使用JDK提供的stop()、destroy()方法【已废弃】

  • 推荐线程自己停止下来

  • 建议使用一个标志位进行终止变量,当flag=false,则终止线程运行。

  • 建议线程正常停止 —> 利用次数,不建议死循环

  • 建议使用标志位 —> 设置一个标志位

在这里插入图片描述

代码

// 测试stop
public class TestStop implements Runnable{
    //1.设置一个标志位
    prvate Boolean flag = true;
    
    @Override
    public void run(){
        int i = 0;
        while (flag){
            System.out.println("run.....thread"+i++);
        }
    }
    
    
    //2.设置一个公开的方法停止线程,转换标志位
    public void stop(){
        this.flag =  false;
    }
    
    
    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        
        new Thread(testStop).start();
        
        
        for (int i =0;i<1000;i++){
            System.out.println("main"+i);
            if (i ==800){
                // 调用stop方法切换标志位,让线程停止
                testStop.stop();
                System.out.prnitln("线程该停止了");
            }
        }
        
        
    }
    
  
    
}

线程休眠

  • sleep(时间)指定当前线程在阻塞的毫秒数;1000ms == 1s
  • sleep存在异常InterruptedException;
  • sleep时间达到后线程进入就绪状态;
  • sleep可以模拟网络延时,倒计时等。
  • 每一个对象都有一个锁,sleep不会释放锁;

在这里插入图片描述

代码:模拟网络延时

// 模拟网络延时 : 放大问题的发生性
public class TestSleep implements Runnable{
    // 票数
    private int ticketNums = 10;
    
    @Override
    public void run(){
        while(true){
            if(ticktNums <= 0){
                break;
            }
            
            // 模拟延时
            try{
                Thread.sleep(200);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            
            System.out.println(Thread.currentThread().getName()+"-->拿到了第"+ticketNums--+"票");
        }
    } 
    
    
    public static void main(String[] args){
        TestSleep ticket = new TestSleep();
        
        new Thread(ticket,"小名").start();
        new Thread(ticket,"小黑").start();
        new Thread(ticket,"黄牛").start();
        
    }
}

代码:模拟倒计时

// 模拟倒计时 : 
public class TestSleep2 implements Runnable{
    
    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;
            }
        }
    }
 
}

代码:打印当前系统时间

// 打印当前系统时间
public class TestSleep2 implements Runnable{
    
    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();
            }
        }
    }
      
    // 模拟倒计时
    public static void tenDown() throws InterruptedException{
        int num = 10;
        
        while(true){
            Thread.sleep(1000);
            System.out.println(num--);
            if(num<=0){
                break;
            }
        }
    }
 
}