我不是Java专家,但我继承了一些需要修补的代码。我将源代码放入netbeans中,但出现错误:匿名类实现接口;不能有参数。
这是代码:
Executor background = Executors.newSingleThreadExecutor(); Runnable mylookupThread = new Runnable(FilePath, SearchIndex) { public void run() { MainWindow.this.processFile(this.val$FilePath); Thread t = new Thread(new lookupThread(MainWindow.arrFile, true, false, this.val$SearchIndex)); t.setName("Lookup"); t.setPriority(10); t.start(); } }; background.execute(mylookupThread); Executor statusThread = Executors.newSingleThreadExecutor(); Runnable myStatusThread = new Runnable() { public void run() { MainWindow.this.updateStatus(); } }; statusThread.execute(myStatusThread);
错误在第二行弹出。救命?!?
创建mylookupThread单独的类,使其成为实例并将其传递给Executor:
mylookupThread
Executor
class LookupTask implements Runnable { private final String filePath, searchIndex; LookupTask(String filePath, String searchIndex) { this.filePath = filePath; this.searchIndex = searchIndex; } public void run() { ... } } ... background.execute(new LookupTask(filePath, searchIndex));
另一种方法是使filePath, searchIndex最终:
filePath, searchIndex
final String filePath = ... final String searchIndex = ... Executor background = Executors.newSingleThreadExecutor(); Runnable mylookupThread = new Runnable() { public void run() { MainWindow.this.processFile(filePath); Thread t = new Thread(new lookupThread(MainWindow.arrFile, true, false, searchIndex)); t.setName("Lookup"); t.setPriority(10); t.start(); } }; background.execute(mylookupThread);