d*r 发帖数: 238 | 1 我现在正在做一个程序,遇到个问题。
我的程序是这样的:
user输入一组数,经过计算后,存到一个文件里。
因为运算的那一步比较复杂,所以user在输入一组数之后,要等一会才能输入第二组。
但是user并不需要知道结果,所以我想用threading来解决。
main thread用来接受user的输入,另一个thread(saving thread)平时处于idel。main
thread收到user
的输入后,send给saving thread。Saving thread接到main thread的message之后,开始
计算结果。
这个过程有点像http communication。但是只发生在两个thread之间。
现在问题是:
我需要一个像message pool一类的东西来存main thread发过来的message。
如果用VC的话,可以直接调用windows message queue。
如果用java怎么办呢?又没有现成的class?是在不行就只能自己写了。 |
f********h 发帖数: 149 | 2 这不就是producer 和 consumer的问题吗?网上有很多这类的java code的。
【在 d*r 的大作中提到】 : 我现在正在做一个程序,遇到个问题。 : 我的程序是这样的: : user输入一组数,经过计算后,存到一个文件里。 : 因为运算的那一步比较复杂,所以user在输入一组数之后,要等一会才能输入第二组。 : 但是user并不需要知道结果,所以我想用threading来解决。 : main thread用来接受user的输入,另一个thread(saving thread)平时处于idel。main : thread收到user : 的输入后,send给saving thread。Saving thread接到main thread的message之后,开始 : 计算结果。 : 这个过程有点像http communication。但是只发生在两个thread之间。
|
st 发帖数: 1685 | 3 a first in first out queue can solve the problem...
actually IBM tuple space(free java stuff) can be used for this... I think.
or just something in memory... :D
【在 d*r 的大作中提到】 : 我现在正在做一个程序,遇到个问题。 : 我的程序是这样的: : user输入一组数,经过计算后,存到一个文件里。 : 因为运算的那一步比较复杂,所以user在输入一组数之后,要等一会才能输入第二组。 : 但是user并不需要知道结果,所以我想用threading来解决。 : main thread用来接受user的输入,另一个thread(saving thread)平时处于idel。main : thread收到user : 的输入后,send给saving thread。Saving thread接到main thread的message之后,开始 : 计算结果。 : 这个过程有点像http communication。但是只发生在两个thread之间。
|
g*****g 发帖数: 34805 | 4 To be more specific, the computing thread will process until no more data in
queue and then sleep itself. A notification will be fired when current entered
data is the only one in queue.
。
thread)平时处于idel。main
开始
【在 st 的大作中提到】 : a first in first out queue can solve the problem... : actually IBM tuple space(free java stuff) can be used for this... I think. : or just something in memory... :D
|
xt 发帖数: 17532 | 5
entered
嗯,这种活我干过很多.
【在 g*****g 的大作中提到】 : To be more specific, the computing thread will process until no more data in : queue and then sleep itself. A notification will be fired when current entered : data is the only one in queue. : : 。 : thread)平时处于idel。main : 开始
|
o*******m 发帖数: 8 | 6 You can use the concept of conneciton pool.Of course , it is thread pool here.
1)Init
create a pool of thread for computing
create a pool of thread for saving results
Logic of you program
1)Get Data from user
2)get a thread from computing pool
3)compute
4)return computing thread and get thread from saving pool
5)save and return saving thread
As simple as this, you do not need sync between threads.
始
【在 d*r 的大作中提到】 : 我现在正在做一个程序,遇到个问题。 : 我的程序是这样的: : user输入一组数,经过计算后,存到一个文件里。 : 因为运算的那一步比较复杂,所以user在输入一组数之后,要等一会才能输入第二组。 : 但是user并不需要知道结果,所以我想用threading来解决。 : main thread用来接受user的输入,另一个thread(saving thread)平时处于idel。main : thread收到user : 的输入后,send给saving thread。Saving thread接到main thread的message之后,开始 : 计算结果。 : 这个过程有点像http communication。但是只发生在两个thread之间。
|
y***i 发帖数: 279 | 7 This is just way too much for a simple problem. Just launch a new thread
for computing as user finishes inputing, which dies after it gets and
saves the result. This is much similar to a swing GUI, and user
clicks go.
【在 o*******m 的大作中提到】 : You can use the concept of conneciton pool.Of course , it is thread pool here. : 1)Init : create a pool of thread for computing : create a pool of thread for saving results : Logic of you program : 1)Get Data from user : 2)get a thread from computing pool : 3)compute : 4)return computing thread and get thread from saving pool : 5)save and return saving thread
|