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 操作数据库
    转 Python爬虫入门七之正则表达式
    转 python面试题
    转 Perl函数返回值用法指导
    慕课爬虫实战 爬取百度百科Python词条相关1000个页面数据
    慕课爬虫
    转 Python爬虫入门五之URLError异常处理
    转 廖雪峰 urllib
    转 Python爬虫入门四之Urllib库的高级用法
  • 原文地址:https://www.cnblogs.com/tszr/p/12152841.html
Copyright © 2011-2022 走看看