Java 线程侦听器通知
我对处理线程完成的类有问题。它将通知其他已精加工的线程,以便第二个线程可以启动。这是我的项目结构: MainClass.javaJava 线程侦听器通知,java,multithreading,handle,Java,Multithreading,Handle,我对处理线程完成的类有问题。它将通知其他已精加工的线程,以便第二个线程可以启动。这是我的项目结构: MainClass.java public class MainClass implements ThreadCompleteListener { public void main(String[] args) throws InterruptedException { NotifyingThread test = new Thread1(); test.addListener
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
}
}
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
}
}
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();
}
public interface ThreadCompleteListener {
void notifyOfThreadComplete(final Thread thread);
}
Class-Thread1.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
}
}
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
}
}
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();
}
public interface ThreadCompleteListener {
void notifyOfThreadComplete(final Thread thread);
}
我面临的问题是,当我执行MainClass时,我得到了一个错误,比如:发生了致命的异常。程序将退出并在控制台中显示:
java.lang.NoSuchMethodError:main
线程“main”中出现异常
有谁能帮我在一次工作中做到这一点,或者告诉我在代码中做错了什么
非常感谢你的建议 更换公共真空主管道
使用public
static
void main我认为您应该替换:
public void main(String[] args)
为了
当我用公共静态void main替换公共void main时,我不能使用test.addListener((ThreadCompleteListener)this);因为我知道它不能在静态上下文中使用,所以我发现。。。。你关于谷歌搜索框的建议非常有用。。。这是一个如此扩展的工具,不是吗@先生,您当然会出错!!您需要用
newmainclass()替换this
,
notifyOfThreadComplete为空。这不是错误的原因,但它不会像您预期的那样工作。