【发布时间】: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