-2
您能否幫我理解爲什麼我的線程沒有出現在調試器中。 那麼,正如你可以從主要方法的註釋中看到的那樣,我可以運行Task實例的perform()方法。但是當我嘗試啓動一個線程時,它不會啓動。任何中斷點都不會顯示Thrd的run()方法被調用。爲什麼我不能啓動線程
public class ParallelThreads {
public static void main(String[] args) {
Task t = new Task("C:\\FilesToRead\\1.txt");
//System.out.println(t.perform()); // It works!
Thread thread = new Thread(t);
}
}
public class Thrd implements Runnable{
TaskWithResult task;
public Thrd(TaskWithResult task){
this.task = task;
}
@Override
public void run(){
task.perform();
}
}
public interface TaskWithResult {
long perform();
}
public class Task implements TaskWithResult, Runnable {
File fileToRead;
public Task(String file) {
fileToRead = new File(file);
}
long count = 0;
@Override
public void run(){
perform();
}
@Override
public long perform() {
...
return count;
}
}
啓動它你沒有打電話給'開始()'... –
RTFM:http://docs.oracle。 com/javase/6/docs/api/java/lang/Thread.html – Messa