Results 1 to 2 of 2
  1. #1
    enzom83 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Creating new threads from another thread

    Well, I should create a class (called Server), which represents a multi-thread server. This class consists of a constructor, where I can read a specific configuration file to load the port on which the server should listen and other configuration information.
    In addition, the Server class contains a list attribute declared as private and some public methods that allow you to change this list.

    Any request to modify the list by a client must be handled by a thread. To this end, the server must wait for incoming connections (using a ServerSocket accept), and for each connection request, it must create a new thread.

    How can I start the server, implemented in Server class, without adding additional methods to the class Server itself?

    I thought I'd create a primary thread in the constructor of the Server class: I could put the code that creates a new ServerSocket, etc., and that creates others secondary threads inside the run method of this this primary thread. For example (try/catch omitted for simplicity),

    Java Code:
    public class Server {
        private Vector<Object> list;
        
        public Server() {
            // load configuration file
            ...
            // creates and starts a PrimaryThread
        }
    }
    public class PrimaryThread implements Runnable {
        ...
        public PrimaryThread(...) {
            ...
        }
    
        ...
        
        public void run() {
            ServerSocket listening = new ServerSocket(4444);
            ...
            while(true) {
                listening.accept();
                ....
                // creates a new SecondaryThread
            }
        }
    }
    
    public class SecondaryThread implements Runnable {
        ...
        public SecondaryThread(...) {
            ...
        }
    
        ...
        
        public void run() {
            // reply to the client
        }
    }
    
    public class MainClass {
        public static void main(...) {
            Server myServer = new Server();
        }
    }
    The above code should work, but I wonder if it is a good practice: it is a good way to program? Or would it be more correct to add a start and a stop methods to the Server class and remove the PrimaryThread?

    Thanks a lot!
    Last edited by enzom83; 11-29-2010 at 01:45 AM. Reason: I had forgotten to write one line of code.

  2. #2
    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Default

    Your use case leads me to think of following actors:

    - Server: Responsible for managing resources and is sitting at teh very top
    - Worker: Responsible for doing the actual work
    - Contractor: Responsible for managing the contract between the Server and the Worker. Server can do many kinds of work, and if you create the start and stop in the Server class, you will limit its ability. the class 'PrimaryThread' is like a contract between the two. Your design is correct and will scale if you choose to use the server to do different type of things.

Similar Threads

  1. keep child threads running after parent thread dies
    By adammyth in forum Threads and Synchronization
    Replies: 2
    Last Post: 01-27-2010, 02:43 PM
  2. a way for creating a Thread
    By arefeh in forum New To Java
    Replies: 6
    Last Post: 01-02-2010, 06:37 PM
  3. Which method is best for creating Threads?
    By makpandian in forum Threads and Synchronization
    Replies: 11
    Last Post: 06-08-2009, 09:00 AM
  4. how to wrk with twa threads then compile both to 1 thread
    By dmotah in forum Threads and Synchronization
    Replies: 0
    Last Post: 02-04-2008, 10:53 AM

Posting Permissions

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