线程创建--实现Callable接口

步骤

  • 实现Callable接口,需要返回值类型
  • 重写call方法,需要抛出异常
  • 创建目标对象
  • 创建执行服务
    ExecutorService executorService = Executors.newFixedThreadPool(3);
    这是一个线程池,3,代表这个线程池里面可以有三个线程
  • 提交执行
    Future t1 = executorService.submit(testThread01);
  • 获取结果
    Boolean Boolean1 = t1.get();
  • 关闭服务
    executorService.shutdown();
package com.yf.demo02;

import com.yf.demo1.TestThread01;
import org.apache.commons.io.FileUtils;

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

//线程创建方式三:实现Callable接口
public class TestCallable implements Callable<Boolean> {
    private String name;//网络图片地址
    private String url;//保存的文件名

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


    @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 testThread01 = new TestCallable("https://maven.apache.org/images/maven-logo-black-on-white.png","1.jpg");
        TestCallable testThread02 = new TestCallable("https://maven.apache.org/images/apache-maven-project.png","2.jpg");
        TestCallable testThread03 = new TestCallable("https://maven.apache.org/images/logos/maven-feather.png","3.jpg");
        //创建执行服务:线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        //提交执行
        Future<Boolean> t1 = executorService.submit(testThread01);
        Future<Boolean> t2 = executorService.submit(testThread02);
        Future<Boolean> t3 = executorService.submit(testThread03);
        //获取结果
        Boolean Boolean1 = t1.get();
        Boolean Boolean2 = t2.get();
        Boolean Boolean3 = t3.get();
        //关闭服务
        executorService.shutdown();
    }
}

class WebDownLoader{
    public void downLoader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
实现Callable接口创建线程,你需要按照以下步骤进行: 1. 创建一个类,并实现Callable接口。在这个类中,你需要重写call()方法,这个方法会在新线程中运行。 2. 创建一个ExecutorService对象。这个对象会管理线程池,并且可以让你轻松地提交Callable任务。 3. 使用ExecutorService的submit()方法提交Callable任务,并获得一个Future对象。这个对象可以用来检查任务的状态和获取任务的返回值。 下面是一个示例代码: ```java import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class MyCallable implements Callable<Integer> { @Override public Integer call() throws Exception { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } return sum; } public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<Integer> future = executorService.submit(new MyCallable()); System.out.println("计算中..."); Integer result = future.get(); System.out.println("计算结果为:" + result); executorService.shutdown(); } } ``` 在这个示例代码中,MyCallable实现Callable接口,并在call()方法中计算1到100的和。然后,在main()方法中,我们创建了一个单线程的ExecutorService对象,并使用submit()方法提交MyCallable任务。我们使用Future对象获取任务的返回值,并在控制台中输出了结果。最后,我们调用了executorService对象的shutdown()方法来关闭线程池。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值