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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:14:33+00:00 2026-05-17T18:14:33+00:00

When you google for multi-threaded java server , most of the time you’ll get

  • 0

When you google for multi-threaded java server, most of the time you’ll get a solution based on a following pattern:

public class MultiThreadServer implements Runnable {
  private Socket socket;

  MultiThreadServer(Socket socket) {
    this.socket = socket;
  }

  public static void main(String args[]) throws Exception {
    ServerSocket serverSocket = new ServerSocket(4444);
    while (true) {
      new Thread(new MultiThreadServer(serverSocket.accept())).start();
    }
  }

  public void run() {
    // ...
    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(
                new InputStreamReader(
                socket.getInputStream()));
    // ...
  }
}

How does that relate to safe publication idioms presented in Java Concurrency in Practice?

To publish an object safely, both the
reference to the object and the
object’s state must be made visible to
other threads at the same time. A
properly constructed object can be
safely published by:

  • Initializing an object reference from
    a static initializer.
  • Storing a
    reference to it into a volatile field.
  • Storing a reference to it into a final
    field.
  • Storing a reference to it into
    a field that is properly guarded by a
    (synchronized) lock.

Is it really safe to use socket within run() method without any extra synchronization, is it safely published? Please do write more than just yes or no.

  • 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-17T18:14:34+00:00Added an answer on May 17, 2026 at 6:14 pm

    Yes, it’s safe because start of the thread defines a happens-before relation. That is, MultiThreadServer constructor call happens-before execution of the thread’s run method.

    From Java Language Specification 17.4.4-17.4.5:

    A call to start() on a thread happens-before any actions in the started thread

    Precautions from Java Concurrency in Practice are applicable in the case when the object is made accessible to other existing threads, so they are not applicable to this case.

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

Sidebar

Related Questions

I currently have heavily multi-threaded server application, and I'm shopping around for a good
Google is not being my friend - it's been a long time since my
Google/MyBrain are failing me. Without using a framework (which I'll never get past my
I've read a lot recently about how writing multi-threaded apps is a huge pain
I've been writing a multi-threaded DLL for database access using ADO/ODBC for use with
Google Analytics reads like a seismic chart lately
Google maps in some region can serve traffic information showing the blocked roads and
Google's not coming to my rescue, here, and I just know this is the
Google results on this one are a bit thin, but suggest that it is
Google chrome doesn't behave the same as other browsers when encountering this nugget: <?php

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.