【问题标题】:How do I make these two threads run togeather?如何让这两个线程一起运行?
【发布时间】:2015-05-07 21:04:56
【问题描述】:
public class FileFinder {
    private static Timer timerThread;
    private static Find findThread;

    public static void main(String[] args) {
        timerThread = new Timer(Integer.parseInt(args[3]));
        findThread = new Find(args[0], args[1], args[2]);
        Thread timer = new Thread(timerThread);
        Thread finder = new Thread(findThread);

        finder.start();
        timer.start();
    }
}

注意:finder 会找到用户输入的文件 计时器应该与查找线程并行运行,如果指定的超时时间超过两个线程必须退出。

我正在遵循的方法:-

定时器

 @Override
    public void run() {
    newTime = startTime;
    System.out.println("Timer Running\n");
    System.out.println("TimeOut is:" + timeOut +"\n");
    while(true){
        if(newTime < startTime+timeOut){
            newTime=startTime+ System.currentTimeMillis();  
        }
        else{
            break;
        }
    }

    System.err.println("Cannot find");
    System.exit(-1);
}

查找

@Override
    public void run() {
        System.out.println("Running find");
        File searchDir = new File(this.searchLocation);
        UserFilter filter = new UserFilter(searchKeyword,searchType,pathOfSearchedFiles);

        /*for(String file : searchDir.list(filter)){
            pathOfSearchedFiles.add(searchDir+File.separator+file);
        }*/

        searchDir.list(filter);

        for(String f: pathOfSearchedFiles){
            System.out.println(f);  
        }
    }

我的查找线程没有时间工作,计时器线程正在退出应用程序。

请提出建议。

【问题讨论】:

  • 在java中存在一个同步关键字。欲了解更多信息tutorialspoint.com/java/java_thread_synchronization.htm
  • 您需要某种基于计时器的同步吗?你的问题现在还不清楚。
  • @Sasha,我想同时运行我的两个线程,计时器线程负责管理查找器线程应该继续搜索文件的时间。
  • @Ankit 我想这是一个糟糕的设计。计时器线程如何在截止日期前终止查找线程?
  • @ankit 一次只能运行一个线程

标签: java multithreading


【解决方案1】:

我认为你的 while 循环有问题:

while(startTime < startTime+timeOut){
                startTime=startTime+ System.currentTimeMillis();
            }

这是一个无限循环......

【讨论】:

    【解决方案2】:

    您的计时器功能没有达到您的预期。它将当前时间添加到开始时间,然后请求它大于自身(除非超时为负)。您有可能会溢出,这就是它提前退出的原因

    尝试类似的方法:

    @Override
    public void run() {
            System.out.println("Timer Running");
            long endTime = System.currentTimeMillis() + timeOut;
    
            while(System.currentTimeMillis() < endTime){
                try{
                    Thread.sleep(500);  //Set the granularity here to a suitable value
                catch(InterruptedException e){
                }
            }
    
            System.err.println("Cannot find");
            System.exit(-1);
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以将 Find 线程作为参数添加到计时器以进行管理:

      主要

       public class FileFinder {
          private static Timer timerThread;
          private static Find findThread;
      
          public static void main(String[] args) {
      
              findThread = new Find(args[0], args[1], args[2]);
              Thread finder = new Thread(findThread);
              timerThread = new Timer(Integer.parseInt(args[3]),finder); // <-- here add parameter
              finder.start();
              timer.start();
          }
      }
      

      定时器

       // You have finder as an attribute Thread finder;
          @Override
              public void run() {
              newTime = startTime;
              System.out.println("Timer Running\n");
              System.out.println("TimeOut is:" + timeOut +"\n");
              while(true){
                  if(newTime < startTime+timeOut){
                      newTime=startTime+ System.currentTimeMillis();  
                  }
                  else{
                      break;
                  }
              }
              if(finder != null)
                  finder.interrupt(); // <--- here you interrupt finder thread
              System.err.println("Cannot find");
              System.exit(-1);
          }
      

      【讨论】:

        【解决方案4】:

        注意:没有办法立即停止线程。您应该以任何方式检查周期中的状态。如果是这样,您为什么决定使用单独的线程来停止查找线程?如果超过时间,您可以在查找器循环中计时并退出。喜欢:

        @Override
        public void run() {
            System.out.println("Running find");
            File searchDir = new File(this.searchLocation);
            UserFilter filter = new UserFilter(searchKeyword,searchType,pathOfSearchedFiles);
        
            /*for(String file : searchDir.list(filter)){
                pathOfSearchedFiles.add(searchDir+File.separator+file);
            }*/
        
            searchDir.list(filter);
        
            for(String f: pathOfSearchedFiles){
                if (System.currentTimeMillis() >= stopTime) {
                    break;
                }
                System.out.println(f);  
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-23
          • 1970-01-01
          • 2021-08-29
          • 1970-01-01
          • 2012-07-16
          • 2018-06-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多