My question is why or how i can make “listenerList” value as 1 or 2, so that i can track and get status of that instance from alert class with the help of vector()? ex: listenerList is my process id to track them, but its always value of 0
Here is whole code, not working to see the output: “it WORKS!!!, if i can reach here!!!”:
[updated after suggestion, but still same, not working]
1) Interface
public interface Listener
{
public void registered();
public void connected(String IP,int Port);
public void disconnected();
}
2) Interface extending
public class alert implements Listener
{
public void registered()
{System.out.println("it WORKS!!!, if i can reach here!!!"); System.exit(0);}
public void connected(String IP, int Port)
{ throw new UnsupportedOperationException("Not supported yet.");}
public void disconnected()
{ throw new UnsupportedOperationException("Not supported yet."); }
}
3) Main class to use
public class Provider extends Thread
{
public Vector listenerList;
public alert alert;
public Provider(String userName) throws Exception{ listenerList = new Vector();
addListener(alert);
}
public void addListener(Listener ls) { listenerList.addElement(ls);}
public void removeListener(Listener ls) { listenerList.removeElement(ls); }
public void run()
{
while(running)
{
fireRegisteredEvent(); // shows now value of 1, but does not do exit(0);
}
}
private void fireRegisteredEvent()
{
System.out.println("Fireing Registration: " + System.currentTimeMillis());
System.out.println("Fireing Registration: " + listenerList.size());
for (int i = 0; i < listenerList.size(); i++)
{
alert.registered();
System.exit(0);
}
}
}
4) Action
private Provider multiinstance;
multiinstance = new Provider("Why are you not triggering: System.exit(0);??????");
multiinstance.start();
5) Output
Fireing Registration: 1303646370073
Fireing Registration: 1
….
6) Expecting result:
a) output: System.out.println("it WORKS!!!, if i can reach here!!!");
OR
b) listenerList.size() value is either 1 or 2?!!
OR
c) alert class and interface should react? no?? if not how i can ??
Because you never instantiated a listener and assigned it to one of the providers. In other words you never call
addListener(...)and thus no listener can be triggered.