• 线程中join方法和Sleep方法的举例


    Sleep

    package Thread01;
    
    //sleep():  睡眠    让调用者进入阻塞状态
    public class Sleep implements Runnable{
    
        static Thread t = null;
        public static void main(String[] args) {
            
            Sleep s = new Sleep();
             t = new Thread(s,"银河一号");
            System.out.println(t.getName()+"处于创建状态");
            t.start();
            
            
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            
            
            System.out.println("银河一号开始跑步!");
            
            try {
                System.out.println("银河一号进入休眠,处于阻塞状态");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
            System.out.println(t.getName()+"处于就绪状态!");
        }
    
        
        
        
        
        
        
    }

    package Thread01;
    
    //join();   参加      让调用进入就绪状态,让当前线程进入阻塞状态
    public class join {
    
        static boolean boo = false;
        public static void main(String[] args) {
    
            Thread t1 = new Thread("线程1下载图片:") {
    
                public void run() {
                    System.out.println("download:开始下载图片!");
    
                    for (int i = 0; i <= 100; i++) {
    
                        System.out.println(Thread.currentThread().getName() + "正在下载:" + i + "%");
                    }
    
                    //下载完成
                    boo = true;
                }
    
            };
            Thread t2 = new Thread() {
            
        
                public void run() {
                    
                    
                    try {
                        //要先下载再显示
                        t1.join();
                        System.out.println("show:显示图片");
                        
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println("程序中止");
                    }
                    //没有下载完成  抛出没有下载完成就不能显示的异常
                    if(!boo) {
                        
                        //异常Exception 分为检查异常(Checked Exception)和非检查异常(Runtime Exception)
                        throw new RuntimeException("还未下载完成不能显示!");
                        
                        
                    }
                    System.out.println("显示结束!");
                    
                    
                    
                }
            
            
            
            };
            
    //开启两个线程 t1.start(); t2.start(); } }

  • 相关阅读:
    composer "Illegal offset type in isset or empty"报错解决方案
    Yii2 使用a标签发送post请求
    YII2 项目安装步骤及异常记录
    三级城市联动菜单
    最新全国省、市、县数据库
    Sanic官翻-SSL示例
    Sanic官翻-WebSocket
    Sanic官翻-部署
    Sanic官翻-概述
    老子的道德经
  • 原文地址:https://www.cnblogs.com/bichen-01/p/11304821.html
Copyright © 2020-2023  润新知