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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:30:03+00:00 2026-05-26T15:30:03+00:00

I have a simple question, how to convert this server into multithreded as far

  • 0

I have a simple question, how to convert this server into multithreded as far as now it is dealing with only one client. Which part should go into run() part :)?

ServerSocket listener = new ServerSocket(9090);
System.out.println("server\n");
try {
    while (true) {
        Socket socket = listener.accept();
        System.out.println(socket+" " + "welcome\n");
        try { 
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            out.println(new Date().toString());
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String answer = input.readLine();
            System.out.println(answer);
            if("hej".equals(answer)){
                System.out.println("Sacrafice accepted");
            }
        } finally {
            socket.close();
        }
    }
} finally {
    listener.close();
}
  • 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-26T15:30:03+00:00Added an answer on May 26, 2026 at 3:30 pm

    Serving each connection returned from accept should go into a separate thread, i.e. the following should be moved into the run() method:

    System.out.println(socket + " " + "welcome\n");
    try {
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println(new Date().toString());
        BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String answer = input.readLine();
        System.out.println(answer);
        if ("hej".equals(answer)) {
            System.out.println("Sacrafice accepted");
        }
    }
    finally {
        socket.close();
    }
    

    Of course, you’ll need to add code that creates a thread. Since thread creation is an expensive task and threads are reusable it’s best to create a number of worker threads in a pool up-front during initialization and then only retrieve worker threads from the pool once you accept a connection and need a thread to service it. As you develop your application you may find that adding some logic to adjust thread pool size at run time based on load is a good idea, but you should probably abstain from this at the current stage and just use a configuration item (like a command line option or a static final) to set the initial thread pool size. You can find thread pool implementations in java.util.concurrent.

    If you do that, thread’s run() method will be very simple waiting in a loop for new tasks and every time it receives a task it should run that task’s run() method. The code above should be put into task’s not thread’s run() method. This way you will separate threads from tasks and hence make sure threads remain reusable. Threads will also need a method to receive tasks and that method should thread-safe. You can use one of the queue implementations from java.util.concurrent to store tasks in your threads between the time they’re passing into a thread for servicing and the time they’re taken out of the queue by thread’s run() method to actually run them.

    This separation of threads and tasks is yet another case when adding another level of indirection solves an important software engineering problem.

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

Sidebar

Related Questions

I'm implementing a very simple audio-only RTMP server. I have my client code like
I have a simple question and wish to hear others' experiences regarding which is
I have a simple question related to one-line programming. First an example: function test(a)
Next question about russian encoding, mssql and python. I have this simple code: import
I have a simple question about how to format a string. I have this
I have a simple question. Is there a way ( using reflections I suppose
I have a simple question about Java's XML API and I hope there's a
I have a simple question, but I'm about 80% sure that the answer to
I have a simple question that I am posing mostly for my curiousity. What
i have pretty simple simple question (i hope so). How do i change the

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.