java.lang.OutOfMemoryError:pthread_创建(1040KB堆栈)失败:重试
当我调用threadExecutor方法时,需要大量java.lang.OutOfMemoryError:pthread_创建(1040KB堆栈)失败:重试,java,android,kotlin,Java,Android,Kotlin,当我调用threadExecutor方法时,需要大量 fun startAnimation(): Unit { sPool.scheduleAtFixedRate(taskQu, 0, 1000, TimeUnit.MILLISECONDS) } 它将导致此错误提示,sPool是java线程执行器 val sPool=Executors.newScheduledThreadPool(2) 而taskQu是异步定时器任务 val taskQu = object :
fun startAnimation(): Unit {
sPool.scheduleAtFixedRate(taskQu, 0, 1000, TimeUnit.MILLISECONDS)
}
它将导致此错误提示,sPool
是java线程执行器
val sPool=Executors.newScheduledThreadPool(2)代码>
而taskQu
是异步定时器任务
val taskQu = object : TimerTask() {
override fun run() {
uihandler.obtainMessage().sendToTarget()
}
}
我收到这个提示
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)
那么,这个问题在哪里出现的呢?我使用下面的程序做了同样的事情,它工作正常:
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);
}
}
您得到的异常是OutofMemoryError,因为每秒都会创建多个线程,所以过了一段时间它就会抛出这个错误。
请检查声明“val sPool=Executors.newScheduledThreadPool(2)!!”的位置,如果这是在方法内部声明的,则将其删除并在方法外部声明。(在类级别声明) @piet.t可能是,但这是重复的内存不足问题,OP可以找到很多线程等等this@piet.t我懂了。我收回了对此的投票。确保val sPool=Executors.newScheduledThreadPool(2)
超出了此方法,并且我没有按照您的建议修改我的代码,发生此错误的几率要小得多。但是仍然发生了,我已经更改了跟踪代码时间。scheduleAtFixedRate(taskQu,0,1000)
,val time=Timer()
,这个错误没有发生,您可以尝试,但非常感谢!!!!