多线程(三)Java线程创建的三种方式

三种创建方式

  • Thread class 继承Thread类
  • 重写runnable方法
  • 实现callable方法

创建线程方式一:继承Thread类,重写run()方法,调用start开启线程


/**
 *创建线程方式一:继承Thread类,重写run()方法,调用start开启线程 
 *  总结:注意,线程开启不一定立即执行,由cpu调度执行
 *
 * @author LCW
 * @since 2020/11/9 8:57
 **/
public class TestThread1 extends Thread {

    @Override
    public void run() {
        //run方法线程体
        for (int i = 0; i < 200; i++) {
            System.out.println("我在看代码--" + i);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public synchronized void start() {
        super.start();
    }

    static String Test() {
        TestThread1 testThread1 = new TestThread1();
        testThread1.start();

        //main线程,主线程
        for (int i = 0; i < 200; i++) {
            System.out.println("我在学习多线程--" + i);
        }
        return "hello";
    }

    public static void main(String[] args) {
        String test = Test();
        //主线程完成后,返回String,新线程执行它的,不影响方法的返回
        System.err.println(test);

    }

}

主线程完成后,返回String,新线程执行它的,不影响方法的返回
在这里插入图片描述
练习Thread,实现多线程下载图片


/**
 * 练习Thread,实现多线程下载图片
 *
 * @author LCW
 * @since 2020/11/9 15:34
 **/
public class TestThread2 extends Thread {

    private String url;
    private String name;

    public TestThread2(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        webDownLoader webDownLoader = new webDownLoader();
        webDownLoader.downloader(url, name);
        System.out.println("系啊在了文件名为:" + name);
    }

    public static void main(String[] args) {
        new TestThread2("https://i1.hdslb" +
                ".com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg", "image.jpg").start();
       new TestThread2("https://i1.hdslb" +
                ".com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg", "image1.jpg").start();
        new TestThread2("https://i1.hdslb" +
                ".com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg", "image2.jpg").start();
    }

}

//下载器
class webDownLoader {
    //下载方法
    public void downloader(String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("IO异常");
        }
    }

}

在这里插入图片描述
创建线程方式2:实现runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法。


/**
 * 创建线程方式2:实现runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法。
 *  * @author LCW
 * @since 2020/11/9 15:53
 **/
public class TestThread3 implements Runnable {
    @Override
    public void run() {
        //run方法线程体
        for (int i = 0; i < 200; i++) {
            System.out.println("我在看代码--" + i);
        }
    }
    public static void main(String[] args) {
        //创建runnable接口的实现类对象
        TestThread3 testThread3 = new TestThread3();
        //创建线程对象,通过线程对象来开启我们的线程,代理
        new Thread(testThread3).start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("我在学习多线程--" + i);
        }
    }
}

小结
继承Thread类

  • 子类继承Thread类具备多线程能力
  • 启动线程:子类对象,start()
  • 不建议使用:避免OOP单继承局限性

实现Runnable接口

  • 实现接口Runnable具有多线程能力
  • 启动线程:传入目标对象+Thread对象.start()
  • 推荐使用:避免单继承局限性,灵活方便,方便同一个对象被多个线程使用
package com.lin.dome1;

/**
 * 多个线程同时操作同一个对象
 * 买火车票的例子
 *
 * @author LCW
 * @since 2020/11/9 16:07
 **/
public class TestThread4 implements Runnable {

    //票数
    private int ticketNums = 10;
    @Override
    public void run() {
        while (true) {
            if (ticketNums <= 0) {
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "==>拿到了第" + ticketNums-- + "票");
        }
    }

    public static void main(String[] args) {
        TestThread4 testThread4 = new TestThread4();
        new Thread(testThread4).start();
        new Thread(testThread4).start();
        new Thread(testThread4).start();
    }
}

线程实现方式三:实现callable方法


/**
 * 线程实现方式三:实现callable方法
 *
 * @author LCW
 * @since 2020/11/9 16:33
 **/
public class TestCallable implements Callable<Boolean> {

    private String url;
    private String name;

    public TestCallable(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public Boolean call() {
        webDownLoader webDownLoader = new webDownLoader();
        webDownLoader.downloader(url, name);
        System.out.println("系啊在了文件名为:" + name);
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable t1 = new TestCallable("https://i1.hdslb" +
                ".com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg", "image.jpg");
        TestCallable t2 = new TestCallable("https://i1.hdslb" +
                ".com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg", "image1.jpg");
        TestCallable t3 = new TestCallable("https://i1.hdslb" +
                ".com/bfs/face/83bb511365da513c55aa3d1958524f3b7db40684.jpg", "image2.jpg");


        ExecutorService executorService = Executors.newFixedThreadPool(3);

        Future<Boolean> future1 = executorService.submit(t1);
        Future<Boolean> future2 = executorService.submit(t2);
        Future<Boolean> future3 = executorService.submit(t3);

        System.out.println(future1.get());
        System.out.println(future2.get());
        System.out.println(future3.get());

        executorService.shutdownNow();


    }

}

//下载器
class webDownLoader {
    //下载方法
    public void downloader(String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("IO异常");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值