zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然java开发常用类库学习笔记:线程操作范例

    class MyThread extends Thread{
        private int time ;
        public MyThread(String name,int time){
            super(name) ;    // 设置线程名称
            this.time = time ;    // 设置休眠时间
        }
        public void run(){
            try{
                Thread.sleep(this.time) ;    // 休眠指定的时间
            }catch(InterruptedException e){
                e.printStackTrace() ;
            }
            System.out.println(Thread.currentThread().getName() + "线程,休眠"
                + this.time + "毫秒。") ;
        }
    };
    public class ExecDemo01{
        public static void main(String args[]){
            MyThread mt1 = new MyThread("线程A",10000) ;    // 定义线程对象,指定休眠时间
            MyThread mt2 = new MyThread("线程B",20000) ;    // 定义线程对象,指定休眠时间
            MyThread mt3 = new MyThread("线程C",30000) ;    // 定义线程对象,指定休眠时间
            mt1.start() ;    // 启动线程
            mt2.start() ;    // 启动线程
            mt3.start() ;    // 启动线程
        }
    };
    class MyThread implements Runnable{
        private String name ;
        private int time ;
        public MyThread(String name,int time){
            this.name = name ;    // 设置线程名称
            this.time = time ;    // 设置休眠时间
        }
        public void run(){
            try{
                Thread.sleep(this.time) ;    // 休眠指定的时间
            }catch(InterruptedException e){
                e.printStackTrace() ;
            }
            System.out.println(this.name + "线程,休眠"
                + this.time + "毫秒。") ;
        }
    };
    public class ExecDemo02{
        public static void main(String args[]){
            MyThread mt1 = new MyThread("线程A",10000) ;    // 定义线程对象,指定休眠时间
            MyThread mt2 = new MyThread("线程B",20000) ;    // 定义线程对象,指定休眠时间
            MyThread mt3 = new MyThread("线程C",30000) ;    // 定义线程对象,指定休眠时间
            new Thread(mt1).start() ;    // 启动线程
            new Thread(mt2).start() ;    // 启动线程
            new Thread(mt3).start() ;    // 启动线程
        }
    };
  • 相关阅读:
    人生苦短,我用python-- Day8
    人生苦短,我用python-- Day7
    人生苦短,我用python-- Day6 面向对象
    人生苦短,我用python-- Day5
    人生苦短,我用python-- Day4
    人生苦短,我用python-- Day3
    人生苦短,我用python-- Day2
    人生苦短,我用python-- Day1
    小康陪你学JAVA--------三大循环之Do-while循环
    小康陪你学JAVA--------三大循环之For循环
  • 原文地址:https://www.cnblogs.com/tszr/p/12152841.html
Copyright © 2011-2022 走看看