I have thread which writes something to socket and then starts waiting for response. This thread needs to retrieve response in a given time, 100 milliseconds for example. My idea is to create reader thread which will read from socket and store response in a blocking queue. First thread just waits when reader finishes work and put object in the queue. If reader does it in a time everything OK, if reader does not I want to throw exception and it's also OK. The problem is that when I come after TimeOutException, object that wasn't taken from queue because of timeout is still there, and may be even just prepared to be inserted into the queue. And I want only next object. How should I skip this message? Or may be there is better approach to this problem?
Also, connection is keep alive, so socket timeout is not suitable.