Java线程池入门04


1. 提交任务的两种方式


  • executor
  • submit

2. executor


executor位于Executor接口中

java 复制代码
public interface Executor {
	void executor(Runnable command);
}

executor提交的是无返回值的任务

下面是一个具体的例子

java 复制代码
package LearnThreadPool;  
  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
  
/**  
 * 使用executor  
 * * @author cat  
 * @version 2025/2/25 11:13  
 * @since JDK17  
 */  
public class UseExecutor {  
    public static void m() {  
        System.out.println(Thread.currentThread().getName());  
    }  
  
    public static void main(String[] args) {  
        ExecutorService executorService = Executors.newSingleThreadExecutor();  
        executorService.execute(UseExecutor::m);  
        executorService.shutdown();  
    }  
}
输出 : 
pool-1-thread-1

3. submit


submit位于ExecutorService接口中

java 复制代码
public interface ExecutorService {
	<T> Future<T> submit(Callable<T> task);  
	<T> Future<T> submit(Runnable task, T result);  
	Future<?> submit(Runnable task);
}

Future也是一个接口,该接口定义了与任务执行结果相关的功能。

java 复制代码
public interface Future<V> {  
	boolean cancel(boolean mayInterruptIfRunning);  
  
    boolean isCancelled();  
  
    boolean isDone();  
  
    V get() throws InterruptedException, ExecutionException;  
  
    V get(long timeout, TimeUnit unit)  
        throws InterruptedException, ExecutionException, TimeoutException;  
}

5个方法的相关功能

下面是一个使用submit的具体例子

java 复制代码
package LearnThreadPool;  
  
import java.util.concurrent.ExecutionException;  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
import java.util.concurrent.Future;  
  
/**  
 * 使用submit  
 * * @author cat  
 * @version 2025/2/25 11:27  
 * @since JDK17  
 */  
public class UseSubmit {  
    private static ExecutorService executorService = Executors.newSingleThreadExecutor();  
    // 该方法可以实现Runnable接口  
    public static void m1() {  
        System.out.println("曲·启明");  
    }  
  
    public static void test01() {  
        Future<?> future = executorService.submit(UseSubmit::m1);  
        try {  
            Object object = future.get();  
            System.out.println(object); // null  
        } catch (InterruptedException e) {  
            throw new RuntimeException(e);  
        } catch (ExecutionException e) {  
            throw new RuntimeException(e);  
        }  
    }  
    public static void test02() {  
        Future<String> submit = executorService.submit(UseSubmit::m1, "任务完成");  
        try {  
            String string = submit.get();  
            System.out.println(string);  
        } catch (InterruptedException e) {  
            throw new RuntimeException(e);  
        } catch (ExecutionException e) {  
            throw new RuntimeException(e);  
        }  
    }  
    // 该方法可以实现Callable<String>接口  
    public static String m2() {  
        return "曲·启明" + " 露西亚·誓焰";  
    }  
    public static void test03() {  
        Future<String> submit = executorService.submit(UseSubmit::m2);  
        try {  
            String string = submit.get();  
            System.out.println(string);  
        } catch (InterruptedException e) {  
            throw new RuntimeException(e);  
        } catch (ExecutionException e) {  
            throw new RuntimeException(e);  
        }  
    }  
    public static void main(String[] args) {  
        test01();  
        System.out.println("---------------------------");  
        test02();  
        System.out.println("---------------------------");  
        test03();  
        executorService.shutdown();  
    }  
}

4. ExecutorSubmit的区别


相关推荐
gb421528714 分钟前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
一朵梨花压海棠go20 分钟前
html+js实现表格本地筛选
开发语言·javascript·html·ecmascript
曾经的三心草25 分钟前
Python2-工具安装使用-anaconda-jupyter-PyCharm-Matplotlib
android·java·服务器
蒋星熠25 分钟前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
Metaphor69226 分钟前
Java 高效处理 Word 文档:查找并替换文本的全面指南
java·经验分享·word
ChinaRainbowSea40 分钟前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程
stormsha41 分钟前
飞算JavaAI炫技赛电商系统商品管理模块的架构设计与实现
java·架构·鸿蒙系统
minh_coo42 分钟前
Spring框架事件驱动架构核心注解之@EventListener
java·后端·spring·架构·intellij-idea
翻滚丷大头鱼1 小时前
Java 集合Collection—List
java·开发语言
aramae1 小时前
C++ -- 模板
开发语言·c++·笔记·其他