b******d 发帖数: 794 | 1 请问怎么多线程调用webclient
我这么写了个,结果一new webclient就退出,连exception都没有,死得干干净净。
public class XInventory implements Runnable {
@Test
public void testMultiThSearch(){
XInventory bbInv = new XInventory();
for(int i=0; i<2; i++){
new Thread(bbInv).start();
}
}
public void run(){
checkWSThread1();
}
private void checkWSThread1(){
System.out.println("processing");
WebClient webClient = new WebClient();
System.out.println("processing finished");
webClient.closeAllWindows();
}
}
输出结果是
processing
processing |
r*****l 发帖数: 2859 | 2 Your main thread died before the two spawned threads had chance to finish.
1, Simple but not good solution: add a delay in the main thread (the test
method).
2, Better solution: add some logic to tracking spawned threads and then
finish the test. For example, each thread increments a global counter in its
finally block and test method checks the counter.
【在 b******d 的大作中提到】 : 请问怎么多线程调用webclient : 我这么写了个,结果一new webclient就退出,连exception都没有,死得干干净净。 : public class XInventory implements Runnable { : @Test : public void testMultiThSearch(){ : XInventory bbInv = new XInventory(); : : for(int i=0; i<2; i++){ : new Thread(bbInv).start(); : }
|
b******d 发帖数: 794 | 3 谢谢真牛牛大虾,搞定了。
its
【在 r*****l 的大作中提到】 : Your main thread died before the two spawned threads had chance to finish. : 1, Simple but not good solution: add a delay in the main thread (the test : method). : 2, Better solution: add some logic to tracking spawned threads and then : finish the test. For example, each thread increments a global counter in its : finally block and test method checks the counter.
|
M***r 发帖数: 79 | 4 realBull already told you the issue. But the solution is right under you
feet. The CountDownLatch is the one specifically designed to solve this kind
of issue in Java. Here is an example: http://www.java2s.com/Code/Java/Threads/AnexampleofCountDownLatch.htm |
b******d 发帖数: 794 | 5 谢谢阿,今天真是大牛云集阿
kind
【在 M***r 的大作中提到】 : realBull already told you the issue. But the solution is right under you : feet. The CountDownLatch is the one specifically designed to solve this kind : of issue in Java. Here is an example: http://www.java2s.com/Code/Java/Threads/AnexampleofCountDownLatch.htm
|
w**z 发帖数: 8232 | 6 use threadpoolexecutor, returning future .
【在 b******d 的大作中提到】 : 请问怎么多线程调用webclient : 我这么写了个,结果一new webclient就退出,连exception都没有,死得干干净净。 : public class XInventory implements Runnable { : @Test : public void testMultiThSearch(){ : XInventory bbInv = new XInventory(); : : for(int i=0; i<2; i++){ : new Thread(bbInv).start(); : }
|
e*****t 发帖数: 1005 | 7 realBull says it.
btw, junit or testng can run tests in parallel. So there's no point to spawn
your own thread. You don't need to implements Runnable, just have two metho
ds which call checkWSThread1() (you may want to rename it) and annotate them
with @Test.
its
【在 r*****l 的大作中提到】 : Your main thread died before the two spawned threads had chance to finish. : 1, Simple but not good solution: add a delay in the main thread (the test : method). : 2, Better solution: add some logic to tracking spawned threads and then : finish the test. For example, each thread increments a global counter in its : finally block and test method checks the counter.
|
b******d 发帖数: 794 | 8 thx for the points, but i need real multi-thread. 10+ threads with different
parameters that are easy to generate within one method. I will also need to
call these multi-thread methods from outside to increase the productivity n
ot just to speed up the test, so i can't use the parallel testing.
anyway, that's a nice feature to have, and will definitely use it to test
thread safety.
thanks,
spawn
metho
them
【在 e*****t 的大作中提到】 : realBull says it. : btw, junit or testng can run tests in parallel. So there's no point to spawn : your own thread. You don't need to implements Runnable, just have two metho : ds which call checkWSThread1() (you may want to rename it) and annotate them : with @Test. : : its
|
g*****g 发帖数: 34805 | 9 Use a CountDownLatch, that's designed for this scenario.
its
【在 r*****l 的大作中提到】 : Your main thread died before the two spawned threads had chance to finish. : 1, Simple but not good solution: add a delay in the main thread (the test : method). : 2, Better solution: add some logic to tracking spawned threads and then : finish the test. For example, each thread increments a global counter in its : finally block and test method checks the counter.
|