g****n 发帖数: 18 | 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 | xt 发帖数: 17532 | 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);
| m******t 发帖数: 2416 | 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);
| c****e 发帖数: 90 | 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);
|
|