开发者

Java threads doubt

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) {
       开发者_JS百科 //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


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.


You're doing a lot of initialisation before kicking off your threads and blocking there.

I would move all that code into the runnable. Then you would also avoid these duplicated variables names such as connection and connection1, have those objects owned by Thready.


The code

Socket connection = socket1.t();
Socket connection1 = socket2.accept();

uses Socket.accept which is a blocking method. See the javadoc:

Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.


You have these 2 lines

Socket connection = socket1.accept();
Socket connection1 = socket2.accept();

Now, .accept() blocks until a connection is made.

So that means when your code waits on the 2. line above. You'll never get around to start the thread for 'connection' until the 2. connection is made.


I tweaked the code a bit as per the suggestions and got it running, I guess. Here is the modified constructor and run method

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


}

This does(I guess) implement concurrent threads. Thank you all for your suggestions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜