Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: extending Observable and implementing runnable possible ??

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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
    import java.io.* ;
    import java.net.* ;
    import java.util.* ;
     
    class Client implements Runnable{
    	Socket s = null ;
    	ObjectInputStream in = null ;
    	ObjectOutputStream out = null ;
     
    	public Client(Socket s){
    		try{
    			this.s = s ;
    			in = new ObjectInputStream(s.getInputStream()) ;
    			out = new ObjectOutputStream(s.getOutputStream()) ;
    		} catch (Exception e){
    			System.out.println(e) ;
    		}
     
    	}
    	public void run(){
    		System.out.println(s) ;
    		try{
    			Random r = new Random() ;
    			for (int i = 0 ; i < 1000000 ; i++){
    				System.out.println(i) ;
    				//int rd = r.nextInt() % 200 ;
    				if (i % 1000 == 0){
    					out.writeObject(new String("muchacha " + i)) ;
    					out.flush() ;
    				}else{
    					out.writeObject(new Integer(i)) ;
    					out.flush() ;
    				}
    			}
    			out.close() ;
    			in.close() ;
    			s.close() ;
    		} catch (Exception e){
    			System.out.println(e) ;
    		}
     
    	}
    }
     
    class Server{
    	public static void main(String [] args){
    		ServerSocket ss = null ; 
    		Socket conn = null ; 
    		try{
    			ss = new ServerSocket(4444) ;
    			while (true){
    				conn = ss.accept() ;
    				System.out.println("Connection from " + conn) ;
    				Thread t = new Thread(new Client(conn)) ;
    				t.start() ;
    			}
    		} catch (Exception e){}
    	}
    }

    Client side code
    import java.util.* ;
    import java.net.* ;
    import java.io.* ;
     
    class Watcher implements Observer{
    	public void update(Observable obj, Object arg){
    		System.out.println("observer update() arg : " + arg) ;
    	}
    }
     
    class BeingWatched extends Observable implements Runnable{
    	Socket conn = null ;
    	ObjectInputStream in = null ;
    	ObjectOutputStream out = null ;
     
    	public BeingWatched(Socket conn, Observer w){
    		try{
    			this.addObserver(w) ;
    			this.conn = conn ;
     
     
    		} catch (Exception e){}
     
    	}
    	public void run(){
    		System.out.println("came here") ;
     
    		try{
    		in = new ObjectInputStream(conn.getInputStream()) ;
    		out = new ObjectOutputStream(conn.getOutputStream()) ;
    		while(true){
    			Object o = (Object)in.readObject() ;
    			if (o instanceof String){
    				setChanged() ;
    				notifyObservers(o) ;
    			}
    		}}catch(Exception e){ System.out.println(e) ;}
    	}
    }
     
    class Obs{
    	public static void main(String [] args){
    		try{
    			Watcher w1 = new Watcher() ;
    			Socket s = new Socket("127.0.0.1", 4444) ;
    			BeingWatched bw = new BeingWatched(s, w1) ;
    			Thread t = new Thread (bw) ;
    			t.start() ;
    		} catch(Exception e){}
     
    	}
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

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

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: extending Observable and implementing runnable possible ??

    Quote Originally Posted by javapenguin View Post
    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?

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: extending Observable and implementing runnable possible ??

    Quote Originally Posted by copeg View Post
    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 ? )

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default 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:
    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).

  6. #6
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: extending Observable and implementing runnable possible ??

    Quote Originally Posted by copeg View Post
    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 . 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 View Post
    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 ??

  7. #7
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

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

  8. The Following User Says Thank You to copeg For This Useful Post:

    dumb_terminal (November 16th, 2010)

  9. #8
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: extending Observable and implementing runnable possible ??

    Quote Originally Posted by copeg View Post
    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.

Similar Threads

  1. class extending
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2010, 12:49 AM
  2. Implementing RSS Feeds
    By madhu_sushmi in forum Java Networking
    Replies: 0
    Last Post: April 1st, 2010, 07:57 PM
  3. Implementing Semaphores
    By bananasplitkids in forum Threads
    Replies: 1
    Last Post: March 27th, 2010, 04:35 AM
  4. Image location on a Runnable JAR file?
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 06:59 AM
  5. adding get mothods to a class extending thread
    By aliaa2a in forum Object Oriented Programming
    Replies: 6
    Last Post: August 3rd, 2009, 06:41 AM