Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notify another thread when ServerSocket blocks on accept()

I've got a pair of threads running a Socket and ServerSocket:

runThreadA() {
     // Connects to B, or fails if B is not yet accepting
     socket.connect();
}

runThreadB() {
    // Blocks until another thread connects
    serverSocket.accept();
}

Is there a way I can guarantee that B calls accept() and blocks before A calls connect()?

like image 255
user2424511 Avatar asked Apr 25 '26 07:04

user2424511


1 Answers

Check CyclicBarrier and CountDownLatch which can help you make the threads to wait each other, they might be helpful.

like image 156
Katona Avatar answered Apr 26 '26 20:04

Katona