Java deep multi-thread Program Model Research

Source: Internet
Author: User
Java language deep multi-threaded Program Model Research-general Linux technology-Linux programming and kernel information, the following is a detailed reading. Multithreading is an indispensable part of complex programming. To improve the performance of application programs, multithreading is a feasible solution. This article describes examples of scanning computer ports written in Java to illustrate the issues that should be paid attention to in the design of multithreading and to draw a commonly used multi-threaded model.

This article requires readers to have a certain degree of Java language foundation and a certain understanding of Socket. All the programs in this article are compiled and run properly in Java SDK 1.4.2.

Now, we need to scan a host to find out which ports are open. First, we use a single thread for processing. The program code is as follows:
Bytes -------------------------------------------------------------------------------------------------------
Import java. io. IOException;
Import java.net. Socket;
Import java.net. UnknownHostException;

Public class PortScannerSingleThread {
Public static void main (String [] args ){
String host = null; // The first parameter, the target host.
Int beginport = 1; // The second parameter, the Start port.
Int endport = 65535; // The third parameter indicates the end port.
Try {
Host = args [0];
Beginport = Integer. parseInt (args [1]);
Endport = Integer. parseInt (args [2]);
If (beginport <= 0 | endport> = 65536 | beginport> endport ){
Throw new Exception ("Port is illegal ");
}
} Catch (Exception e ){
System. out. println ("Usage: java PortScannerSingleThread host beginport endport ");
System. exit (0 );
}


For (int I = beginport; I <= endport; I ++ ){
Try {
Socket s = new Socket (host, I );
System. out. println ("The port" + I + "is opened at" + host );
} Catch (UnknownHostException ex ){
System. err. println (ex );
Break;
} Catch (IOException ex ){
}
}
}
}
Bytes --------------------------------------------------------------------------------------------------------
In the above program, the java.net. Socket class is used to identify whether the port is open. The program accepts three parameters. The first parameter is the Host IP address, and the second and third parameters are the port numbers (1 ~ 65535 ). The running result of this program (java PortScannerSingleThread 10.1.1.1 1 1000) is as follows:
The port 25 is opened at 10.1.1.182
The port 110 is opened at 10.1.1.182
The port 135 is opened at 10.1.1.182
...

However, the above program running efficiency is not flattering. It takes 10 minutes or even longer to scan the target host port. It is estimated that no user can tolerate this efficiency.

Therefore, it is necessary to improve the processing efficiency of programs. The following programs are processed through multiple threads. The program code is as follows:
Bytes ----------------------------------------------------------------------------------------------------------
Import java. io. IOException;
Import java.net. Socket;
Import java.net. UnknownHostException;

Public class PortScannerMultiThread {
Public static void main (String [] args ){
String host = null;
Int beginport = 1;
Int endpoints = 65535;
Try {
Host = args [0];
Beginport = Integer. parseInt (args [1]);
Endport = Integer. parseInt (args [2]);
If (beginport <= 0 | endport> = 65536 | beginport> endport ){
Throw new Exception ("Port is illegal ");
}
} Catch (Exception e ){
System. out. println ("Usage: java PortScannerSingleThread host beginport endport ");
System. exit (0 );
}


For (int I = beginport; I <= endport; I ++ ){
PortProcessor pp = new PortProcessor (host, I); // create a thread for a port
Pp. start ();
}
}
}

Class PortProcessor extends Thread {
String host;
Int port;

PortProcessor (String host, int port ){
This. host = host;
This. port = port;
}

Public void run (){
Try {
Socket s = new Socket (host, port );
System. out. println ("The port" + port + "is opened at" + host );
} Catch (UnknownHostException ex ){
System. err. println (ex );
} Catch (IOException ioe ){
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.