topics

全部话题 - 话题: threadpool
1 2 3 末页 (共3页)
g*********9
发帖数: 1285
1
应用是基于high throughput messaging的,来一个message,处理一下,处理时间非常
短, 然后发一个message回去作为response.
假定有一个固定大小(N)的ThreadPool.
方法一:来一个message,往ThreadPool里送一个Runnable,假定ThreadPool有足够大
的queue.
方法二:在Threadpool里起N个Thread, 每一个Thread配一个Blocking Queue并负责处
理Queue里的message.起一个单独的Thread收message, 把message均匀的放到各个queue
里面。
比较:
1 实现简单,错误处理容易,比较robost,效率一般,因为要不停的创建Runnable,
overhead大一些
2 实现复杂一些,要保证Thread永远不Crash,效率应该比一高。
大侠门可以补充一下。
w*******e
发帖数: 285
2
我原来的一个简单的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.
我不太理解的是概念上的问题,我的
w*******e
发帖数: 285
3
我现在把自己thread改成了implement runable了,是不是新建runable的cost要比新建
thread小很多? 但是每次执行的时候还是用一个threadpool当中的idle thread来新建
一个runable,如果我想完全可重复利用的话,我是不是应该自定义一个thread
factory,但是然后在execute里面执行什么呢?一个什么都不做的runable吗?
g*****g
发帖数: 34805
4
方法三,起两个 threadpool, 一个干调度,一个干活。基本上所有的系统都是这样。

queue
p*****2
发帖数: 21240
5
那应该怎么搞
我也是担心这个
一个threadpool怎么能不影响另一个
我做的话会把任务distribute出去
g*********9
发帖数: 1285
6
来自主题: Programming版 - 这个算不算Java ThreadPool的bug
如果function里用一个local的ThreadPool,用完了不call shutdown(), memory leak
没商量。还是特难查那种,都是ThreadGroup造成的。
c*********e
发帖数: 16335
7
来自主题: Programming版 - 这个算不算Java ThreadPool的bug
local为啥要用ThreadPool?

leak
g*********9
发帖数: 1285
8
来自主题: Programming版 - 这个算不算Java ThreadPool的bug
local为啥不能用ThreadPool?
m****o
发帖数: 182
9
来自主题: Programming版 - 这个算不算Java ThreadPool的bug
纯local的调用用一个Thread就搞定了,为啥要用ThreadPool?不过如果你坚持要用的
话,这个就跟资源管理一回事,你开了一个文件的读写句柄,能够最后不关掉吗?

leak
w****e
发帖数: 1883
10
来自主题: Programming版 - 这个算不算Java ThreadPool的bug
想不出来怎么能memory leak,你的function都退出了,从root到threadpool都没有路
径了。要不把code贴出来看看?
g*********9
发帖数: 1285
11
来自主题: Programming版 - 这个算不算Java ThreadPool的bug
ThreadGroup在root set里。每个Thread自动加到ThreadGroup,只有ThreadPool
shutdown()才能从把Thread从ThreadGroup里踢出去。
v****s
发帖数: 1112
12
来自主题: Programming版 - 多线程优化求助! (转载)
我看了一下threadpool,觉得很有用。 我想这样用:
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(poolSize,
maxPoolSize, keepAliveTime, TimeUnit.SECONDS, queue);
for(i = 0; i for(j = 0; j while(threadPool.getActiveCount < poolSize){
threadPool.execute( computeRij(a[i], b[j] ) );
}
}
}
但是可能中间这个while会占用太多的cpu时间因为很多时候都在空循环。怎样才能把这
个threadpool用好呢?我感觉关键在于如何知道哪个thread在什么时候complete了。谢谢!!
l******8
发帖数: 9475
13
来自主题: THU版 - 百年校庆年级联系人查询
500 Servlet Exception
[show] java.lang.NullPointerException
java.lang.NullPointerException
at com.whaty.platform.database.oracle.dbpool.executeQuery(dbpool.java:99)
at com.whaty.platform.database.oracle.standard.info.OracleInfoList.
getNewsByTags(OracleInfoList.java:1100)
at com.whaty.platform.database.oracle.standard.info.
OracleNormalInfoManage.getNewsByTags(OracleNormalInfoManage.java:234)
at _jsp._lishi__jsp._jspService(left.jsp:205)
at com.caucho.jsp.JavaPage.service(Jav... 阅读全帖
g*****g
发帖数: 34805
14
来自主题: Java版 - 求思路
This is wrong. container manages its own threadpool. But it doesn't prevent
you from using your own threadpool for other tasks.
Spring scheduler, for example, runs in its own threadpool.
g*****g
发帖数: 34805
15
来自主题: Java版 - 工作中遇到的并行处理问题
I think the problem is overstated. When the predictors are kept in a
separate blocking queue, they don't have to have the same life cycle as
threadpool, which gives you more flexiblity. And if you want to tie them,
you can always override shutdown method of the threadpool. Also Predictor is
not affected by predict calls, I don't see how they can be corrupted.
In fact, in a complicated server app, this separation makes it possible to
send these tasks to a generic threadpool.

framework
g*****g
发帖数: 34805
16
来自主题: Programming版 - 如何快速处理大量网上xml文件?
返回Future不是blocking的,调用Future.get才是Blocking的,你自己钻进牛角尖了。
那个onCompleted一般是这么写的。
onComplete(Response response) {
parserExecutorService.submit(new Task(response));
return response;
}
把异步返回的payload扔进parse的threadpool就完了,整个过程都是non-blocking的,
瓶颈纯粹取决于下载速度。实现上就是epoll发现完成了,trigger一个event产生一个
job扔进Async httpclient内建的threadpool,因为这个job没IO也不blocking会立刻完
成。到这里跟Node.js都是相似的。
parserExecutorService是另一个threadpool 同步处理支持多线程,在这里Java秒杀
了Node.
如果Future不能异步只能阻塞等,那还要Future干什么?get()是一个异步变同步的办
法而已。
a********d
发帖数: 195
17
来自主题: JobHunting版 - 面经
废了上次面试之后挺久没有电面了,schedule今天的电面是一家比较中型的公司。面的
开始还好,后面问的一度郁闷。居然没有算法题(汗),全是基础知识,得复习啊。
12分钟等待-!---!------!-----
1.Introduce yourself and your experience and why you apply for this position
, how to prove that you are qualified.边翻我简历边侃,之前我的简历肯定完全没
看,一路嗯下来。
2.Struct and class difference, conditions to use.
3.Process and thread, communications of each. Dead lock conditions and
avoidance method.
4.ThreadPool in C#, why you want to use it, when, and how it know one thread
is terminated? What's the limit of... 阅读全帖
m****c
发帖数: 11
18
来自主题: JobHunting版 - fb面经
threadpool是只需要写post method吗?还是需要写完整的threadpool从
initialization开始啊?
z****e
发帖数: 54598
19
来自主题: JobHunting版 - T a b l e a u 昂塞特面经
发现他们backend很喜欢考多线程
而且对于eventloop,异步这一块有偏好
虽然threadpool也可以,但是现在好像不怎么流行用threadpool
最新的都是一个core对应一个thread,这样便于scale out
最新成果是quasar,这个直接把同步搞成异步,用一个annotation就实现了
建议多看看vert.x,一天到晚就在搞这些东西
streaming我觉得没有必要
不过你要是连streaming都懂,这些都小意思了
streaming最好把rxjava,reactive这些先看看
不过他们也考得很实在,这种公司比较有前途,做的东西也靠谱
z****e
发帖数: 54598
20
来自主题: JobHunting版 - T a b l e a u 昂塞特面经
发现他们backend很喜欢考多线程
而且对于eventloop,异步这一块有偏好
虽然threadpool也可以,但是现在好像不怎么流行用threadpool
最新的都是一个core对应一个thread,这样便于scale out
最新成果是quasar,这个直接把同步搞成异步,用一个annotation就实现了
建议多看看vert.x,一天到晚就在搞这些东西
streaming我觉得没有必要
不过你要是连streaming都懂,这些都小意思了
streaming最好把rxjava,reactive这些先看看
不过他们也考得很实在,这种公司比较有前途,做的东西也靠谱
c**t
发帖数: 2744
21
来自主题: Database版 - 大虾帮忙看看MySQL的设置
[OK] Currently running supported MySQL version 5.6.30
[OK] Operating on 64-bit architecture
-------- Storage Engine Statistics -----------------------------------------
------------------------
[--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MEMORY +MRG_MYISAM
+MyISAM +PERFORMANCE_SCHEMA
[--] Data in MyISAM tables: 170K (Tables: 17)
[--] Data in InnoDB tables: 28M (Tables: 107)
[!!] Total fragmented tables: 5
-------- Security Recommendations ------------------------------------------
... 阅读全帖
C****n
发帖数: 2324
22
要看你的具体应用, 这个没有定论, 我也不知道你的MAINPROCESS是个什么东西.
我的布局是:
ASP.NET website/IIS
WINDOWS SERVICE/HOST WCF SERVICE
这样ASP.NET就是一个WCF CLIENT, call WCF HOST. 我的是ONE WAY的. 也就是撂给
WCFSERVICE就不管了, 不等结果的.
如果你一定要等结果, 而且处理时间不长的话, 最好把哪个WCF合并到ASP.NET里面吧 (
如果可能的话). 这样没那么多罗嗦事. 如果由于某种原因必须分开, 就两个PROCESS
挺好. ASP.NET/IIS CALL WINDOWS SERVICE/WCF
看来你其实是可以合在一起的, 而且网页用户是不用等结果的. 那就合在一起做吧.
你怕影响响应时间, 直接用threadPool 就行了.
ThreadPool.QueueUserItem(xxxx), 这样你的ASP.NET逻辑马上返回. 让你的任务处理
在IIS后台慢慢来吧.几个小时也没问题.
不过你真这样做就要注意不要随便重启IIS, 因为怕你处理一半你把... 阅读全帖
w*r
发帖数: 2421
23
Well, to solve this problem is totally depend on your implementation.
Generally, I would just use a sql execution threadpool and a sqlTaskQueue to
getInstances of the connection and release the instances of connection as soon
as possible in your transaction. Oracle has a connection implementation in its
JDBC pacakage. To gain the maximum throughput, the sql execution threadpool
could be tuned to keep running as fast as possible by keep its connection
instances and only release the connection whe
g*****g
发帖数: 34805
24
来自主题: Java版 - 求思路
You don't check the status of a thread, you check the status of a task.
Usually you'd use a threadpool, but you can use a simple timer task too. The
simplest way to do it may be like this, with Spring for example. @Scheduled
will use a default threadpool btw.
class MyService {
String status;

@RequestMapping(yourURL)
public getStatus(){return status;}
@Scheduled(yourSchedule)
public runTask {
//change status as you need.
}
}
g*****g
发帖数: 34805
25
来自主题: Java版 - 工作中遇到的并行处理问题
You init N threads threadpool, each thread is associated with a Predictor,
you simply pass your task to the threadpool.
r******g
发帖数: 138
26
来自主题: Java版 - Java NIO 问题求教
不太明白Event-driven为什么比socket+threadpool model 更能处理大量request
concurrently. 假如socket+threadpool 设置的pool size是10,而且每个thread 都是
long running job的话, 第11个request会block。对NIO来说,尽管只有一个thread
for event loop,每获得的event读和写也需要单独的thread来处理,如果也用thread
pool的话并且pool size也是10,那第11个request不也是要等前面10个request有一个
结束才被处理获得response?
r******g
发帖数: 138
27
来自主题: Java版 - Java NIO 问题求教
不太明白Event-driven为什么比socket+threadpool model 更能处理大量request
concurrently. 假如socket+threadpool 设置的pool size是10,而且每个thread 都是
long running job的话, 第11个request会block。对NIO来说,尽管只有一个thread
for event loop,每获得的event读和写也需要单独的thread来处理,如果也用thread
pool的话并且pool size也是10,那第11个request不也是要等前面10个request有一个
结束才被处理获得response?
S**********n
发帖数: 264
28
来自主题: Programming版 - 大牛们讨论一下异步编程吧
Async in .net is done through using system threadpool. Current recommended
model is task model, before this it is APM, and background worker. All of
them used QueueUserWorkItem behind the scene. That is not some thing new,
similar stuff was supported in VC6.0. Java has similar models.
For applications, Async has to go through a different thread. For .net, as
long as managed thread maps to native thread, async problem is still a
concurrent problem. Threadpool is not magic.
n**x
发帖数: 606
29
threadpool根本上就是多线程啊。 只不过没有了创建线程的开销罢了。我这500个都是
threadpool的thread啊。
g*****g
发帖数: 34805
30
threadpool就是 threadpool, 通常是 ExecutorService的子类。
z****e
发帖数: 54598
31

thread.
妈蛋,同样的原理,你用java做一个有啥难的?
直接上threadpool,java也有阿
ejb什么都是threadpool
z****e
发帖数: 54598
32

threadpool的数量跟jvm有毛关系,这个纯粹是代码level的事
你难道通过jvm的参数来调节threadpool?
y***n
发帖数: 1594
33
来自主题: JobHunting版 - Bloomberg C# developer interview.
今天和Bloomberg聊了一个小时,对方应该也是个中国人,问题都还可以,但是都很刨
根究底, 看你到底懂不懂。 没有算法的问题, 但是很多Multi-threading..
1> Reference Type/ Value type, can value type be on heap?
2> Static class.
3> Delegate/Event.
4> which thread does the delegate run on?
5> Can a strut has a default constructor?
6> Difference between mutex/monitor/semaphore.
7> When do you use the ThreadPool?
8> a lot, lot threading questions.....
下边这本书一定要读透, 所有的朋友都觉得这是 .Net bible.
http://www.amazon.com/CLR-via-Second-Pro-Developer/dp/0735621632
y***m
发帖数: 7027
34
来自主题: JobHunting版 - 不如总结些技巧和注意点
比如
threadpool 管理 thread
回调函数: 声明接口,运行时传入实现方式。
hashtable比较大时最好预定一下尺寸,不然低效,因为要重新hash计算
aop一般只能基于容器注入,否则不灵
单例考虑到多机时要额外考虑
threadlocal在线程内部控制应用
自己定制hiberate session factory,管理多数据源
........
还有webservice等...这些一般需要做过才知道,大家都总结后分享出来很节省摸索时间
d********w
发帖数: 363
35
来自主题: JobHunting版 - [vmware面经] software engineer
他们家老印太多了,不推荐去吧
1. threadpool design: 给出一些任务, 并发去处理,设计到queue排队等待,同步在
哪里加锁,线程池预先分配,饥饿和队列溢出的处理
2. 压缩算法,给一个16进制字节流如何压缩
0x 12 32 FD FD 32 43 43 43
3. 实现semaphore
4. 2-phase commit 要几次通信,3-phase commit呢,分布式系统中给一个信息,如何
最快传输到每个结点,gossip?
5. 给一个任意的path “/etc/abc/def/a.txt”去验证是否存在,如果是目录的话,假
设可以用getFiles()获取所有的当前文件和子目录,然后给一个父目录,/etc/, 遍历
所有的子目录和文件,判断是否文件句柄有效,这道题也有些模糊了。
d********w
发帖数: 363
36
来自主题: JobHunting版 - [vmware面经] software engineer
他们家老印太多了,不推荐去吧
1. threadpool design: 给出一些任务, 并发去处理,设计到queue排队等待,同步在
哪里加锁,线程池预先分配,饥饿和队列溢出的处理
2. 压缩算法,给一个16进制字节流如何压缩
0x 12 32 FD FD 32 43 43 43
3. 实现semaphore
4. 2-phase commit 要几次通信,3-phase commit呢,分布式系统中给一个信息,如何
最快传输到每个结点,gossip?
5. 给一个任意的path “/etc/abc/def/a.txt”去验证是否存在,如果是目录的话,假
设可以用getFiles()获取所有的当前文件和子目录,然后给一个父目录,/etc/, 遍历
所有的子目录和文件,判断是否文件句柄有效,这道题也有些模糊了。
g*****g
发帖数: 34805
37
来自主题: JobHunting版 - 准备总结一下design pattern了
真是写java的,singleton这么常见的东西就不该自己实现。
@Singleton annotation是写进EJB spec,一堆IoC架构都支持的东西,干嘛要重建轮子。
至于你要两个threadpool,没有问题。在IoC架构里,singleton无非是一个可植入的全
局变量的概念罢了,起两个不同变量名即可。Singleton除了其他Singleton的植入,是
不建议有非final的instance variable的,这主要为了避免多线程问题。
我想说的就是设计模式,既然可以抽象总结,大多也就可以由架构支持,并非都需要手
写的。
n*****a
发帖数: 55
38
来自主题: JobHunting版 - Amazon onsite 求祝福
上周三onsite,原来说第二天给结果。后recrutor 通知我说有人pto,要到这周二才给
结果。也上来求祝福。
没有什么值钱的面经,有一个比较有趣的是design一个threadpool.其他的都是版上常
见的问题。
s*****r
发帖数: 43070
39
来自主题: JobHunting版 - 关于java的疑惑
core java里面有很多拗口难懂的玩意,很少有人知道怎么用,比如nio和security,俺
把他们叫做academic java。javax里面,稀奇古怪的东西更多。
凡是觉得Java比C++容易的,属于没太多知识,能说的出名来的Java core package不会
超过5个。
concurrent里面的threadpool应该还是常见的类,用处也不小。

JEE,
r******g
发帖数: 138
40
我面试写出来了。用java。只要求最简单的实现。
可以参考这个
http://stackoverflow.com/questions/13217618/simple-threadpool-i
w**5
发帖数: 34
41
来自主题: JobHunting版 - fb面经
就是写一个threadpool的void post(Runnable)。
你有好多个线程用你这个pool。
mypool.post(r1); mypool.post(r2); .....mypool.post(rn);
每个线程调用post这么不能block,要马上return.
你的thradpool要用Queue保存所有的runnable,然后单线程一个一个运行Queue里面的
task。
w**5
发帖数: 34
42
来自主题: JobHunting版 - fb面经
就是写一个threadpool的void post(Runnable)。
你有好多个线程用你这个pool。
mypool.post(r1); mypool.post(r2); .....mypool.post(rn);
每个线程调用post这么不能block,要马上return.
你的thradpool要用Queue保存所有的runnable,然后单线程一个一个运行Queue里面的
task。
g*****g
发帖数: 34805
43
来自主题: JobHunting版 - 这题怎么做?
You don't need to start any producer thread. threadpool is about consumer.
An enum is enough to mark the producer.

different
g*****g
发帖数: 34805
44
来自主题: JobHunting版 - 这题怎么做?
For simulation,you just need to demonstrate the ability submit each task
asynchronously. That's why each task is a callable future.
e.g.
Future f = threadpool.submit(new PrintTask(USB, 10));

different
b*****n
发帖数: 618
45
Dropbox那个我写的比较简单,就是分成parse document,download document两步,每
步用单独的threadpool,download完了就submit一个request另一个pool里面,你如果
想自己control request queue也可以。
两个documents相似这个我直接答的minhashing但是他们应该是用不同的方法。
b**********5
发帖数: 7881
46
我觉得你回答, 还要download, download完, 还要parse, 别搞不同threadpool。
。 用storm做算了。。。 storm全都帮你handle。。。
b*****n
发帖数: 618
47
来自主题: JobHunting版 - how to code this question of LinkedIn
我只是说了merge的那部分,produce那部分也比较好办把。
idea是一样的,可以用两个queue,
第一个queue里开始存的都是input,
produce完的output放到第二个queue里面。
每个thread worker的逻辑是一个while(true) loop
先看一下第一个queue里面还有没有input没处理,如果还有就先处理produce,
否则就是上面的处理merge的逻辑。
其实就是个size为k的threadpool。
我可能还是没明白你说的 “use a PriorityQueue to create two functions:
producer and consumer” 是什么意思?
g*****g
发帖数: 34805
48
来自主题: JobHunting版 - 讨论一下12306的架构?
刷网站就是挤厕所,没有什么不同。15分钟挤不进去有可能30分钟就挤进去了,就跟排
队15分钟没排到有可能30分钟能排到,没有本质区别。除了一个可以坐着等,一个要去
挤。有人第一分钟就挤进去了,就跟有人排队早,第一分钟就放进去了也一样。
诸位都是学计算机的,一个threadpool是带个queue还是大家都synchronize lock比运
气,本来就没有争的必要。

述。
b**********5
发帖数: 7881
49
来自主题: JobHunting版 - 求neflix mobile面经 大包子
第四面继续: 现在给你一个
class task implements runable{
void run();
List depedencies;
}
就是有一个task list, 每个task可能depends on some other tasks in the list,
让给个solution, so that tasks are run after their dependent tasks
我说, 不就是topology sort吗。。。 然后就哗啦哗啦用BFS写了一个function。 先
用java 8 filter一个sublist for those that dependencies size is 0. 然后
create一个 Executor。newFixedThreadPool, 把这些sublist给submit掉, 再把
sublist从原来的list里撤掉, 再iterate through the list, 把这个zero
dependecies的task从 每个task的dependency list... 阅读全帖
r*****s
发帖数: 1815
50
国内一般面试这种细节:
http://mm.fancymore.com/reading/java-threadpools.html
换这边多数人也不会。


: 面过几个国内码农,工作十多年了,特别能吹,分不清map和unordered_
map让他
写个快

: 速排序好几遍都写不对,我都醉了。

1 2 3 末页 (共3页)