|
j*******e 发帖数: 22 | 2 for a long time, i am wondering how to implement thread pool
as in Apache. in fact, this is a very important technology in
large-scale web server and applications.
i got a sample code for it, pls refer thgis link:
http://www.java-pro.com/code/2000/jp0010.zip
(it is inconvenient for me to post here.) |
|
g****n 发帖数: 18 | 3 I wrote a server. It has a threadGroup (threadPool) keeping track of number of
threads connecting to port 5012, using group.activeCount().
The server also has another port 6012 open and uses it to communicate with a
dispatcher. This is a single thread setup. Is there a way to pass the value
group.activeCount to the thread on port 1235? I tried, but the value is not
updated everytime a client is connected to the server. I am attaching the code
as follows. Can anyone point out where something is w |
|
l****u 发帖数: 2166 | 4 using thread pool, better call its threadFactory to create
any new threads bah |
|
x******o 发帖数: 4 | 5 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 | 6 yun
threat implementing runnable already... |
|
|
m******t 发帖数: 2416 | 8 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. |
|
g*****g 发帖数: 34805 | 9 我有一个程序,主线程需要等几个线程结束后做一些动作。
通常用join就可以解决。现在的问题是这个其实是一个三级的层次。
主线程产生一些子线程,这些子线程又分别产生一些孙线程,等孙
线程结束后子线程需要做一些处理。所有的孙线程都扔进一个ThreadPoolExcutor
执行。我的理解孙线程何时开始是由ThreadPool决定的。这样在子
线程里用join等待似乎不成吧? |
|
g*****g 发帖数: 34805 | 10 前些日子我重写了一个threadpool的核心代码,原来
的代码lock得太厉害了,改完了出了一堆race condition,
原来都是挨个走,不出毛病,这下顺畅了毛病就都出来了。 |
|
q*********u 发帖数: 280 | 11 好冷清阿,
我再来一个问题,有人说过new Thread();代价还是太大,
在j2se里面的,有Executors这么个东东,也请大牛能透露一下,用的都是那些类来new
线程或者说是threadpool的 |
|
g*****g 发帖数: 34805 | 12 For your case, this is fine. Normally you could have
potentially unlimited concurrent users, and you want to
use a threadpool, which could 1, limit the number of
threads, 2, reuse threads.
That's when the concurrency kicks in. |
|
g*****g 发帖数: 34805 | 13 既然是Cassandra,用JMX去看一眼TP统计。
通常用的线程多不是问题,如果threadpool queue不停增长
就是问题。 |
|
z***e 发帖数: 5393 | 14 也可能是我太白痴哈。
很简单一个东西,我用maven的junit跑几个unit test,就是比较一下linear遍历和
threadpool并发处理,看时间的差异,也就大概是这样的东西:
public void test() {
int N=10000;
List data = new LinkedList();
// 先initialize
data.add(new Long[N]);
data.add(new Long[N]);
data.add(new Long[N]);
data.add(new Long[N]);
for(Long[] array :data) {
... //往里面填点数字
}
// 1. linear process
for(Long[] array : data) {
for(int i=0; i
if (array[i]%2 == 0) {
// 做一些简单计算
}
}
}
// 2. parallel
ExecutorServ... 阅读全帖 |
|
h*********e 发帖数: 247 | 15 I found the reason
the pom.xml had :
org.eclipse.jetty
jetty-server
${jettyVersion}
org.mortbay.jetty
jetty-maven-plugin
${jettyVersion}
/guoguoHello
... 阅读全帖 |
|
g*****g 发帖数: 34805 | 16 threadpool will help only if there are more than one accesses to the
resource available. If the resource access is exclusive, it all depends
on how quick the processing is and how quick new thread being created.
Think of a printer with lots of jobs, yes, they need to wait. |
|
t*******e 发帖数: 684 | 17 你的问题是thread的生成不受你的控制,这个无解。否则自己定义了有上界的
threadpool就可以了。 |
|
g*****g 发帖数: 34805 | 18 你前面的说得很好,最后一句我不同意。
大部分应用不需要效率是真的,server端应用最重要的是
保证可读性,清晰的三层架构有利于日后的扩展。但
效率在大规模项目里是需要的,只不过你可以在功能基本实现
以后一步一步的解决瓶颈,也不是所有的程序员都会需要处理
这部分的问题。这里涉及到很多架构的东西,cache的使用,
sql的优化,nosql DB的使用,threadpool的调优,JVM的
调优等等。 |
|
x*******6 发帖数: 262 | 19 请问和threadpool之类的工具有什么区别?总得给个Callable或者Runnable来提交任务
吧 |
|
w**z 发帖数: 8232 | 20 我只用三句:
ExecutorService executor = Executors.newFixedThreadPool/
newCachedThreadPool
Future future = executor.submit(worker)
future.get()
Threadpool executor makes multithread programming in Java much much easier,
just make sure worker is thread safe. |
|
g*****g 发帖数: 34805 | 21 这不难呀,你需要的是实现一个自己的BlockingQueue,里面放三个
LinkedBlockingQueue,按三个优先级,把那些方法wrap一遍。接下来你需要的是一个
Threadpool。ThreadPoolExecutor拿过来直接用对你就够了。一个小时的活。
什么wait/notify统统用不到。没事把java.util.concurrent看一看,里面很多好东西。 |
|
m****r 发帖数: 6639 | 22 en. since she only has 3 different levels, this is how i would do it.
why do we need threadPool for this queue implementation?
西。 |
|
g*****g 发帖数: 34805 | 23 有几个annotation可以方便做scheduling,async, threadpool,没什么太神奇的,但
还是很方便。 |
|
f*******t 发帖数: 7549 | 24 constructor为什么slow?不能优化一下吗?
另外这个handleRequest()为什么slow?要找出原因再对症下药。如果它消耗很大CPU资
源,用threadpool执行不一定能提升效率;如果是synchronous地等待什么external
resource,多线程也许有帮助,不过也可能引入race condition和死锁等问题。 |
|
g*****g 发帖数: 34805 | 25 This is a classic producer/consumer problem then, you should be able to find
many examples online. You could create a fix thread threadpool, and the
same number of Predictor, you then pass predictor and your input to your
jobs before sending them to executor.
work
and
dequeues |
|
o**2 发帖数: 168 | 26 int cores = Runtime.getRuntime ().availableProcessors ();
补充一点,default的new Messenger()使用上面的core数量作为threadpool的容量。
如果不涉及IO,光是计算的话,这些threads足够用了,超过硬件的core数的话,就浪
费了。 |
|
g*****g 发帖数: 34805 | 27 你这有几个问题,第一可以试试一次10K记录。第二处理应该异步,应该并行。你说单
个操作要22秒是不合理的。你把记录包进一个task,扔进一个threadpool处理,立马就
能快很多倍。接下来要再优化可能要看你的数据结构了。 |
|
g*****g 发帖数: 34805 | 28 You don't need one thread for each queue, you need a threadpool.
is |
|
l******y 发帖数: 182 | 29 有这样一个问题:
// 一个数数类。其中,minus只有在count >= 10的时候才能被激活。
class Count {
public count = 0;
private lock = ReentrantLock();
private Condition condition = lock.newCondition();
public void add() {
lock.lock();
count += 1;
condition.signalAll();
lock.unlock();
}
public void minus() {
lock.lock();
while (count < 10)
condition.await();
count -= 10;
lock.unlock();
}
}
// 调用数数类
class Foo {
public static Co... 阅读全帖 |
|
l******y 发帖数: 182 | 30 其中理由还是不太理解。2是柱塞了main吗?是不是lock, unlock那些一定要放到
threadpool里才能正常工作?像这样在main里直接调用一定会被柱塞吗?谢谢! |
|
l******y 发帖数: 182 | 31 您的意思是不是这样:
在第2种情况,main运行到count.minus()被阻塞。所以剩下的for loop都运行不了了。
然后程序就永远阻塞在这里了。
如果是1,main始终可以运行。阻塞问题只是发生在threadpool里的线程。所以程序始
终可以把for loop运行完。线程里的阻塞最后也就会解决。
不知道我的理解对不对?谢谢! |
|
i***c 发帖数: 301 | 32 用threadpool(c#)之类的安全,自己造thread问题多 |
|
|
|
w***g 发帖数: 5958 | 35 我知道有个boost threadpool,在sourceforge上能找到。貌似还可以。
图解说,使用的语言C++语言最好,Java也可以。Linux平台。谢谢。 |
|
w***g 发帖数: 5958 | 36 ACE是用10年前的C++写的。那时候的C++在现在很多人看来就是个带类的C,而不是正经
的C++。用ACE的项目比较多,一般上了项目就没得选了。但对于新项目来说,boost和
poco显然是更好的选择。
poco确实追求大而全,而且不像boost那样可以裁减。但poco的实用性显然比boost强。
不谈各种网络协议的实现,光说threadpool,boost就没有(不算那个非官方实现)。
我感觉一个库进入boost的标准并不是这个库在实际上有多重要,而是这个库能体现某
种语言上的奇技淫巧。比如那个spirit,我感觉已经有点走火入魔了。程序写着确实爽
了,但编译极其缓慢,生成的代码极度膨胀--library超前编译器的发展太多了。 |
|
g*****g 发帖数: 34805 | 37 这个有啥复杂的?两个数据源表都是不变的,结果表的所有结果也是
互相独立的。对于每一项,起一个Job,扔进Threadpool算就是了,
看机器强弱设定一下线程数即可。
别说supercomputer,整一群台式机都搞定了。
5000
1
[i |
|
v****s 发帖数: 1112 | 38 是啊,如果用java,再用K个thread,怎么run 这些threads? 还有就是怎么判断某个
thread已经算好了,然后给安排下一个?请讲讲threadpool好么?谢谢虫哥! |
|
g*****g 发帖数: 34805 | 39 这个很简单,看看ThreadPoolExecutor的文档,初始化core size。
你每个Job是一个Runnable,往里扔就是了。至于ThreadPool是怎么
实现的,你不需要知道。
如果怕内存占用太多,在afterExecute()里加新Job即可。 |
|
|
g*****g 发帖数: 34805 | 41 That's not how you use it. If you want to run N threads,
submit N jobs into threadpool. And in afterExecute, submit
another one. |
|
w***g 发帖数: 5958 | 42 sourceforge上有一个野鸡threadpool,仿boost风格,挺好用的. |
|
g*****g 发帖数: 34805 | 43 While what you say is true with JDK, however, lots of frameworks already
remove
the boiler plate code. e.g.
@Async annotation in Spring is enough to make a method asynchronous and
hooked to a threadpool. I won't need 1) and 2) below, and if I don't have
the source code of worker1/2, I can always wrap worker1.doSomething in my
own method. So 3) and 4) are not applicable. 5) is more like actor model,
but that's not the requirement here.
classes; |
|
w**z 发帖数: 8232 | 44 add an XML to define the war location under Conf/Catalina/localhost
Tomcat will restart the webapp if you change the war file and set reload to
be true.
Make sure your webapp can be restarted without restarting Tomcat. It doesn't
work for us since we have our own threadpool and the webapp cannot be
shutdown properly without shutting down tomcat. |
|
o**2 发帖数: 168 | 45 如果一定要用上eventloop这个概念的话,应该是每一个active object有一个自己的
eventloop。
你提到的例5里面,只有一个messenger instance,就是在125行处的new GuiMessenger
()。然后这个instance被传到了FileAnalyzer里去。
GuiMessenger可以精确的使用特定的thread执行一个特定的active object。
Active object "file.frame"是在127行用guiMessenger.registerJavaFxReceiver (
this, "file.frame", "setStatus");来注册的,所以guiMessenger在内部会用JavaFX
的thread来执行"file.frame"。
而active object "file.analyzer"是在130行注册的普通active object,
guiMessenger将用ThreadPool里的thread来执行"file.analyzer"。
但对使用的user program来说,调用它们是没有区别的... 阅读全帖 |
|
g*****g 发帖数: 34805 | 46 configure a threadpool for your worker threads, keep others. |
|
s*****n 发帖数: 5488 | 47 The only issue I can see is unbounded threads and the cost of thread
creation/destory. So at the first step, you can use threadpool.
So change to ExecutorService with fixedthreadpool is enough unless you need
to fine control of the tasks. |
|
S**********n 发帖数: 264 | 48
Task model is one way for one to harness the power of system threadpool.
Period. |
|
g*****g 发帖数: 34805 | 49 如果两个动作无关,Java也很好并行。比如用Spring,你给
attack和hitByThunder两个方法都加上@Async就得。结果就是调用的时候会自动起线程
在threadpool里跑。 |
|
g*****g 发帖数: 34805 | 50 如果两个动作无关,Java也很好并行。比如用Spring,你给
attack和hitByThunder两个方法都加上@Async就得。结果就是调用的时候会自动起线程
在threadpool里跑。 |
|