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 8474987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:46:27+00:00 2026-06-10T17:46:27+00:00

I wrote a thread class that checks the socket connection to the server by

  • 0

I wrote a thread class that checks the socket connection to the server by sending a small string every one second.

begin() method executes the thread.

After connection is lost, the thread tries to connect again.

My question is if it’s ok to re-run by begin() the thread inside the run() method like I did (see below).

public void begin() {  
   Check = new Thread(this);
   Check.start();
}

@Override
public void run() {
   Thread thisThread = Thread.currentThread();
   while (Check==thisThread) {
      try {
         oos.writeObject("a");
         // oos.flush();

         synchronized (this) {
            while (pleaseWait) {
               try {
                  System.out.println("waiting");    
                  wait();
                  System.out.println("not waiting");      
               } 
               catch (Exception e) {
                  System.err.println("Thread is interrupted: "+e.getMessage());
               }
            }
         }
         sleep(1000);
         } catch (Exception ex) {
              v = new Visual("The connection is lost. The system will try to reconnect now.");
              this.end();
              try {
                 Server=ClientLogin.checkingServers(); //returns the reachable server string address
                 socket = new Socket(Server, ServerPort);
                 System.out.println("Connected: " + socket);
                 oos = new ObjectOutputStream(socket.getOutputStream());
                 begin();
                 v = new Visual("The system is reconnected.");
              }
              catch(UnknownHostException uhe){  
                 System.out.println("Host unknown: " + uhe.getMessage());
                 v = new Visual("The system has failed to reconnected.");
              }
              catch (IOException ioe) {
                 System.out.println("The system cannot connect to servers: " + ioe.getMessage());
                 v = new Visual("The system has failed to reconnected.");
              }
              catch (Exception e) {
                 System.out.println("The system has failed to reconnect: " + e.getMessage());
                 v = new Visual("The system has failed to reconnected.");
              }
          }
   }
}

public void end() {
   Check = null;
}
  • 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-06-10T17:46:29+00:00Added an answer on June 10, 2026 at 5:46 pm

    I don’t know any reason why that wouldn’t work, but it looks kinda messy. You may have to declare Check as volatile to ensure that the loop always reads the current value, for those times when the new thread overwrites it.

    IMHO a better approach would be a separate “supervisor” thread which is responsible for starting one of these threads, and then uses Thread.join() to wait for it to die, at which point it can start it up again.

    In this way your main thread’s logic can concentrate on exactly what it’s supposed to do, without needing to have any “self awareness”.

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

Sidebar

Related Questions

I've got a small, very simple, winforms which calls one class and is writing
The setting: I want to write a point-to-point Connection class that, when used, does
I am writing an Objective-C class that I want to be thread safe. To
I wrote a small application to use as a sandbox for testing ideas that
I wrote a simple implementation class: public interface MyRemote extends Remote { public String
I'm trying to understand concurrency in Go. In particular, I wrote this thread-unsafe program:
I wrote little WPF application with 2 threads - main thread is GUI thread
If you read this thread before - forget everything I wrote , I must
I wrote a window service that performs some task using the threads.Now i am
Lets write simple console application: static void Main(string[] args) { IList<Thread> threads = new

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.