我在处理线程完成的类时遇到问题。它会通知其他被细化的线程,以便第二个线程可以启动。这是我的项目结构:
主类.java
public class MainClass implements ThreadCompleteListener {
public void main(String[] args) throws InterruptedException {
NotifyingThread test = new Thread1();
test.addListener((ThreadCompleteListener) this);
test.start();
}
@Override
public void notifyOfThreadComplete(Thread thread) {
// TODO Auto-generated method stub
}
}
类 - 线程 1.java
public class Thread1 extends NotifyingThread {
@Override
public void doRun() {
try {
metoda();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static synchronized void metoda() throws InterruptedException {
for(int i = 0; i <= 3; i++) {
Thread.sleep(500);
System.out.println("method in Thread1");
}
}
public void notifyOfThreadComplete(Thread thread) {
// TODO Auto-generated method stub
}
}
通知线程.java
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
public abstract class NotifyingThread extends Thread {
private final Set<ThreadCompleteListener> listeners = new CopyOnWriteArraySet<ThreadCompleteListener>();
public final void addListener(final ThreadCompleteListener listener) {
listeners.add(listener);
}
public final void removeListener(final ThreadCompleteListener listener) {
listeners.remove(listener);
}
private final void notifyListeners() {
for (ThreadCompleteListener listener : listeners) {
listener.notifyOfThreadComplete(this);
}
}
@Override
public final void run() {
try {
doRun();
} finally {
notifyListeners();
}
}
public abstract void doRun();
}
线程完成侦听器.java
public interface ThreadCompleteListener {
void notifyOfThreadComplete(final Thread thread);
}
我面临的问题是,当我执行 MainClass 时,我收到如下错误:发生了致命异常。程序将退出,并在控制台中出现:
java.lang.NoSuchMethodError: main线程"main"中的异常
任何人都可以帮助在一个工作和平中做到这一点或告诉我在代码中做错了什么?
非常感谢任何建议!
替换公共无效主
带公共static
空主
我认为你应该替换:
public void main(String[] args)
为
public static void main(String[] args)