• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Alive check thread when reading from socket

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Michal Glowacki
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Michal Glowacki
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nitesh, this is what I needed!
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic