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

The Archive Base Latest Questions

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

For a java application:Why isn’t there possible to start two different threads in the

  • 0

For a java application:Why isn’t there possible to start two different threads in the same main???

One thread that is listening form incoming connections to a port and another thread that listens for other connections to a totally different port???

Something like:

public static void main(String[] args) {

        // TODO Auto-generated method stub


         ClientThread clienThread= new ClientThread();
        new Thread(clienThread).start();

        ThreadPooledServer server = new ThreadPooledServer(6000);
        new Thread(server).start();
    }

Where ClientThread() and ThreadPooledServer() are two different threads!

Question:Why doesn’t java let me do that?

public class Start {

    Socket socket;
    private String  serverIpAddress="127.0.0.1";
    static Thread cThread;
    public static void main(String[] args) {
        // TODO Auto-generated method stub


         ClientThread clienThread= new ClientThread();
        new Thread(clienThread).start();

        ThreadPooledServer server = new ThreadPooledServer(6000);
        new Thread(server).start();




    }


}

public class ClientThread implements Runnable {

        public void run() {
            try {
                InetAddress serverAddr = InetAddress.getByName(serverIpAddress);

                socket = new Socket(serverAddr, 6100);

                System.out.println("s-a creat");
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host");
            } catch (IOException e) {
                System.err
                        .println("Couldn't get I/O for the connection to host");
            }


        }
    }

public class ThreadPooledServer implements Runnable {

    protected int serverPort =6000;
    public static String SERVERIP = "127.0.0.1";
    protected ServerSocket serverSocket = null;
    protected boolean isStopped = false;
    protected Thread runningThread = null;
    public static int clientconnection = 0;
    Vector<WorkerRunnable> workerList = null;

     protected ExecutorService threadPool =
            Executors.newFixedThreadPool(5);

    public ThreadPooledServer(int port) {
        this.serverPort = port;
        workerList = new Vector<WorkerRunnable>();
    }

    public void run() {
        synchronized (this) {
            this.runningThread = Thread.currentThread();
        }
        openServerSocket();

        while (!isStopped()) {
            Socket clientSocket = null;
            try {
                System.out.println("Serverul asteapta clienti spre conectare");
                clientSocket = this.serverSocket.accept();
                clientconnection++;
                System.out.println("Serverul a acceptat clientul cu numarul:"
                        + clientconnection);
                // Log.d("Server:","s-a acceptat un nou client");

            } catch (IOException e) {
                if (isStopped()) {
                    System.out.println("Server Stopped.");
                    return;
                }
                throw new RuntimeException("Error accepting client connection",
                        e);
            }
            // creare thread pt noul client conectat
            //WorkerRunnable workerRunnable = new WorkerRunnable(clientSocket);
            //Thread workerThread = this.threadPool.execute(new WorkerRunnable(clientSocket));
            //workerThread.start();
            this.threadPool.execute(new WorkerRunnable(clientSocket));
            //this.workerList.add(workerRunnable);
        }
        this.threadPool.shutdown();
        System.out.println("Server Stopped.");
    }

    private synchronized boolean isStopped() {
        return this.isStopped;
    }

    public synchronized void stop() {
        this.isStopped = true;
        try {
            this.serverSocket.close();
            // stop thread-uri workers
            for (int i = 0; i < this.workerList.size(); i++) {
                WorkerRunnable worker = this.workerList.elementAt(i);
                worker.stop();
            }
        } catch (IOException e) {
            throw new RuntimeException("Error closing server", e);
        }
    }

    private void openServerSocket() {
        try {
            InetSocketAddress serverAddr = new InetSocketAddress(SERVERIP,
                    serverPort);
            serverSocket = new ServerSocket();
            serverSocket.bind(serverAddr);

        } catch (IOException e) {
            throw new RuntimeException("Cannot open port 6000", e);
        }
    }
}

public class WorkerRunnable implements Runnable {

    protected Socket clientSocket = null;
    Scanner s = null;
    String longitude;
    String latitude;

    boolean stop = false;

    public WorkerRunnable(Socket clientSocket) {
        this.clientSocket = clientSocket;

    }

    public void run() {

        try {

            System.out.println("O noua conexiune acceptata"
                    + clientSocket.getInetAddress() + "cu portul"
                    + clientSocket.getPort());

            Scanner is = new Scanner(new DataInputStream(
                    this.clientSocket.getInputStream()));
            while (!stop) {

                while (is.hasNext()) {
                    try {
                        longitude = is.next();
                        latitude = is.next();
        System.out.println(longitude);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            is.close();
            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        this.stop = true;
    }

This is all I have!!!

EDIT:I moved the ClientThread code into a different class and I succeded to run my code,but now I can’t get to create two sockets.

I mean it creates one but for the second one I get:

“Couldn’t get I/O for the connection to host”

I assume that is coming from here:

 catch (IOException e) {

                System.err
                        .println("Couldn't get I/O for the 
connection to host");

        }

Does someone knows why??

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

    This has nothing to do with threads or sockets. The error you are getting is described here. Without seeing your code I’d say that the classes you are trying to instantiate are inner classes of Start. You don’t have an instance of Start because you’re in its static main() method.

    try something like:

    public class Start
    {
      public static void main(String[] args)
      {
        Start myStart = new Start();
        myStart.doIt();
      }
    
      private void doIt()
      {
        ClientThread clienThread= new ClientThread();
        new Thread(clienThread).start();
    
        ThreadPooledServer server = new ThreadPooledServer(6000);
        new Thread(server).start();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my java application has a loading task which requires two server calls which can
I have a Java application that launches another java application. The launcher has a
I'm writing a Java application that runs on Linux (using Sun's JDK). It keeps
Part of our java application needs to run javascript that is written by non-developers.
I have a Java application that monitors a folder for incoming XML files. When
If Java application requires certain JRE version then how can I check its availability
My Java application is saving stuff in 'user.home' but on windows this does not
My Java application is started from within a native program through java.dll. This native
I have a Java application which I want to shutdown 'nicely' when the user
I have a distributed Java application running on 5 application servers. The servers all

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.