Java 多线程中面临的问题
我已经编写了一段代码,它将从主类启动固定数量的线程。下面的函数只是它的一部分。所有线程都将使用此方法。我已经给出了像USER1、USER2等线程名称 我的要求是,在这个方法中,在driver=WebDriver之后。。。。。。。语句我的所有线程都应该等待,直到它们都获得驱动程序。我知道我们可以加入。但在这里无法实现。有人能给我引路吗Java 多线程中面临的问题,java,multithreading,Java,Multithreading,我已经编写了一段代码,它将从主类启动固定数量的线程。下面的函数只是它的一部分。所有线程都将使用此方法。我已经给出了像USER1、USER2等线程名称 我的要求是,在这个方法中,在driver=WebDriver之后。。。。。。。语句我的所有线程都应该等待,直到它们都获得驱动程序。我知道我们可以加入。但在这里无法实现。有人能给我引路吗 private void testSuitLogin(String driverType){ try{ System.o
private void testSuitLogin(String driverType){
try{
System.out.println(Thread.currentThread().getName()+" Start Time "+System.currentTimeMillis());
driver = WebDriverFactory.getDriver(driverType);
System.out.println(Thread.currentThread().getName()+" End Time "+System.currentTimeMillis());
homePage();
googleSignIn();
driver.quit();
}
catch(Exception e){
if(driver==null)
{
totalNumberOfUsers--;
return ;
}
}
}
您可以使用
倒计时闩锁
。使用固定数量的线程
值创建一个倒计时闩锁
,并在获得WebDriver
实例后调用倒计时()
,然后调用等待()
等待所有线程到达
CountDownLatch countDownLatch = new CountDownLatch(fixedNumber);
private void testSuitLogin(String driverType){
try{
System.out.println(Thread.currentThread().getName()+" Start Time "+System.currentTimeMillis());
driver = WebDriverFactory.getDriver(driverType);
countDownLatch.countDown(); // decreases the value of latch by 1 in each call.
countDownLatch.await(); //It will wait until value of the latch reaches zero.
System.out.println(Thread.currentThread().getName()+" End Time "+System.currentTimeMillis());
homePage();
googleSignIn();
driver.quit();
}
catch(Exception e){
if(driver==null)
{
countDownLatch.countDown();
totalNumberOfUsers--;
return ;
}
}
}
您可以使用
倒计时闩锁
。使用固定数量的线程
值创建一个倒计时闩锁
,并在获得WebDriver
实例后调用倒计时()
,然后调用等待()
等待所有线程到达
CountDownLatch countDownLatch = new CountDownLatch(fixedNumber);
private void testSuitLogin(String driverType){
try{
System.out.println(Thread.currentThread().getName()+" Start Time "+System.currentTimeMillis());
driver = WebDriverFactory.getDriver(driverType);
countDownLatch.countDown(); // decreases the value of latch by 1 in each call.
countDownLatch.await(); //It will wait until value of the latch reaches zero.
System.out.println(Thread.currentThread().getName()+" End Time "+System.currentTimeMillis());
homePage();
googleSignIn();
driver.quit();
}
catch(Exception e){
if(driver==null)
{
countDownLatch.countDown();
totalNumberOfUsers--;
return ;
}
}
}
第一:如果所有人都等待所有人得到驱动程序,那么当一个人没有得到驱动程序时,你就有问题了 为了让所有人都互相等待(我不认为我真的这样做过,但这里有一个建议)。因为您知道线程的数量,所以可以制作如下内容:
- 线程获取驱动程序
- 线程调用一个同步方法(一次只能运行一个线程),该方法将计数器递减1(初始化为线程数)
- 线程产量
- 线程再次运行,调用一个方法检查计数器是否已达到0
- 答:计数器还没有为0,线程已生成
- 计数器为0,线程继续工作
- 线程获取驱动程序
- 线程调用一个同步方法(一次只能运行一个线程),该方法将计数器递减1(初始化为线程数)
- 线程产量
- 线程再次运行,调用一个方法检查计数器是否已达到0
- 答:计数器还没有为0,线程已生成
- 计数器为0,线程继续工作
catch
中的countDownLatch
块等待所有线程都获取驱动程序,如果在其中一个线程调用失败时继续,那么等待所有线程获取驱动程序将毫无意义。中止应用程序似乎更合理。但OP并没有给出应该做什么的指示,所以这就留给他们了。但是是的,catch是处理这种情况的好地方。@AadityaGavandalkar感谢您的建议和编辑。当驱动程序调用失败时,这将导致死锁。在出现特定异常的情况下,应减少catch
中的countDownLatch
块等待所有人获得驱动程序,如果你只是在一个失败的时候继续,那将是毫无意义的。中止应用程序似乎更合理。但OP并没有给出应该做什么的指示,所以这就留给他们了。但是,是的,捕获是处理这种情况的好地方。@Aadityagvandalkar感谢您的建议和编辑。