线程监听器通知

Mis*_*r S 2 java multithreading handle

我遇到了处理线程完成的类的问题.它可以通知其他线程,因为第二个线程可以启动.这是我的项目结构:

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

}   

}
Run Code Online (Sandbox Code Playgroud)

类 - 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

}
}
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

ThreadCompleteListener.java

public interface ThreadCompleteListener {
void notifyOfThreadComplete(final Thread thread);
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,当我执行MainClass时,我收到的错误是:发生致命异常.程序将退出并在控制台中显示:

java.lang.NoSuchMethodError:线程"main"中的主要异常

任何人都可以帮助在一个工作的平静中得到这个或告诉我在代码中做错了什么?

非常感谢任何建议!

Ade*_*ros 7

取代public void main

公共static无效的主要