Skip to content
Advertisement

Why can’t server and client be started together?

Relevant code:

JavaScript
JavaScript

Why can’t the server and client be started together in the @Test? I think it gets stuck in the infinite while loop but at the same time, shouldn’t there be context switching with the new threads started after accepting the connection?

I expected at least the name of the 2 new threads to be printed but it doesn’t happen.

Advertisement

Answer

Let us look carefully to your test code:

JavaScript

Ok, this lines creates a server, and the test thread is ready to execute next line

JavaScript

Ok, the test thread starts the server, and will be ready to execute the next line when the start method will return.

What happens in start:

JavaScript

Ok, you should see that message

JavaScript

Ok, you have created a ServerSocket

JavaScript

ok you a waiting for a connection (at serverSocket.accept()), will create a new thread to handle it as soon as you will get one, and loop again.

But as this point, the test thread is waiting and will never go to the following line to start the first connection. And it will remain stuck unless something else (maybe another thread) starts those damned connections.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement