Java Interface Issues

Earlier I wrote a query about Java threads. ( link text )

And based on the answers I decided to implement them. So, I made this coding bit on a machine with 2 processor 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)
     {

     }   }}

Hyperterminal 890, 81 ( 2 ), , , , " " " , (80 81). , , , , , , , . , , .

.

+3
5

accept . accept , , , . , [1] ServerSocket, , accept , .

[1] , ServerSocket , , , . java.nio -, , , .

+8

, .

runnable. , 1, , Thready.

+5

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

Socket.accept, . . Javadoc:

. , .

+3

2

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

,.accept() , .

, , 2. . "" , 2.

+3

, .

 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);
    }


}

( ) . .

0

Source: https://habr.com/ru/post/1729533/


All Articles