Results 1 to 2 of 2
  1. #1
    DC200 is offline Member
    Join Date
    Dec 2008
    Rep Power

    Default One socket and multiple threads

    I'm trying to write a simple client that listens on a socket to perform certain tasks for other clients. Since one of functions takes longer to complete than the others, the rest of the quicker functions are rendered unavailable for that whole duration (if statements inside a while loop). As such, I thought it would be better to move the functions to separate threads: one thread to handle the time consuming function, and the second thread to other to handle the rest of the functions that only take milliseconds to complete.

    The while loop that listens to the socket's input stream was moved to the thread's run() method, and the two threads are sequentially initialized in the main method with the code below.

    Java Code:
    new Thread(new longFunction(socket)).start();
    new Thread(new quickFunctions(socket)).start();
    Both threads are running and listening to the socket, but not at the same time. Only one thread listens at a time, meaning the functions that belong to the other thread are unavailable. How would I go about fixing this problem?

    Edit: Below is the code used by both of the threads to listen to the socket. The listen() method is invoked in run() after the socket has been connected to the Scanner and PrintWriter (socket is passed to the thread when it is created).

    Java Code:
    	public void listen() throws IOException {
    		while (instream.hasNextLine()==true) {
    			command = instream.nextLine(); //String command
    			if (request.startsWith("task name")) {
    Last edited by DC200; 12-29-2009 at 08:36 AM.

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    First, only one thread should listen to the socket.

    For the long running tasks, start a thread to process them. Have that thread wait on some sort of queue (a Vector would probably work) and have the thread loop and wait for requests on the the queue. Other task would run on the listening thread.

    Note that I'm assuming that the long running task can run asynchronously, that is, that the application on the other end of the socket does not expect back an immediate response. In that case, it doesn't matter how long each task takes, they have to be run one at a time.

Similar Threads

  1. GUI and Threads
    By rp181 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-10-2009, 09:39 PM
  2. append response to the request from Socket and write to another socket
    By in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 08:02 PM
  3. multiple threads access the same method.
    By bhanu in forum New To Java
    Replies: 3
    Last Post: 02-16-2009, 07:54 AM
  4. Threads and Timeout in Socket Networking
    By byuu in forum Threads and Synchronization
    Replies: 2
    Last Post: 05-27-2008, 10:05 PM
  5. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 11:25 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