java - How do you limit threads in the ExecutorService? -


i use executorservice run many tasks in different threads. sometimes, many runnable instances waiting in thread pool may cause out of memory problem.

i try write blocking job executor solve it. there official solution ?

for example:

    blockingjobexecutor executor = new blockingjobexecutor(3);     (int = 0; < 1000; i++) {         executor.addjob(new runnable() {              @override             public void run() {                 try {                     thread.sleep(1000);                 } catch (interruptedexception e) {                     e.printstacktrace();                 }                 logfactory.getlog(btest.class).info("test " + system.currenttimemillis());             }         });     }     executor.shutdown(); 

here blockingjobexecutor class:

import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.timeunit; import java.util.concurrent.atomic.atomicinteger;  public class blockingjobexecutor {      atomicinteger counter = new atomicinteger();     executorservice service;     int threads;      public blockingjobexecutor(int threads) {         if (threads < 1) {             throw new illegalargumentexception("threads must greater 1.");         }         service = executors.newfixedthreadpool(threads);         this.threads = threads;     }      static class jobwrapper implements runnable {         blockingjobexecutor executor;         runnable job;          public jobwrapper(blockingjobexecutor executor, runnable job) throws interruptedexception {             synchronized (executor.counter) {                 while (executor.counter.get() >= executor.limit()) {                     executor.counter.wait();                 }             }             this.executor = executor;             this.job = job;         }          @override         public void run() {             try {                 job.run();             } {                 synchronized (executor.counter) {                     executor.counter.decrementandget();                     executor.counter.notifyall();                 }             }         }     }      public int limit() {         return threads;     }      public void shutdown() {         service.shutdown();         try {             service.awaittermination(long.max_value, timeunit.milliseconds);         } catch (interruptedexception e) {             throw new runtimeexception(e);         }     }      public void addjob(runnable job) {         try {             service.execute(new jobwrapper(this, job));         } catch (interruptedexception e) {             throw new runtimeexception(e);         }     }  } 

there 2 ways can happen. can have many runnables queued waiting run, or many threads running @ same time. if there many jobs queued up, can use fixed size blockingqueue in executorservice limit number of items can queued up. when try queue new task, operation block until there room in queue.

if there many threads running @ once, can limited how many threads available run tasks in executorservice calling executors.newfixedthreadpool number of threads want.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -