如何使这两个线程运行togeather?

时间:2015-05-07 10:39:54

标签: java multithreading

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();
    }
}

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

方法我跟随: -

TIMER

 @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);  
        }
    }

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

请建议。

4 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分: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);
    }

}

答案 2 :(得分:0)

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

主要

 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();
    }
}

<强> TIMER

 // 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);
    }

答案 3 :(得分:0)

注意:没有办法立即停止线程。你应该检查周期中的状态。如果是这样,为什么你决定有单独的线程来阻止finder线程?如果超过时间,您可以在取景器循环中定时并退出。像:

@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);  
    }
}
相关问题