Thread类创建、Runnable接口和Callable接口实现多线程

1.Thread类创建
通过新建thread类继承Thread父类,并重写run方法,其中使用common-io的jar包实现一个下载器,主函数中创建3个线程,并用start函数执行。

public class thread extends Thread{
    private String url;
    private String name;
    public thread(String url,String name){
        this.url = url;
        this.name = name;
    }
    @Override
    public void run() {
        WebDownloader wd = new WebDownloader();
        wd.downloader(url,name);
        System.out.println("name:"+name);
    }
    public static void main(String[] args) {
        thread t1 = new thread("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片1.jpg");
        thread t2 = new thread("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片2.jpg");
        thread t3 = new thread("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片3.jpg");
        t1.start();
        t2.start();
        t3.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异常");
        }
    }
}

结果如下,可以看出是并行执行的。
结果
2.Runnable接口实现
同理,runnable接口实际上是使用new Thread(Runnable实例).start()来实现的。

public class runnable implements Runnable{
    private String url;
    private String name;
    public runnable(String url,String name){
        this.url = url;
        this.name = name;
    }
    @Override
    public void run() {
        WebDownloader1 wd = new WebDownloader1();
        wd.downloader(url,name);
        System.out.println("name:"+name);
    }
    public static void main(String[] args) {
        runnable t1 = new runnable("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片1.jpg");
        runnable t2 = new runnable("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片2.jpg");
        runnable t3 = new runnable("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片3.jpg");
        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();
    }
}
class WebDownloader1{
    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异常");
        }
    }
}

3.Callable实现多线程

public class call implements Callable<Boolean> {
    private String url;
    private String name;

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

    }
    @Override
    public Boolean call() {
        WebDownloader wd = new WebDownloader();
        wd.downloader(url,name);
        System.out.println("name:"+name);
        return true;
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        call t1 = new call("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片1.jpg");
        call t2 = new call("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片2.jpg");
        call t3 = new call("https://exp-picture.cdn.bcebos.com/16a84fe10ef85856d946959753e9ccd2ba66cdcd.jpg","图片3.jpg");
        //创建服务
        ExecutorService es = Executors.newFixedThreadPool(3);

        Future<Boolean> r1 = es.submit(t1);
        Future<Boolean> r2 = es.submit(t2);
        Future<Boolean> r3 = es.submit(t3);
        
        boolean rs1 = r1.get();
        boolean rs2 = r2.get();
        boolean rs3 = r3.get();
        
        es.shutdown();
    }
}

Callable方法主要是借助创建一个服务来提交实现多线程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值