我遇到了处理线程完成的类问题。它可以通知其他线程,因为第二个线程可以启动。这是我的项目结构:
MainClass.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
}
}
类 - Thread1.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
}
}
NotifyingThread.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();
}
ThreadCompleteListener.java
public interface ThreadCompleteListener {
void notifyOfThreadComplete(final Thread thread);
}
我面临的问题是,当我执行MainClass时,我得到的错误如下:发生致命异常。程序将退出并在控制台中显示:
java.lang.NoSuchMethodError:main 线程&#34; main&#34;中的例外情况
任何人都可以帮助实现这一目标,或者说出我在代码中做错了什么吗?
非常感谢任何建议!
答案 0 :(得分:7)
替换 public void main
public static
void main
答案 1 :(得分:2)
我认为你应该替换:
public void main(String[] args)
的
public static void main(String[] args)