Java 中多线程环境的自定义事件侦听器使用 Wait() 和 notify()
Custom Event Listener for Multi threaded environment in Java Using Wait() and notify()
我有一个多线程环境,我想使用事件侦听器处理所有线程。
我已经创建了监听事件的监听器,我的问题是我想等待并使用一些特定的唯一 ID 或我们可以用于唯一目的的任何东西通知每个线程。
我是 java 中事件侦听器编程的新手。
我正在使用 Java 制作 IVR 应用程序,每次调用都会创建我的新线程。
任何演示对我来说都非常有用。
这不会直接适合您的代码,您必须工作才能适合您。
这是我的想法。
public class MyThread extends Thread{
private static int uniqueId;
private int myId;
MyThread(){
myId = ++uniqueId;
}
//getters and setters to myId
}
public class 你的班级{
public HashMap threads = new HashMap();// 使用它通过 ids 映射你的线程
//when you want to access a thread, you get it from the map and call wait and notify.
//this map should be publicly visible to you.
}
我有一个多线程环境,我想使用事件侦听器处理所有线程。
我已经创建了监听事件的监听器,我的问题是我想等待并使用一些特定的唯一 ID 或我们可以用于唯一目的的任何东西通知每个线程。
我是 java 中事件侦听器编程的新手。
我正在使用 Java 制作 IVR 应用程序,每次调用都会创建我的新线程。
任何演示对我来说都非常有用。
这不会直接适合您的代码,您必须工作才能适合您。
这是我的想法。
public class MyThread extends Thread{
private static int uniqueId;
private int myId;
MyThread(){
myId = ++uniqueId;
}
//getters and setters to myId
}
public class 你的班级{
public HashMap threads = new HashMap();// 使用它通过 ids 映射你的线程
//when you want to access a thread, you get it from the map and call wait and notify.
//this map should be publicly visible to you.
}