Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 708369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:23:00+00:00 2026-05-14T04:23:00+00:00

In a multithreaded Java application, I just tracked down a strange-looking bug, realizing that

  • 0

In a multithreaded Java application, I just tracked down a strange-looking bug, realizing that what seemed to be happening was this:

  • one of my objects was storing a reference to an instance of ServerSocket
  • on startup, one thread would, in its main loop in run(), call accept() on the socket
  • while the socket was still waiting for a connection, another thread would try
    to restart the component
  • under some conditions, the restart process missed the cleanup sequence before it reached the initialization sequence
  • as a result, the reference to the socket was overwritten with a new instance, which then wasn’t able to bind() anymore
  • the socket which was blocking inside the accept() wasn’t accessible anymore, leaving a complete shutdown and restart of the application as the only way to get rid of it.

Which leaves me wondering: a) Does the blocking call prevent, or interfere with, GC in any way? b) If the ServerSocket does get GCed, will that make the socket available again?

In general, what are good practices I can follow to avoid this type of bug? For instance, I learned two lessons here:

  • All lifecycle logic (i. e. component level, init-start-stop-cleanup cycles) must be synchronized. Rather obvious, I guess, but I didn’t take it seriously enough.
  • Lifecycle logic should be as simple as possible to avoid my problem of non-obvious code paths that skip cleanup or initialization steps.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:23:00+00:00Added an answer on May 14, 2026 at 4:23 am

    You should always close() ServerSocket if you want a predictable cleanup.

    The fact that all the references have been dropped only means that it is available for GC, not that it ever will be GC’ed. With available process memory in megabytes, there is a very small chance that a single object will be garbage collected in short period.

    Usually you call close from a maintenance thread. In many server apps it would be called from the shutdown hook.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a MultiThreaded Java Application that will be used to load
I'm trying to make a multithreaded server/client app with java ! this code is
My java application has to send messages(multithreaded) to a socket server. The application can
I have a multi-threaded Java application that will output information about a message it
We have a (very) multithreaded application that would pass all unit tests, but would
Possible Duplicate: Would a multithreaded Java application exploit a multi-core machine very well? I
imagine a transactional, multithreaded java application using spring, jdbc and aop with n classes
In a multithreaded Java application I need to iterate over a collection of objects.
More specifically, I have a multithreaded command line Java application which runs and collects
I am using java to create an application for network management. In this application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.