threadpool

How Does a Cached Thread Pool Reuse Existing Threads

ぃ、小莉子 提交于 2019-11-30 08:21:12
I've just started looking at Java's Executors class and the newCachedThreadPool( ) method. According to the API, the resulting thread pool reuses existing Thread objects for new tasks. I'm a bit puzzled how this is implemented because I couldn't find any method in the Thread API that lets you set the behaviour of an existing Thread object. For example, you can create a new Thread from a Runnable object, which makes the Thread call the Runnable 's run( ) method. However, there is no setter method in the Thread API that takes a Runnable as an argument. I'd appreciate any pointers. Executors does

Reasonable number of threads for thread pool running web service requests

删除回忆录丶 提交于 2019-11-30 08:19:05
When creating an FixedThreadPool Executor object in Java you need to pass an argument describing the number of threads that the Executor can execute concurrently. I'm building a service class that's responsibility is to process a large collections of phone numbers. For each phone number I need to execute web service (that's my bottleneck) and then save response in a hashmap. To make this bottleneck less harmful to the performance of my service I've decided to create Worker class which fetches unprocessed elements and processes them. Worker class implements Runnable interface and I run Workers

Java Cached thread pool and thread local

回眸只為那壹抹淺笑 提交于 2019-11-30 07:30:32
问题 I have a question about java and concurrency. Let say I have a ThreadLocal variable called 'a'. And I use a CachedThreadPool to obtain new threads. When a thread is reused, what happens to the ThreadLocal variable 'a'? It maintains the same value (cause it is the same thread) or it starts empty (as if the thread was new)? 回答1: By default ThreadLocals are reused along with the thread. If you need them to be be reinitialized you can do so by overriding the methods noted below: from javadoc for

Active Thread Number in Thread Pool

旧巷老猫 提交于 2019-11-30 07:25:22
When I write the below code, why do I get avaliable Thread number like 1022, 1020. I have to get 25 thread max as I am using thread pool. I guess the ouput thread number is the avaliable threads on the system. I need to get the avaliable thread number in my thread pool, in win form application. private void Foo() { int intAvailableThreads, intAvailableIoAsynThreds; // ask the number of avaialbe threads on the pool, //we really only care about the first parameter. ThreadPool.GetAvailableThreads(out intAvailableThreads, out intAvailableIoAsynThreds); // build a message to log string strMessage =

程序员笔记|编写高性能的Java代码需要注意的4个问题

倾然丶 夕夏残阳落幕 提交于 2019-11-30 07:11:22
一、并发 Unable to create new native thread …… 问题1:Java中创建一个线程消耗多少内存? 每个线程有独自的栈内存,共享堆内存 问题2:一台机器可以创建多少线程? CPU,内存,操作系统,JVM,应用服务器 我们编写一段示例代码,来验证下线程池与非线程池的区别: //线程池和非线程池的区别 public class ThreadPool { public static int times = 100;//100,1000,10000 public static ArrayBlockingQueue arrayWorkQueue = new ArrayBlockingQueue(1000); public static ExecutorService threadPool = new ThreadPoolExecutor(5, //corePoolSize线程池中核心线程数 10, 60, TimeUnit.SECONDS, arrayWorkQueue, new ThreadPoolExecutor.DiscardOldestPolicy() ); public static void useThreadPool() { Long start = System.currentTimeMillis(); for (int i = 0; i <

ThreadPoolExecutor with unbounded queue not creating new threads

江枫思渺然 提交于 2019-11-30 07:05:46
问题 My ThreadPoolExecutor is failing to create new threads. In fact I wrote a somewhat hacky LinkedBlockingQueue that will accept any task (i.e. it is unbounded) but call an additional handler - which in my application spews warning trace that the pool is behind - which gives me very explicit information that the TPE is refusing to create new threads even though the queue has thousands of entries in it. My constructor is as follows: private final ExecutorService s3UploadPool = new

understanding InvalidAsynchronousStateException occurrences

≯℡__Kan透↙ 提交于 2019-11-30 06:39:25
When does InvalidAsynchronousStateException get thrown? I have the following piece of code: control.InvokeRequired ? control.Invoke(expression) : expression(); In some random cases I get InvalidAsynchronousStateException and my application hangs, after doing some reading it seems to be that this exception will be thrown when the thread where the control was created finished. Is this correct? If so, this doesn't seem to be the case, unless something is making my application crash and this exception is just a consequence? is this possible? System.ComponentModel.InvalidAsynchronousStateException:

How to log correct context with Threadpool threads using log4net?

▼魔方 西西 提交于 2019-11-30 05:19:14
I am trying to find a way to log useful context from a bunch of threads. The problem is that a lot of code is dealt with on Events that are arriving via threadpool threads (as far as I can tell) so their names are not in relation to any context. The problem can be demonstrated with the following code: class Program { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); static void Main(string[] args) { new Thread(TestThis).Start("ThreadA"); new Thread(TestThis).Start("ThreadB"); Console.ReadLine(); } private

How to create LIFO executor?

让人想犯罪 __ 提交于 2019-11-30 05:06:53
I would like to create a thread pool which will execute the most recently submitted task. Any advice on how to accomplish this? Thank you andersoj You could probably just implement your own BlockingQueue wrapper that maps offer/poll to a stack. Then use this as the BlockingQueue implementation you pass to a ThreadPoolExecutor . My suggestion would be to wrap one of the existing Deque implementations such as ArrayDeque . This is not synchronized, so you'll need to wrap each of the BlockingQueue methods with a synchronizer (if not something more exotic). You'll also need to introduce wait /

Async await and threads [duplicate]

泄露秘密 提交于 2019-11-30 04:59:58
问题 This question already has answers here : async - stay on the current thread? (3 answers) Closed 3 years ago . I am working with async-await and tasks, but I can't understand the one thing: Is async task executes in separate thread? As msdn says (Asynchronous programming): The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. But in the remarks in description of ThreadPool