I'm having a system with hundreds of threads. Most of the thread are sleeping or waiting in a given time but they can wake up whenever they like. I would like to reduce the number of OS threads that are dedicated to my system. Do you know about a simple way of doing it? For example, is there a thread pool package that whenever a thread moves to sleep mode, it stores the state and kill the thread开发者_开发技巧. whenever it wakes up, it starts new thread with the state of the old one.
Thanks
Are you looking for something like ThreadPoolExecutor?
An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.
Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.
The thing you described is basically what thread is.
Now, you may know that your application logic only depends on a few variables, not everything on the thread stack. You only needs these few variables to recover from sleep. VM and OS can't know that, and they can't help you.
You must do it yourself. When your thread is about to retire, wrap the essential state up and store it on a queue. Then exit thread, or return it to a thread pool.
When a certain condition is met, lookup the state from the queue, create a new task based on it, and run the task on a new thread.
It's my understanding that you're better off letting the thread sleep, due to the overhead of initializing a new thread. When the thread is sleeping, it won't be scheduled on the CPU, so I guess I don't really see the concern.
精彩评论