Results 1 to 2 of 2
- 11-29-2010, 12:42 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
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(); } }
Thanks a lot!Last edited by enzom83; 11-29-2010 at 12:45 AM. Reason: I had forgotten to write one line of code.
- 12-03-2010, 10:15 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 22
- Rep Power
- 0
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
-
keep child threads running after parent thread dies
By adammyth in forum Threads and SynchronizationReplies: 2Last Post: 01-27-2010, 01:43 PM -
a way for creating a Thread
By arefeh in forum New To JavaReplies: 6Last Post: 01-02-2010, 05:37 PM -
Which method is best for creating Threads?
By makpandian in forum Threads and SynchronizationReplies: 11Last Post: 06-08-2009, 08:00 AM -
how to wrk with twa threads then compile both to 1 thread
By dmotah in forum Threads and SynchronizationReplies: 0Last Post: 02-04-2008, 09:53 AM
Bookmarks