aspose file tools
The moose likes Threads and Synchronization and the fly likes Alive check thread when reading from socket Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Alive check thread when reading from socket" Watch "Alive check thread when reading from socket" New topic
Author

Alive check thread when reading from socket

Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 113
I have an application that connects to remote socket and waits for xml messages. I need also to check if the time between those messages is not bigger than x seconds. I never wrote threaded application in java, could anyone help me? I am even more confused because of socket reading. Not sure how can I pause it and switch the thread. This is code snippet:

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35223
    
    7
For an introduction to Java sockets see http://java.sun.com/docs/books/tutorial/networking/sockets/index.html. It contains a working client/server pair.

Also read Don't println to a Socket; it looks as if the code you posted is susceptible to that problem.


Android appsImageJ pluginsJava web charts
Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 113
Ulf Dittmer wrote:For an introduction to Java sockets see http://java.sun.com/docs/books/tutorial/networking/sockets/index.html. It contains a working client/server pair.

Also read Don't println to a Socket; it looks as if the code you posted is susceptible to that problem.


I am sorry Ulf but I don't know how it applies to my question/problem. I am wondering, how to swap to check-alive-thread when awaiting an input from server socket. So, my app is continously awaiting data from socket, but another thread should make a check fx each second if the waiting time not exceeded x seconds.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Michal Glowacki wrote:I am wondering, how to swap to check-alive-thread when awaiting an input from server socket. So, my app is continously awaiting data from socket, but another thread should make a check fx each second if the waiting time not exceeded x seconds.


There is no need for swaping the thread reading the data. All it needs to do is have a state variable (volatile) that tells whether it has received any data or not.
As soon as you start reading start a Timer (simplest approach) which checks for the state variable every "x" minutes [Every X minutes if you want to keep the socket reader alive forever and it should get something every x seconds]. If the state variable says that the data is not received the interrupt the reader thread or inform the admin, as your application demands.


apigee, a better way to API!
Michal Glowacki
Ranch Hand

Joined: Mar 14, 2006
Posts: 113
Thanks Nitesh, this is what I needed!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Alive check thread when reading from socket
 
Similar Threads
How To Read Html Page Opened In Browser Using Java Program
Set Timer
replaceSelection() in TextPane
Not Sure
BufferedReader.readLine doesn't return null