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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:08:26+00:00 2026-05-13T12:08:26+00:00

I had earlier posted a query on Java threads. ( link text ) And

  • 0

I had earlier posted a query on Java threads. ( link text)

And based on the answers i received, i decided to implement them. So ive done this bit of coding on a machine with 2 cpu cores. The code is as follows

import java.net.*;
import java.io.*;


public class thready implements Runnable{
private Socket num;

public thready(Socket a) {
    this.num=a;
}
public void run() {
    try {
        BufferedInputStream is = new BufferedInputStream(num.getInputStream());
        System.out.println("Connected to port"+num);
    } catch (IOException ex) {
        //Logger.getLogger(thready.class.getName()).log(Level.SEVERE, null, ex);
    }


}
public static void main(String [] args)
{
    int port = 80;
    int port1= 81;
    //int count = 0;
     try{

    ServerSocket socket1 = new ServerSocket(port);
    ServerSocket socket2 = new ServerSocket(port1);
    while (true) {
    Socket connection = socket1.accept();
    Socket connection1 = socket2.accept();

    Runnable runnable =new thready(connection);
    Runnable run= new thready(connection1);
    Thread t1=new Thread(runnable);
    Thread t2=new Thread(run);
    t1.start();
    t2.start();
    }
     }
     catch(Exception e)
     {

     }   }}

Now Im testing this piece of code using Hyperterminal and am connecting to both port 890 and port 81(am using 2 instances of the hyperterminal) and as i understand it the expected behavior should be that “Connected to port ‘port number'” should be printed as soon as a connection to any port( 80 or 81) is made. But the output that im getting here from this piece of code is that if i connect to only 1 port then the required output is not getting printed and if i connect to both ports, one after the other, the output is printed only after both ports are connected. So this again leads me to the initial confusion as to whether both these threads are executing concurrently or the execution is alternating between these 2 threads.

Any suggestions would be of great help.

Cheers

  • 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-13T12:08:26+00:00Added an answer on May 13, 2026 at 12:08 pm

    You’re calling accept before starting the threads. accept will block until a connection is made, so that’s why you’re seeing the behavior you do. If you want to listen on multiple ports, you will need to[1] create a thread for each ServerSocket and then either start a communication thread when the accept returns or process the connections one by one in the thread doing the listening.

    [1] This applies only if you are using ServerSocket directly, which you probably should be using while learning. The java.nio package and its subpackages contain classes for use with multiplexing non-blocking I/O that can be used to, e.g., listen on multiple sockets in the same thread.

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

Sidebar

Ask A Question

Stats

  • Questions 315k
  • Answers 315k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, don't use this code. There is almost never a… May 13, 2026 at 11:18 pm
  • Editorial Team
    Editorial Team added an answer The fewer number of JS files you serve, the faster… May 13, 2026 at 11:18 pm
  • Editorial Team
    Editorial Team added an answer 2008 is just like before in that it doesn't want… May 13, 2026 at 11:18 pm

Related Questions

I had posted this earlier on Stack Overflow, but couldn't get a positive result.
Hi I posted a question a little earlier and I had some replies that
(This is technically an addendum to an earlier StackOverflow question I had posted, but
For my apps, I store some configuration file in xml along with the assembly(exe),
This is a more direct question stemming from an earlier more general question i

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.