由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 请问关于用threadPoolExecutor实现threadpool的问题?
相关主题
Can Java thread return a value?新手问一个多线程的问题
Java NIO 问题求教ExecutorCompletionService 中submit的task个数如何得到?
请教一个多线程的问题FrameWork of Thread application 1
thread signaling 的一个问题ZT: 关于性爱的多线程问题研究(一)
java thread question问个Thread 的问题,java certificate里的
Another ?: Re: Keep track # of threads能这么 create thread 吗?
问一个基础问题implements runable 和 extends thread
one multi-threading questionjava的接口runnable
相关话题的讨论汇总
话题: thread话题: runnable话题: count
进入Java版参与讨论
1 (共1页)
w*******e
发帖数: 285
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.
我不太理解的是概念上的问题,我的
l****u
发帖数: 2166
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.

x******o
发帖数: 4
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.
w*******e
发帖数: 285
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.

l****u
发帖数: 2166
5
yun
threat implementing runnable already...

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

x******o
发帖数: 4
6
是的
here is an example, http://developer.amd.com/article_print.jsp?id=83
还是看ExecutorService的源代码吧,如果你想搞得非常清楚。
m******t
发帖数: 2416
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吗?

1 (共1页)
进入Java版参与讨论
相关主题
java的接口runnablejava thread question
线程问题。Another ?: Re: Keep track # of threads
另一个入门问题。问一个基础问题
再问一个今天的面试题one multi-threading question
Can Java thread return a value?新手问一个多线程的问题
Java NIO 问题求教ExecutorCompletionService 中submit的task个数如何得到?
请教一个多线程的问题FrameWork of Thread application 1
thread signaling 的一个问题ZT: 关于性爱的多线程问题研究(一)
相关话题的讨论汇总
话题: thread话题: runnable话题: count