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的区别


相关推荐
M1A12 分钟前
Java集合框架深度解析:LinkedList vs ArrayList 的对决
java·后端
Tanecious.2 分钟前
C++--红黑树
开发语言·c++
Top`6 分钟前
Java 泛型 (Generics)
java·开发语言·windows
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ31 分钟前
如何使用Java WebSocket API实现客户端和服务器端的通信?
java·开发语言·websocket
Shartin37 分钟前
Can201-Introduction to Networking: Application Layer应用层
服务器·开发语言·php
是小崔啊44 分钟前
tomcat源码02 - 理解Tomcat架构设计
java·tomcat
没有bug.的程序员1 小时前
JAVA面试宝典 -《安全攻防:从 SQL 注入到 JWT 鉴权》
java·安全·面试
栈溢出了1 小时前
MyBatis实现分页查询-苍穹外卖笔记
java·笔记·mybatis
morningcat20181 小时前
java17 gc笔记
java·jvm·笔记
共享家95271 小时前
linux_线程概念
linux·开发语言·jvm