简体   繁体   English

使用Wait()和notify()在Java中为多线程环境定制事件监听器

[英]Custom Event Listener for Multi threaded environment in Java Using Wait() and notify()

I have a multithreaded environment and I want to handle all threads using the event listener. 我有一个多线程环境,我想使用事件监听器处理所有线程。

I have made the listener which will listen to the events, My question is that i want to wait and notify each thread using some specific unique id or whatever we can use for uniqueness purpose. 我已经创建了一个将监听事件的监听器,我的问题是我想等待并使用一些特定的唯一ID或我们可以用于唯一目的的任何方式通知每个线程。

I am new in event listener programming in java. 我是Java中事件监听器编程的新手。

I am making IVR application with Java and on each call my new thread will be created. 我正在用Java制作IVR应用程序,每次调用都会创建新线程。

Any demo will be highly useful for me. 任何演示对我都非常有用。

This wont directly fit to your code, you have to work to fit to you. 这不会直接适合您的代码,您必须努力使其适合您。

This is my idea. 这是我的主意。

public class MyThread extends Thread{
private static int uniqueId;
private int myId;
MyThread(){
    myId = ++uniqueId;
}
//getters and setters to myId

} }

public class YourClass{ 公共课程YourClass {

public HashMap threads = new HashMap();// use this to map your threads by ids 公共HashMap线程= new HashMap(); //使用它通过ID映射您的线程

//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.

} }

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

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