Redian新闻
>
a Java MultiThreading question
avatar
a Java MultiThreading question# Java - 爪哇娇娃
r*o
1
Thanks ahead for any solution and suggestion from you, I am not familar to
Java multithreading.
A program need to have 3 different threads, each of them will live as long as
the program,
1. Main thread, start with main() function, its function is to start following
two threads, then waiting to receive command line input from user.
2. A coordinator thread, works as a coordinator, after main thread receives a
command from user, it will send the command as a request to other machine's
participant t
avatar
r*s
2
试试看在thread2代码的开始先让他调用sleep()或者yield()

as
following
a
killed
main

【在 r*******o 的大作中提到】
: Thanks ahead for any solution and suggestion from you, I am not familar to
: Java multithreading.
: A program need to have 3 different threads, each of them will live as long as
: the program,
: 1. Main thread, start with main() function, its function is to start following
: two threads, then waiting to receive command line input from user.
: 2. A coordinator thread, works as a coordinator, after main thread receives a
: command from user, it will send the command as a request to other machine's
: participant t

avatar
m*t
3

main
What do you do in the coordinator thread (thread 2?) once it's started? If it
runs in a polling loop, yeah it'd very likely block out other threads for a
while (it can't completely "take over the execution" though).

【在 r*******o 的大作中提到】
: Thanks ahead for any solution and suggestion from you, I am not familar to
: Java multithreading.
: A program need to have 3 different threads, each of them will live as long as
: the program,
: 1. Main thread, start with main() function, its function is to start following
: two threads, then waiting to receive command line input from user.
: 2. A coordinator thread, works as a coordinator, after main thread receives a
: command from user, it will send the command as a request to other machine's
: participant t

avatar
h*r
4
If this is the case, how to solve it?

line
it

【在 m******t 的大作中提到】
:
: main
: What do you do in the coordinator thread (thread 2?) once it's started? If it
: runs in a polling loop, yeah it'd very likely block out other threads for a
: while (it can't completely "take over the execution" though).

avatar
B*N
5
can not see the reason from pseudo code, better put the real code.
psedo code sounds reasonable. something is wrong in your thread2.

as
following
a
killed
main

【在 r*******o 的大作中提到】
: Thanks ahead for any solution and suggestion from you, I am not familar to
: Java multithreading.
: A program need to have 3 different threads, each of them will live as long as
: the program,
: 1. Main thread, start with main() function, its function is to start following
: two threads, then waiting to receive command line input from user.
: 2. A coordinator thread, works as a coordinator, after main thread receives a
: command from user, it will send the command as a request to other machine's
: participant t

avatar
m*t
6

Well the coordinator could wait on something and only get notified when there
is some message to send -- google the producer-consumer pattern.

【在 h****r 的大作中提到】
: If this is the case, how to solve it?
:
: line
: it

avatar
w*r
7
from your psudo code, we can find nothing wrong. The problem may reside in
your run method of thread2. Thread scheduler should get a chance to interrup
the thread2 to continue execute the rest part of your main method. If your
thread2 is too heavy duty, it will take long(infinite) time for thread
scheduler to come back to the main method. For example, if your thread2's run
method is
public void run(){
while(true){
do nothing
}
}
The virtue machine will not get a chance to come back to create t

【在 r*******o 的大作中提到】
: Thanks ahead for any solution and suggestion from you, I am not familar to
: Java multithreading.
: A program need to have 3 different threads, each of them will live as long as
: the program,
: 1. Main thread, start with main() function, its function is to start following
: two threads, then waiting to receive command line input from user.
: 2. A coordinator thread, works as a coordinator, after main thread receives a
: command from user, it will send the command as a request to other machine's
: participant t

avatar
w*r
8
From your psudo code, there is nothing run. Are you sure the thread3 is never
got created? Or it is just blocked on execution? Well, it is too vague to
juedge from your psudocode, there is nothing wrong there.

run
do
receives
machine's
programming.
to
killed

【在 w*r 的大作中提到】
: from your psudo code, we can find nothing wrong. The problem may reside in
: your run method of thread2. Thread scheduler should get a chance to interrup
: the thread2 to continue execute the rest part of your main method. If your
: thread2 is too heavy duty, it will take long(infinite) time for thread
: scheduler to come back to the main method. For example, if your thread2's run
: method is
: public void run(){
: while(true){
: do nothing
: }

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