/** This is run before each @TestD method. */ @Before public void before() { if (!run) { AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory(); bot = (MyBot) beanFactory.autowire(MyBot.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); Object botBean = beanFactory.initializeBean(bot, "mybot"); bot = (MyBot) botBean; // the bot also needs a trigger so its act() method is called regularly. // (there is no trigger bean in the context) PeriodicTrigger trigger = new PeriodicTrigger(ACT_LOOP_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); trigger.setInitialDelay(ACT_LOOP_INITIAL_DELAY_MILLIS); bot.setTrigger(trigger); logger.info("starting test case testGroupBot"); // adding the bot to the bot manager will cause it to be initialized. // at that point, the trigger starts. botManager.addBot(bot); // the bot should now be running. We have to wait for it to finish before we // can check the results: // Together with the barrier.await() in the bot's listener, this trips the barrier // and both threads continue. try { bot.getBarrier().await(); } catch (InterruptedException e) { e.printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } catch (BrokenBarrierException e) { e.printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } run = true; } }
@Override public void run() { int counter; System.out.printf( "%s: Processing lines from %d to %d\n", Thread.currentThread().getName(), firstRow, lastRow); for (int i = firstRow; i < lastRow; i++) { int row[] = mock.getRow(i); counter = 0; for (int j = 0; j < row.length; j++) { if (row[j] == number) { counter++; } } results.setData(i, counter); try { Thread.sleep(10); } catch (InterruptedException e) { // TODO: handle exception } } System.out.printf("%s: Lines processed \n", Thread.currentThread().getName()); try { barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }
public void run() { try { Thread.sleep(1000); System.out.println(Thread.currentThread().getName() + " waiting at barrier 1"); /* * 阻塞等待条件1达成 */ this.barrier1.await(); Thread.sleep(1000); System.out.println(Thread.currentThread().getName() + " waiting at barrier 2"); /* * 阻塞等待条件2达成 */ this.barrier2.await(); System.out.println(Thread.currentThread().getName() + " waiting at barrier 3"); this.barrier3.await(); Thread.sleep(1000); System.out.println(Thread.currentThread().getName() + " done!"); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }
private static boolean waitBarrier(CyclicBarrier barrier) { while (true) try { barrier.await(); return true; } catch (InterruptedException ex) { continue; } catch (BrokenBarrierException ex) { ex.printStackTrace(); return false; } }
public void run() { System.out.println("[并发任务" + name + "] 开始执行"); for (int i = 0; i < 999999; i++) ; // 模拟耗时的任务 System.out.println("[并发任务" + name + "] 开始执行完毕,通知障碍器"); try { // 每执行完一项任务就通知障碍器 cb.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }
public void run() { try { System.out.println(Thread.currentThread().getName() + " wait for CyclicBarrier."); // 将cb的参与者数量加1 cb.await(); // cb的参与者数量等于5时,才继续往后执行 System.out.println(Thread.currentThread().getName() + " continued."); } catch (BrokenBarrierException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
private int await() { if (skipBarrier) { log.warn("Skipping barriers....."); return configuration.getNodesNum(); } int parties = -1; try { parties = barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } return parties; }
@Override public StartContainersResponse startContainers(StartContainersRequest request) throws IOException { try { startLaunchBarrier.await(); completeLaunchBarrier.await(); // To ensure the kill is started before the launch Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } throw new IOException(new ContainerException("Force fail CM")); }
@Override public void run() { try { barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); return; } catch (BrokenBarrierException e) { e.printStackTrace(); return; } for (int i = 1; i <= readerIterations; i++) { iterate(); } }
@Override public void run() { System.out.println(Thread.currentThread().getName() + "开始计算..."); try { Thread.sleep(2000); numbers[index] = index; System.out.println(Thread.currentThread().getName() + "完成计算..."); cyclicBarrier.await(2, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } System.out.println("所有线程都已完成计算,开始汇总..."); }
public void run() { if (!isEventHandled()) { synchronized (this) { try { wait(timeout_); } catch (InterruptedException e) { // ignored } } } if (barrier_ != null) { try { barrier_.await(); } catch (InterruptedException ie) { // ignored } catch (BrokenBarrierException e) { e.printStackTrace(); } } }
@Override public void run() { try { TimeUnit.SECONDS.sleep(new Random().nextInt(10)); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " is ready to action"); try { barrier.await(); System.out.println(Thread.currentThread().getName() + " start ..."); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (BrokenBarrierException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } }