Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Threads and Synchronization and the fly likes synchronized Timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronized Timeout" Watch "synchronized Timeout" New topic

synchronized Timeout

Maalti Iyer
Ranch Hand

Joined: Jul 11, 2001
Posts: 52
Hello All,
How long will java waits before it times out on an object lock?. In otherwords, I've a socket connection to an external server
(code snippet :
synchronized(_obj) {
if (!_primaryInUse &&_outputStream1 != null) {
index =, 0, 27);
In a multi-threaded environment, there can only be one connection a client can establish with this external server (threads shouls share this connection).
Thanks for your time
maalti Iyer.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24193

"synchronized" never times out.
[ August 11, 2003: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
If you want to insert some sort of timeout here, you can try having another thread close() the outputStream if it detects no activity after a certain period of time. This is often unreliable though - the close() may simply block too. You should probably look into the NIO classes for nonblocking IO.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: synchronized Timeout
It's not a secret anymore!