Java_多线程

1.基本介绍

 

 

 

2.线程创建

 方式1:

 

package thread_;

//创建线程方式一:继承Thread类,重写run()方法,调用start开启线程
public class thread01 extends Thread {
    @Override
    public void run() {
        //run方法线程体
        for (int i = 0; i < 20; i++) {
            System.out.println("我在学习java" + i);
        }
    }

    public static void main(String[] args) {
        //main线程,主线程

        thread01 thread01 = new thread01();//创建一个多线程
        thread01.start();//开启线程

        for (int i = 0; i < 20; i++) {
            System.out.println("我在学习spring" + i);
        }
    }
}

使用多线程下载三张网图:

package thread_;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//联系thread,实现多线程同步下载图片
public class thread2 extends Thread {
    private String url;
    private String name;
    public thread2(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) {
        thread2 thread2 = new thread2("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png","照片1");
        thread2 thread3 = new thread2("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png","照片2");
        thread2 thread4 = new thread2("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png","照片3");

        thread2.start();
        thread3.start();
        thread4.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.out.println("IO异常,downloader方法出现异常");
        }
    }
}

方式2:

package thread_;

//创建线程方法2:实现runnable接口,重写run方法,调用start()
public class thread03 implements Runnable {
    @Override
    public void run() {
        //run方法线程体
        for (int i = 0; i < 2; i++) {
            System.out.println("我在打篮球-----");
        }

    }

    public static void main(String[] args) {
        //创建一个runnable接口的实现类对象
        thread03 thread03 = new thread03();

        //创建线程对象,通过线程对象来开启我们的线程
        new Thread(thread03).start();

        for (int i = 0; i < 1; i++) {
            System.out.println("我在学习java");
        }
    }
}

方式3:

 

package thread_.Callable;

import org.apache.commons.io.FileUtils;
import thread_.thread2;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;

/**
 * 线程创建方式三,实现callable接口
 * callable的好处
 * 1.可以定义返回值
 * 2.可以抛出异常
 *
 */
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://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png", "照片1");
        TestCallable t2 = new TestCallable("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png", "照片2");
        TestCallable t3 = new TestCallable("https://img-blog.csdnimg.cn/10d936d490d745e6bee95d25cbe5acbd.png", "照片3");

        //创建执行服务
        ExecutorService ser = Executors.newFixedThreadPool(3);

        //提交执行
        Future<Boolean> r1 = ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        Future<Boolean> r3 = ser.submit(t3);

        //获取结果
        boolean rs1 = r1.get();
        boolean rs2 = r2.get();
        boolean rs3 = r3.get();

        //关闭服务
        ser.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.out.println("IO异常,downloader方法出现异常");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值