简体   繁体   中英

Socket accept and multiple threads

I have this issue I have no idea how to resolve and I'm at the brink of insanity. Programming, eh? :/

Anyway, I have a server which has a thread to send users all the info it needs to (which needs to run constantly) and another thread that awaits new server connections. My problem is once socket.accept() is called, the other thread doesn't execute.

So, to explain with code:

class Thread1 extends Thread {


  public void run() {
    while(true)
    {
      s=socket.accept();
    }
  }

class Thread2 extends Thread {

  public void run() {
    //do stuff
    System.out.println("spam");
  }
}

  public static void main(String[] args)
  {
    Thread1 t1 = new Thread1();
    t1.start();

    t1.Thread2 t2 = t1.new Thread2();
    t2.start();
  }
}

Assume all other required member variables are present, no compile errors and connection functionality works fine. Just 'Thread2' executes only once.

So my question is, how do I resolve this problem?

Thanks in advance,

Tim.

Some suggestions

  • never extend a Thread as it's a good way to confuse yourself. Never nest a Thread inside another Thread unless you really like confusion.
  • if you want to run a thread for each socket, then create a new thread for each socket in the loop.

Try the following (Note: You can add IOException handling code)

class SocketAcceptor implements Runnable {
    public void run() {
        while(true) {
            Socket s=socket.accept();
            SocketHandler sh = new SocketHandler(s);
            new Thread(sh).start();
        }
    }
}

class SocketHandler implements Runnable {
    final Socket s;
    SocketHandler(Socket s) { this.s = s; }
    public void run() {
        System.out.println("New connection " + s);
        s.close();
    }

}

A better solution would be to use a Thread pool. eg An ExecutorService, but I would get this working first.

I think you have a basic misunderstanding of threads. Let's see if we can clear that up.

Threads are simply another pipeline of execution. Think of them like tasks with a particular set of instructions. Once the task is done, the thread returns. Pretty simple idea, right?

In your example, Thread1 has an endless loop, which makes sense that it does run infinitely and does accept clients indefinitely.

However, Thread2 simply outputs some text and returns. There's nothing telling it to ' keep spinning '.

Within your main() , even though Thread2 is an inner class of Thread1 (which is kind of bad form to begin with, might I add) it doesn't actually force the thread to keep running.

You'll probably want to add a Queue to your server class that holds new sockets, and have Thread2 loop and check for entries.

Further Reading

Firstly, take a look at the Thread class . Its constructor takes a Runnable , so that's all you should be implementing when working with threads (ie class Foo implements Runnable and then new Thread(new Foo()) ).

If sockets are your fancy, perhaps some further reading on socket-server architecture and even about protocol design would be something you'd benefit from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM