【问题标题】:JUnit run threads with static variable in JavaJUnit在Java中使用静态变量运行线程
【发布时间】:2016-02-27 18:19:44
【问题描述】:

我正在尝试在 JUnit 中运行两个线程。以下代码将被多个 JUnit 测试调用。

我想在结果不为空时停止两个线程。我该怎么办?问题是多个 JUnit 测试共享相同的 String 结果对象,并且此代码以某种方式被先前的测试阻塞。问题是当另一个测试调用此方法时,结果将分配为 null,并且先前的测试将阻塞在 while(true) 循环中。

static String result = null;

public static synchronized String remoteClogTailDir(final int maxRetry, String hostName,
        final String identifier, final String remoteClogDirPaths, final String whichKeyValue) {

    result = null;
    final String[] hosts = hostName.split(",");
    if(hosts != null && hosts.length == 2){
        Thread t1 = null;
        Thread t2 = null;
        t1 = new Thread(new Runnable(){
            @Override
            public void run(){
                String resultOfThread = null;
                resultOfThread = remoteClogTailDir(maxRetry, hosts[0].trim(), identifier, null,
                        remoteClogDirPaths, false, whichKeyValue);
                if(result == null && resultOfThread != null){
                    result = resultOfThread;
                }
            }
        });

        t2 = new Thread(new Runnable(){
            @Override
            public void run(){
                String resultOfThread = null;
                resultOfThread = remoteClogTailDir(maxRetry, hosts[1].trim(), identifier, null,
                        remoteClogDirPaths, false, whichKeyValue);
                if(result == null && resultOfThread != null){
                    result = resultOfThread;
                }
            }
        });

        t1.start();
        t2.start();

        while(true){
            if(result != null){
                t1.interrupt();
                t2.interrupt();
                return result;
            }
        }
    }else{
        return remoteClogTailDir(maxRetry, hostName, identifier, null,
                remoteClogDirPaths, false, whichKeyValue);
    }
}

【问题讨论】:

    标签: java multithreading


    【解决方案1】:

    如果我理解正确,您想并行执行多个搜索,并采取第一个搜索完成。您不应该为此使用静态属性。

    您可以将ExecutorCompletionService 用于此类任务:

    Executor executor = Executors.newCachedThreadPool();
    CompletionService<String> ecs = new ExecutorCompletionService<String>(executor);
    List<Future<String>> futures = new ArrayList<Future<String>>();
    try {
       futures.add(ecs.submit(search1));
       futures.add(ecs.submit(search2));
    
       for (int i = 0; i < futures.size(); ++i) {
         String result = ecs.take().get();
         if (result != null) {
           return result;
         }
       }
    } finally {
      for (Future<String> f : futures) {
         f.cancel(true);
      }
    }
    executor.shutdownNow();
    

    使用 search1 或 search2 一个简单的 Callable :

    Callable<String> search1 = new Callable<String() {
      public String call() {
        return remoteClogTailDir(...)
      }
    }
    

    【讨论】:

    • 哇,这个有效!谢谢!!!!!您对测试用例在两个远程服务器中搜索文件是正确的。如果在任一服务器中找到文件,则两个线程将停止并且该方法将返回文件内容。
    • @YiZhao 如果我能帮到你,别忘了接受这个答案;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 2013-08-31
    • 2014-01-09
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    相关资源
    最近更新 更多