Redian新闻
>
Follow up Re: Simp question on
avatar
Follow up Re: Simp question on# Java - 爪哇娇娃
g*n
1
At first, thanks to everyone who helped me out on the previous question.
Now I encounter a new problem. The code is listed below for the convenience
while (listening)
{
System.out.println("Waiting for a client...");
MultiThread mthread = new MultiThread(group, serverSocket.accept(), h);
mthread.start;
mthread.join();
trackTime = h.getTime();
System.out.println("Track time " + trackTime);
System.out.println("Load: " + group.activeCount
avatar
xt
2

It looks this piece of code really defeats the purpose of multithread
programming.
mthread can't be run for twice. Most probably it is this piece of code that is
run twice (or going for 2 loop runs) for some reason.

【在 g****n 的大作中提到】
: At first, thanks to everyone who helped me out on the previous question.
: Now I encounter a new problem. The code is listed below for the convenience
: while (listening)
: {
: System.out.println("Waiting for a client...");
: MultiThread mthread = new MultiThread(group, serverSocket.accept(), h);
: mthread.start;
: mthread.join();
: trackTime = h.getTime();
: System.out.println("Track time " + trackTime);

avatar
m*t
3

Why don't you make MultiThread keep track of how much time it took itself,
instead of having the server thread do it - it's not really the server
thread's job.

【在 g****n 的大作中提到】
: At first, thanks to everyone who helped me out on the previous question.
: Now I encounter a new problem. The code is listed below for the convenience
: while (listening)
: {
: System.out.println("Waiting for a client...");
: MultiThread mthread = new MultiThread(group, serverSocket.accept(), h);
: mthread.start;
: mthread.join();
: trackTime = h.getTime();
: System.out.println("Track time " + trackTime);

avatar
c*e
4
真是不明白你的代码,不明白你要干吗?
你既然同时要处理多个client,
头一个client连接,你启动一个线程MultiThread去处理他
然后调用join就等那个线程结束
那个线程结束后你计时 (这是什么时间?处理完头一个client后当前系统时间?)
假如在头一个client没有处理完的时候,第二个client来了
这是主线程在等待中(join) 不能处理
直到头一个client完成,MultiThread结束,join 才能返回
如果这时候第二个client还在连接当中,才会被下一个MutiThread处理。
用了join, 让你的多线程一点意义都没有,根本没有并行度,只能一个一个
client的顺序处理。
不用join ,那个计时计下的是什么时间? 在client很少的情况下
accept会block, 记下的是client到来的时间。
在client很密集的时候记下的是java 打印三行message,new并启动一个
进程, 取系统时钟再加loop overhead的时间。
你为什么不在MultiThread.run() 开始和返回之前计时?相减不就是
处理一个clie

【在 g****n 的大作中提到】
: At first, thanks to everyone who helped me out on the previous question.
: Now I encounter a new problem. The code is listed below for the convenience
: while (listening)
: {
: System.out.println("Waiting for a client...");
: MultiThread mthread = new MultiThread(group, serverSocket.accept(), h);
: mthread.start;
: mthread.join();
: trackTime = h.getTime();
: System.out.println("Track time " + trackTime);

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