avatar
java thread question# Java - 爪哇娇娃
d*r
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?是在不行就只能自己写了。
avatar
f*h
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之间。

avatar
st
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之间。

avatar
g*g
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

avatar
xt
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
: 开始

avatar
o*m
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之间。

avatar
y*i
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

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