Home > Java > javaTutorial > How to solve Java method invocation rejected error exception (MethodInvocationRejectedErrorExceotion)

How to solve Java method invocation rejected error exception (MethodInvocationRejectedErrorExceotion)

WBOY
Release: 2023-08-19 20:37:49
Original
950 people have browsed it

How to solve Java method invocation rejected error exception (MethodInvocationRejectedErrorExceotion)

How to solve the Java method invocation rejected error exception (MethodInvocationRejectedErrorExceotion)

Introduction:
In Java programming, when we use multi-threading or thread pool to execute When calling a method, you may sometimes encounter a method invocation rejected error exception (MethodInvocationRejectedErrorExceotion). This exception prevents the thread from properly executing the method and may cause the program to terminate. This article describes how to resolve this exception and provides corresponding code examples.

Exception reason:
Method call rejected error exception is usually caused by resource limitations of the thread pool. When the number of threads in the thread pool reaches the maximum limit and a new method call request comes in, the thread pool will refuse to call the method and throw an exception.

Solution:
The following are several solutions, you can choose the solution that suits you according to the specific situation.

  1. Increase the capacity of the thread pool:
    This exception can be solved by increasing the maximum number of threads in the thread pool. This can be achieved by modifying the configuration file of the thread pool or setting relevant parameters in the code. The following is a specific code example:
ExecutorService executor = Executors.newFixedThreadPool(10);   //创建一个固定大小为10的线程池
((ThreadPoolExecutor)executor).setMaximumPoolSize(20);   //增加最大线程数量为20
Copy after login
  1. Use a non-blocking thread pool:
    You can use a non-blocking thread pool to solve this exception. A non-blocking thread pool will return unprocessed tasks to the caller instead of throwing an exception when the maximum number of threads is reached. The following is a specific code example:
ThreadPoolExecutor executor = new ThreadPoolExecutor(
    10,  //核心线程数量
    20,  //最大线程数量
    60,  //线程保持活跃时间
    TimeUnit.SECONDS,  //活跃时间的单位
    new LinkedBlockingQueue<Runnable>(),  //任务队列
    new ThreadPoolExecutor.CallerRunsPolicy());  //非阻塞策略,将未处理的任务返回给调用方
Copy after login
  1. Use bounded queue:
    Setting the task queue of the thread pool to a bounded queue can prevent the task queue from growing infinitely, causing threads to The pool is unable to accept new tasks. The following is a specific code example:
ThreadPoolExecutor executor = new ThreadPoolExecutor(
    10,  //核心线程数量
    20,  //最大线程数量
    60,  //线程保持活跃时间
    TimeUnit.SECONDS,  //活跃时间的单位
    new ArrayBlockingQueue<Runnable>(100));  //有界任务队列,最大容量为100
Copy after login
  1. Using the rejection policy:
    You can handle the situation where the method call is rejected by setting the rejection policy. The rejection policy determines what to do when the thread pool refuses to accept new tasks. The following is a simple code example:
ThreadPoolExecutor executor = new ThreadPoolExecutor(
    10,  //核心线程数量
    20,  //最大线程数量
    60,  //线程保持活跃时间
    TimeUnit.SECONDS,  //活跃时间的单位
    new LinkedBlockingQueue<Runnable>(),  //任务队列
    new RejectedExecutionHandler() {   //自定义拒绝策略
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            //执行自定义的处理逻辑,例如日志记录或抛出自定义异常
        }
    });
Copy after login

Summary:
When encountering a Java method call rejected error exception (MethodInvocationRejectedErrorExceotion), we can increase the thread pool capacity and use non-blocking threads Pool, use a bounded queue, or set a deny policy to resolve this exception. Choosing the appropriate solution based on the specific situation can ensure that the thread pool can handle all method call requests normally and improve the stability and reliability of the program.

Reference:

  • Java thread pool documentation: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/ util/concurrent/ThreadPoolExecutor.html
  • Java thread pool tutorial: https://www.baeldung.com/thread-pool-java-and-guava

The above is the detailed content of How to solve Java method invocation rejected error exception (MethodInvocationRejectedErrorExceotion). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template