-2

when i invoke threadExecutor method large number

  fun startAnimation(): Unit {

    sPool.scheduleAtFixedRate(taskQu, 0, 1000, TimeUnit.MILLISECONDS)       
}

it will cause this error prompt , sPool is java ThreadExecutors

val sPool = Executors.newScheduledThreadPool(2)!!

and taskQuis async timer task

 val taskQu = object : TimerTask() {
    override fun run() {

        uihandler.obtainMessage().sendToTarget()

    }
}

i got this prompt

java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
                                                                      at java.lang.Thread.nativeCreate(Native Method)
                                                                      at java.lang.Thread.start(Thread.java:1063)
                                                                      at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:921)
                                                                      at java.util.concurrent.ThreadPoolExecutor.ensurePrestart(ThreadPoolExecutor.java:1556)
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:310)
                                                                      at java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(ScheduledThreadPoolExecutor.java:567)

so , where this problem about this issue emerge?

isicout
  • 57
  • 1
  • 6

1 Answers1

1

I did the same thing using the below program and it is working fine:

public class ThreadExecutionTest {

/**
 * @param args
 */
ScheduledExecutorService executor=Executors.newScheduledThreadPool(2);


public static void main(String[] args) {
    // TODO Auto-generated method stub
    ThreadExecutionTest test=new ThreadExecutionTest();
    test.executeThread();

}

public void executeThread() {
    Runnable task=()-> System.out.println("executing thread "+Thread.currentThread().getName() );
    executor.scheduleAtFixedRate(task, 0, 1000, TimeUnit.MILLISECONDS);

}

}

The exception you are getting is OutofMemoryError as multiple thread are getting created after every second, so after sometime it's throwing this error. Please check where you have declared "val sPool = Executors.newScheduledThreadPool(2)!!", if this is declared inside method then remove it and declare it outside the method.(declare it in the class level).

Imran Ahmad
  • 206
  • 1
  • 13
  • sure that `val sPool = Executors.newScheduledThreadPool(2)!` is outside this method , and t have follow your suggest modify my code , this error happend odds is a lot less. but still happend, – isicout Jul 24 '17 at 09:46
  • i have change follow code `time.scheduleAtFixedRate(taskQu, 0, 1000)` , `val time=Timer()`, this error not happend and you can try , but thank you a lot !!!! – isicout Jul 24 '17 at 09:48