简体   繁体   English

线程监听器通知

[英]Thread listener notification

I'm having a problem with class which handles thread complete. 我遇到了处理线程完成的类的问题。 It schould notify other threads that are fineshed so the second one can start. 它可以通知其他线程,因为第二个线程可以启动。 This is my project structure: 这是我的项目结构:

MainClass.java 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

}   

}

Class - Thread1.java 类 - 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 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 ThreadCompleteListener.java

public interface ThreadCompleteListener {
void notifyOfThreadComplete(final Thread thread);
}

Problem which I face is that when I execute MainClass I'm getting an error like: Fatal exception occured. 我遇到的问题是,当我执行MainClass时,我收到的错误是:发生致命异常。 Program will exit and in console appears: 程序将退出并在控制台中显示:

java.lang.NoSuchMethodError: main Exception in thread "main" java.lang.NoSuchMethodError:线程“main”中的主要异常

Could anyone help to get this in one working peace or tell what I'm doing wrong in the code? 任何人都可以帮助在一个工作的平静中得到这个或告诉我在代码中做错了什么?

Many thanks for any advice! 非常感谢任何建议!

replace public void main 取代public void main

with public static void main public static void main

I think you should replace: 我想你应该替换:

public void main(String[] args)

for 对于

public static void main(String[] args)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM