Closed Thread
Results 1 to 1 of 1

Thread: Creating some sort of Thread listener

  1. #1
    ThemePark is offline Programmer
    Join Date
    Oct 2008
    Location
    Århus, Denmark
    Posts
    105
    Rep Power
    0
    I have a Server which constantly accepts connections from my Client. Upon receiving a connecting, it gets the socket with .accept() and creates a new Connection object which handles the Server-Client connection, stores the socket in that object, and then stores the Connection object in an ArrayList. So I have an ArrayList of the running Connections in my Server. Both Server and Connection extends Thread.

    Now, I have also made a Client and a gui for that client and made it so that when you press the Exit button, a boolean is set in the Connection object, finished = true. This allows the while loop which I use in the run method of the Connection object to terminate, and thus I know that the connection is to be terminated, and I can clean up everything in my Connection object.

    Now here is the problem. When the while loop is ended, the Connection's run method will end, and the boolean will have been set, thus indicating that this connection has ended, and can be removed. Now I want to somehow indicate to the Server that this particular connection has ended so I can remove it from the ArrayList and leave room for a new connection.

    I can come up with two ways of this. Either use a for loop and constantly run through the ArrayList of Connections to see if any of the connections have ended, i.e. if the boolean has been set to true. Or I can store the Server object in the Connection class, and when the run method ends, I can call a method in the Server class and use the Connection object as a parameter and clean it up that way.

    However, I would prefer a third, more elegant solution. Something similar of adding a listener to the Server class, that triggers whenever one of the Connection threads stops running. Is there a way of doing this?

    As far as I can tell, I can't edit my own posts, so sorry about this double post.

    Anyway, it occured to me that I should be able to make custom events, so I've been looking into that. I can tell that I will need an Event object, an Event Listener, and a class with some methods to add and remove listeners, and finally a method to fire the event.

    I am a bit confused as to what needs to go where though. Obviously firing the event goes into the Connection, at the end of the run method in my thread, but how about the rest?

    It's mostly the listeners that cause me problems. Do I add the listener to the Server or to the Connection? And do I have the array of listeners in the Server or the Connection?
    Last edited by xXHalfSliceXx; 01-16-2009 at 05:51 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate Implementing Listener logic for your own classes.
    By Sinipull in forum Java Tutorials
    Replies: 2
    Last Post: 09-18-2010, 01:55 PM
  2. listener in java
    By Ananta2010 in forum Java Help
    Replies: 4
    Last Post: 07-07-2010, 03:18 AM
  3. JButton Action Listener
    By patilsneha in forum Java Help
    Replies: 0
    Last Post: 12-17-2009, 02:39 AM
  4. mouseEntered listener
    By johnsonk in forum Java Help
    Replies: 4
    Last Post: 12-07-2009, 12:23 PM
  5. Creating a Thread
    By Lop in forum Managed C++
    Replies: 2
    Last Post: 07-20-2006, 06:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts