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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:58:47+00:00 2026-05-27T05:58:47+00:00

I am writing a multi-thread program in Java, where i am creating a seperate

  • 0

I am writing a multi-thread program in Java, where i am creating a seperate thread to handle each new client connection. I have:

Socket s;


while(true)
{
    s = server.accept();
    ClientHandler ch = new ClientHandler(s);
    Thread servingThread = new Thread(ch);
    servingThread.start();
}

In the ClientHandler thread i have:

public class ClientHandler implements Runnable
{
    private Socket s;

public ClientHandler(Socket _Socket, boolean _accepted, BaseStation _bs)
{
    this.s = _Socket;
}

If in Java i can’t pass an object but only a reference to it, isn’t that going to cause a problem, to the s instance inside ClientHandler whenever server accepts a new connection?
Is it not going to change inside ClientHandler too and corrupt it? If so, what’s the correct way to do it?

  • 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-27T05:58:47+00:00Added an answer on May 27, 2026 at 5:58 am

    Your concern is correct because the Socket object (like any other object in Java) is passed by reference, therefore there is a probability of it being referenced by different modules, or different threads, causing unpredictable behaviour.

    However in case of your program there is no problem. The reason is: whenever server accepts a new connection it will create a new Socket object, which you are then passing to a new instance of ClientHandler. Therefore every new connection from the client will be served by an independent ClientHandler instance, so there are no race conditions to the Socket object to worry about. So you are safe for now.

    As a counter-example if you decide to create two ClientHandler threads to read from the same Socket object like this …

    s = server.accept();
    ClientHandler ch = new ClientHandler(s);
    ClientHandler ch2 = new ClientHandler(s);
    new Thread(ch).start();
    new Thread(ch2).start();
    

    … then you might be in trouble because two ClientHandlers will get the same reference to the same Socket object and if both attempt to read from the same socket they will each get only half of the data. E.g. if you are receiving a string "hello", you might end up with ch reading "hel" and ch2 reading "lo", or any other combination.

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

Sidebar

Related Questions

I have implemented a multi-threaded program which involves spawning a thread for each user,and
I am on MacOSX. I am writing a multi threaded program. One thread does
I am writing multi-thread socket chat in C++Builder 2009. It is almost complete in
Latelly I've been working with multi-thread coding, after a while writing I realized that
I'am writing a multi-threaded program in C. Before creating the threads, a global python
I have a multi-threaded Delphi program creating multiple instance of some classes, and I
I'm writing a multi-threaded Java web crawler. From what I understand of the web,
I have a multi-project ASP.NET + C# solution. I was planning on writing a
I am writing a multi-threaded program using OpenMP in C++. At one point my
I'm writing a multi-threaded server using boost::asio (for sockets), boost::thread (for threading), libconfig++ (for

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.