threadpool

ScheduledThreadPoolExecutor and corePoolSize 0?

橙三吉。 提交于 2019-12-03 23:30:33
I'd like to have a ScheduledThreadPoolExecutor which also stops the last thread if there is no work to do, and creates (and keeps threads alive for some time) if there are new tasks. But once there is no more work to do, it should again discard all threads. I naivly created it as new ScheduledThreadPoolExecutor(0) but as a consequence, no thread is ever created, nor any scheduled task is ever executed. Can anybody tell me if I can achieve my goal without writing my own wrapper around the ScheduledThreadpoolExecutor ? Thanks in advance! Alex V. Actually you can do it, but its non-obvious:

Android JAVA - Service return OutOfMemoryError exception after some while

别来无恙 提交于 2019-12-03 21:46:20
I am developing my first android app. I have been created a Service class which role is to check if any new information on an external webpage. The HTTP request and service work as i should, but after a while I get these OutOfMemoryError . Are someone able to see where the Service gather all that memory? Error message 1. java.lang.OutOfMemoryError: pthread_create (stack size 16384 bytes) failed: Try again at java.lang.VMThread.create(Native Method) at java.lang.Thread.start(Thread.java:1029) at org.apache.http.impl.conn.tsccm.AbstractConnPool.enableConnectionGC(AbstractConnPool.java:140) at

Use Python pool.map to have multiple processes perform operations on a list

一曲冷凌霜 提交于 2019-12-03 20:59:35
I'm trying to start 6 threads each taking an item from the list files, removing it, then printing the value. from multiprocessing import Pool files = ['a','b','c','d','e','f'] def convert(file): process_file = files.pop() print process_file if __name__ == '__main__': pool = Pool(processes=6) pool.map(convert,range(6)) The expected output should be: a b c d e f Instead, the output is: f f f f f f What's going on? Thanks in advance. Part of the issue is that you are not dealing with the multiprocess nature of pool, (note that in Python, MultiThreading does not gain performance due to Global

how do I use key word arguments with python multiprocessing pool apply_async

不羁的心 提交于 2019-12-03 19:56:11
问题 I'm trying to get to grips with pythons multiprocessing module, specifically the apply_async method of Pool . I'm trying to call a function with arguments and keyword arguments. If I call the function without kwargs it's fine but when I try to add in a keyword argument I get: TypeError: apply_async() got an unexpected keyword argument 'arg2' Below is the test code that I'm running #!/usr/bin/env python import multiprocessing from time import sleep def test(arg1, arg2=1, arg3=2): sleep(5) if _

A very simple thread pool using pthreads in C++

怎甘沉沦 提交于 2019-12-03 18:30:34
问题 I'm trying to understand some of the basics of using POSIX pthreads. The kind of thing I need to do (eventually) is parallelize some computations, using a thread pool model. At present I want to ensure I have a very basic sense for how the POSIX pthread model works. So I'm trying to create the simplest thread pool that's general enough to do the kinds of things I want to do. There will be some shared memory, an input queue, and an output queue, and there will be mutexes protecting each. I've

Caused by: java.lang.OutOfMemoryError: Java heap space

大兔子大兔子 提交于 2019-12-03 17:28:30
MY GOAL: I want run my application for 1000 users. NOW I am trying to run for 100 user. During application run, I'd like to do some process for each user that will take a minimum of one hour per user, so I'm using one thread per user. ERROR Caused by: java.lang.OutOfMemoryError: Java heap space I've tried to figure out what this means, but I'm not really sure how to resolve it. Can anybody help me? Jean Logeart This error means that your program needs more memory than your JVM allowed it to use! Therefore you pretty much have two options: Increase the default memory your program is allowed to

Efficiently waiting for all tasks in a threadpool to finish

我只是一个虾纸丫 提交于 2019-12-03 16:56:18
问题 I currently have a program with x workers in my threadpool. During the main loop y tasks are assigned to the workers to complete, but after the tasks are sent out I must wait for all tasks for finish before preceding with the program. I believe my current solution is inefficient, there must be a better way to wait for all tasks to finish but I am not sure how to go about this // called in main after all tasks are enqueued to // std::deque<std::function<void()>> tasks void ThreadPool:

Will the ThreadLocal object be cleared after thread returned to Thread Pool?

让人想犯罪 __ 提交于 2019-12-03 16:32:39
问题 Will the contents that are stored in the ThreadLocal storage during an execution be cleared automatically when the thread is returned to ThreadPool (as would be expected) ?? In my application I am putting some data in ThreadLocal during some execution but if next time the same Thread is being used, then I am finding the obsolete data in ThreadLocal storage. 回答1: The ThreadLocal and ThreadPool don't interact with one another unless you do this. What you can do is a a single ThreadLocal which

多线程学习(6)ThreadPoolExecutor 线程池学习-1

為{幸葍}努か 提交于 2019-12-03 16:13:54
threadpool模型: 调用方通过调用api将任务,装进queue里,然后会有一个机制监事queue里有没有task,如果有task,就分配给某个worker去执行。workers代表线程池的话.worker就是某条线程了。 线程池的构造方法: Executor框架最核心的类是ThreadPoolExecutor,他是线程池的实现类,主要由下列7个组件构成。 package java.util.concurrent; public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null |

How to restart schedule when scheduleWithFixedDelay throws an exception?

泪湿孤枕 提交于 2019-12-03 16:13:21
I use ScheduledExecutorService to schedule some tasks which need to run periodically. I want to know whether this code works to recover the schedule when an exception happens. ScheduledExecutorService service = Executors.newScheduledThreadPool(1); this.startMemoryUpdateSchedule(service);//See below method //Recursive method to handle exception when run schedule task private void startMemoryUpdateSchedule(ScheduledExecutorService service) { ScheduledFuture<?> future = service.scheduleWithFixedDelay(new MemoryUpdateThread(), 1, UPDATE_MEMORY_SCHEDULE, TimeUnit.MINUTES); try { future.get(); }