The blog of CSDN in 杨天福

更多精彩内容,请关注微信公众号:ytf0422

多线程使用方法(启动,休眠,干扰)

1、写一个子类继承Thread,重写run方法

package thread0103;

public class Thread0103 {
    public static void main(String[] args) {
        //实例化自定义线程对象
        Testthread t1=new Testthread();
        //调用start方法启动线程
        t1.start();
        t1.setName("线程1");
        System.out.println("a");

        Testthread t2=new Testthread();
        t2.start();
        t2.setName("线程2");
        System.out.println("b");

        Testthread t3=new Testthread();
        t3.start();
        t3.setName("线程3");
        System.out.println("c");
    }

}
package thread0103;

public class Testthread extends Thread{//自定义线程继承Thread类
    @Override//重写父类的run方法
    public void run() {
        for(int i=0;i<100;i++){
        //调用Thread对象的线程名称
        System.out.println(Thread.currentThread().getName()+":i="+i);

        if(i==50){
            try {
                Thread.sleep(2000);//定义线程到达50后等待2秒
            } catch (InterruptedException e) {
                System.out.println("线程等待异常");
            }
        }
    }
    }

}

2、实现Runable接口,重写run方法,将此类作为运行目标传给Thread对象

package com.duoxiancheng.ytf;

public class MyThread implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i <=100; i+=10) {
            if(i==0){
                System.out.println("电影《"+Thread.currentThread().getName()+"》开始下载");

            }else if(i==100){
                System.out.println("电影《"+Thread.currentThread().getName()+"》已经下载"+i+"%,下载完成!");
            }else{
                System.out.println("电影《"+Thread.currentThread().getName()+"》已经下载"+i+"%");
            }
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                System.err.println("线程被干扰");            }
        }
    }

}
package com.duoxiancheng.ytf;

public class MyComputer {

    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"正在运行");
        MyThread mt1=new MyThread();
        Thread t1=new Thread(mt1);
        t1.setName("追龙");

        MyThread mt2=new MyThread();
        Thread t2=new Thread(mt2);
        t2.setName("奇门遁甲");

        MyThread mt3=new MyThread();
        Thread t3=new Thread(mt3);
        t3.setName("机器之血");

        MyThread mt4=new MyThread();
        Thread t4=new Thread(mt4);
        t4.setName("大闹天竺");

        MyThread mt5=new MyThread();
        Thread t5=new Thread(mt5);
        t5.setName("我在故宫修文物");

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        Scanner s= new Scanner(System.in);
        for (int i = 0; i < 10000; i++) {
            System.out.println("按回车键干扰");
            s.nextLine();       
            t1.interrupt();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

}

这里写图片描述

阅读更多
版权声明:天际流火叩响大地之门,岁月星辰刻画沧桑年轮! https://blog.csdn.net/ytfunnysite/article/details/53993118
个人分类: JavaSE基础
上一篇学生管理系统(增删改查)
下一篇多线程复制文件显示进度
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭