Redian新闻
>
请问关于用threadPoolExecutor实现threadpool的问题?
avatar
请问关于用threadPoolExecutor实现threadpool的问题?# Java - 爪哇娇娃
w*e
1
我原来的一个简单的tcpserver是每次有连接就创建一个新的thread,就像这样
Socket connectionSocket = serverSocket.accept();
new myThread(connectionSocket, CoreDB).start();
现在想改成threadpool,本来想自己写,但是发现有一个非常简单的
ThreadPoolExecutor,用起来就象这样
Socket connectionSocket = serverSocket.accept();
threadPoolExecutor.execute(new myThread(connectionSocket));
help里面说ThreadPoolExecutor有最小count和max count,thread数量低于min count的
时候每次都创建新的thread,在min count和max count之间只有queue满的时候才会创建
新的thread,如果已经达到max count而且queue也满了就会转给rejecthandler.
我不太理解的是概念上的问题,我的
avatar
l*u
2
using thread pool, better call its threadFactory to create
any new threads bah

【在 w*******e 的大作中提到】
: 我原来的一个简单的tcpserver是每次有连接就创建一个新的thread,就像这样
: Socket connectionSocket = serverSocket.accept();
: new myThread(connectionSocket, CoreDB).start();
: 现在想改成threadpool,本来想自己写,但是发现有一个非常简单的
: ThreadPoolExecutor,用起来就象这样
: Socket connectionSocket = serverSocket.accept();
: threadPoolExecutor.execute(new myThread(connectionSocket));
: help里面说ThreadPoolExecutor有最小count和max count,thread数量低于min count的
: 时候每次都创建新的thread,在min count和max count之间只有queue满的时候才会创建
: 新的thread,如果已经达到max count而且queue也满了就会转给rejecthandler.

avatar
x*o
3
execute(java.lang.Runnable) expects a Runnable
you used a Thread class, :(
code will run but it is not the expected way of using ThreadPoolExector.
Check the source code of ThreadPoolExecutor, it uses a thread factory (
default or assigned one) to create reusable threads in the pool.
avatar
w*e
4
我现在把自己thread改成了implement runable了,是不是新建runable的cost要比新建
thread小很多? 但是每次执行的时候还是用一个threadpool当中的idle thread来新建
一个runable,如果我想完全可重复利用的话,我是不是应该自定义一个thread
factory,但是然后在execute里面执行什么呢?一个什么都不做的runable吗?

【在 x******o 的大作中提到】
: execute(java.lang.Runnable) expects a Runnable
: you used a Thread class, :(
: code will run but it is not the expected way of using ThreadPoolExector.
: Check the source code of ThreadPoolExecutor, it uses a thread factory (
: default or assigned one) to create reusable threads in the pool.

avatar
l*u
5
yun
threat implementing runnable already...

【在 w*******e 的大作中提到】
: 我现在把自己thread改成了implement runable了,是不是新建runable的cost要比新建
: thread小很多? 但是每次执行的时候还是用一个threadpool当中的idle thread来新建
: 一个runable,如果我想完全可重复利用的话,我是不是应该自定义一个thread
: factory,但是然后在execute里面执行什么呢?一个什么都不做的runable吗?

avatar
m*t
7
Are you actually doing a lot of things in your Runnable constructor? If not
, just new Runnable every time. It's not worth it to poll Runnable
instances, because then you'd have to worry about properly cleaning up the
state of your Runnable for its next use.

【在 w*******e 的大作中提到】
: 我现在把自己thread改成了implement runable了,是不是新建runable的cost要比新建
: thread小很多? 但是每次执行的时候还是用一个threadpool当中的idle thread来新建
: 一个runable,如果我想完全可重复利用的话,我是不是应该自定义一个thread
: factory,但是然后在execute里面执行什么呢?一个什么都不做的runable吗?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。