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