extending Observable and implementing runnable possible ??
Hello all, I was wondering whether it is possible for a class implementing runnable, to notify another class through Observable ?? I mean i have tried it and end up with two non responding program. can somebody point it out whether it is possible at al.l if possible what i am doing wrong here ? Regards.
Server side code
Client side code
Re: extending Observable and implementing runnable possible ??
You can implement them both like this
public class AClass implements Runnable, Observable
{
}
you could also have
public class AClass extends JFrame implements Runnable, Observable
you may even be able to do
public class AClass extends JFrame, Runnable, Observable
since only one of them is a class and the other two are interfaces.
Re: extending Observable and implementing runnable possible ??
Quote:
Originally Posted by
javapenguin
You can implement them both like this
public class AClass implements Runnable, Observable
{
}
....
since only one of them is a class and the other two are interfaces.
No you cannot...did you read the API for Observable? It is a class not an interface. Observer is an interface.
Quote:
Originally Posted by dumb_terminal
I mean i have tried it and end up with two non responding program.
Can you explain what you mean by non-responding? Does this mean the program exits and completes all its code, or deadlocks somewhere in between?
Re: extending Observable and implementing runnable possible ??
Quote:
Originally Posted by
copeg
Can you explain what you mean by non-responding? Does this mean the program exits and completes all its code, or deadlocks somewhere in between?
it hangs so that means its most probably a deadlock, could it be a Thread - Adress space issue since the observable is ruunning on a different thread and the observer on main thread ? but then again i passed a reference to the observer class. i don't know much about Java thread space (is the terminology Thread space correct ? :D)
Re: extending Observable and implementing runnable possible ??
The question now is where does it hang. Add tons of println's in there and see if it hangs on the connection side of the server or client, or within the thread itself. Add them in and see up to what point they stop. One issue that just caught my eye is the following piece of code:
Code java:
while (true){
conn = ss.accept() ;
System.out.println("Connection from " + conn) ;
Thread t = new Thread(new Client(conn)) ;
t.start() ;
}
Not sure this is the behavior you want, to continually try to accept the ServerSocket and create thread after thread if they are accepted (not actually sure how this will behave in this context - whether the accept will block).
Re: extending Observable and implementing runnable possible ??
Quote:
Originally Posted by
copeg
The question now is where does it hang.
It hung when either one of the side was trying to get the Streams. I commented out the input stream of server and output stream of the client then worked just fine. Yup the observer is getting update :D. But what is the issue here ?? i have tried to get both streams (I/O) on both sides on other occassions but with no problem.
Quote:
Originally Posted by
copeg
Not sure this is the behavior you want, to continually try to accept the ServerSocket and create thread after thread if they are accepted (not actually sure how this will behave in this context - whether the accept will block)
Well normally servers accepts each connection don't they ?? :confused:
Re: extending Observable and implementing runnable possible ??
Try reversing the InputOutput stream instantiations in one of the Client or Server. You may have a deadlock when it comes to input/output connections. If the server first tries to create an InputStream, it is waiting on the client to get an outputstream, but currently the client can't create the outputstream because it is waiting in the getInputStream for the server to create an outputstream.
Re: extending Observable and implementing runnable possible ??
Quote:
Originally Posted by
copeg
Try reversing the InputOutput stream instantiations in one of the Client or Server. You may have a deadlock when it comes to input/output connections. If the server first tries to create an InputStream, it is waiting on the client to get an outputstream, but currently the client can't create the outputstream because it is waiting in the getInputStream for the server to create an outputstream.
Worked like charm.... thanks. ^:)^