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

  • SEARCH
  • Home
  • 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 6577687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:40:11+00:00 2026-05-25T15:40:11+00:00

I create a class that allows me to open a single instance of my

  • 0

I create a class that allows me to open a single instance of my Java program. It uses a daemon thread that open a ServerSocket. if the TCP Port was already taken throws an exception at instantiation time.

The code works normally under linux and windows.

Here is the code i am using:

public class SingleInstaceHandler extends Thread {

    private static final Logger log = Logger.getLogger(IEPLC_Tool.class);
    private boolean finished = false;

    @SuppressWarnings("unused")
    private ServerSocket serverSocket;

    /*
     * Constructor
     * Generate the server socket.
     * If the TCP door was busy throws IOException.
     */
    public SingleInstaceHandler() throws IOException {
        @SuppressWarnings("unused")
        ServerSocket serverSocket = new ServerSocket(44331);
        this.setDaemon(true);
        this.start();
        log.info("Server socket initialized"); //if commented out it works
    }

    public void run() {
        synchronized (this) {
            while (!finished) {
                try {
                    log.debug("Server socket goes to sleep");
                    this.wait();
                    log.debug("Server socket waken up");
                } catch (InterruptedException e) {
                    log.debug("ERROR while sending SocketThread2 in wait status");
                    e.printStackTrace();
                    System.exit(-1);
                }
                log.info("Server socket end");
            }
        }
    }

    public synchronized void shutdown() {
        log.debug("SingleInstaceHandler shutdown() caled");
        finished = true;
        notifyAll();
    }
}

Sometimes instead the port is not kept busy… any idea?


UPPENDED AFTER FURTHER TESTS:

running many other tests. it seams that if the port is taken by something like another SW instance new ServerSocket(44331); throws an exception but sometimes even if the port is not taken for some reason it can not get this resource. in this case no exception is launched and i can open as many instance as i want of my application. maybe i should do some other operation to force the thread to lock the port…

any idea?

Thanks,

Ste

  • 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-25T15:40:11+00:00Added an answer on May 25, 2026 at 3:40 pm

    I do feel a bit stupid in posting an answer to my question… well.. my code has a bug that took me long to figure out:

    the problem is that in te constructor i do:

    ServerSocket serverSocket = new ServerSocket(44331);
    

    instead of:

    this.serverSocket = new ServerSocket(44331);
    

    I did not notice it before… basicaly the bug was that I was declaring a local socket within the constructor. When the constructur procedure was terminated the socket was released or not depending on the Garbadge Collector. Behavior was quite random. Funny part was that plenty of time calling/not calling the logger was enougth to make the Garbadge collector starting or not. It took me quite a while in noticing the mistake.

    furthermore it’s better to put a :

    this.serverSocket.close() after the wait.

    Thans for helping anyway!

    Cheers,

    Ste

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

Sidebar

Related Questions

Is there any easy way to create a class that uses IFormatProvider that writes
I want to create a class that initializes a timer which will be used
I would like to create a class that creates and manages log files. I
I am trying to create a class that implements the IUnknown interface. I have
I'm trying to create a class that can instantiate arrays at runtime by giving
When I create utility classes I typically create a class that has a private
A common pattern in C++ is to create a class that wraps a lock
Hey I usually run into a situation where I will create a class that
I'm trying to figure out the best way to create a class that can
In python, is there a way to create a class that is treated like

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.