Java multi-thread detection of available IP addresses
I encountered a problem when I was doing system performance optimization recently. I will share it with you after solving the problem.
When we establish a connection through socket, if our computer cannot connect to the specified site, this will cause the system to be stuck in the socket connect method,
We all know that the socket connection requires three handshakes. The computer sends a message header to the server and the server returns the result. At this time, the socket connection is successful.
If it doesn't, a Timeout will be stuck here. When the time reaches, the method returns an error. The default timeout of the socket seems to be 20 seconds,
Our system now detects an available ip address from the available ip address list and saves it as available data to the memory.
Before that, we performed a serial test. If one was unavailable, we tried the next operation. This operation stopped the business process for a long time.
Now it is changed to parallel testing. When multiple threads are started for testing and available ip addresses are returned directly, the detection operation time is reduced to milliseconds, greatly improving the system performance.
The Code is as follows:
Resource interface:
Package com. sunyard. frame. resource. base; /*** initializable resources ** @ author zhangWei * @ since October 29, 2014 6:58:55 * @ version zhw_1.1 */public abstract class InitAble {/*** initialization * @ return true Initialization succeeded */public abstract boolean init (); /*** destroy the resource, such as closing the connection. */public abstract void destory ();}
Detector:
Package com. sunyard. frame. resource; import java. util. list; import com. sunyard. frame. resource. base. initAble; import com. sunyard. frame. thread. threadPool; import com. sunyard. frame. utils. listUtils;/*** resource detector, detect an available resource from a group of resources ** @ author zhangWei * @ since October 29, 2014 7:00:45 * @ version zhw_1.1 */public class ResourceDetector {/** resources to be detected */private list
Resources;/** create the Thread of the object */private Thread mainThread;/** test result */private InitAble result;/** used to concurrently detect the Thread pool of available resources, java ExecutorService can be used to replace */private ThreadPool pool = new ThreadPool (10);/** number of failed detection records */private Integer failCount = 0; public ResourceDetector (List
Resources) {super (); this. resources = resources; this. mainThread = Thread. currentThread ();}/*** detector starts to detect available resources ** @ author zhangWei * @ since 7:20:21 on October 18 */public InitAble detect () {if (ListUtils. isNotEmpty (resources) {for (InitAble I: resources?#pool.exe cute (createDetectTask (I);} synchronized (mainThread) {try {mainThread. wait () ;}catch (InterruptedException e) {}} return result ;}else {return null ;}} /** create a subthread to test a resource */private Runnable createDetectTask (final InitAble I) {return new Runnable () {@ Overridepublic void run () {try {if (I. init () {result = I; synchronized (mainThread) {mainThread. notify () ;}} else {synchronized (failCount) {if (++ failCount = resources. size () {synchronized (mainThread) {mainThread. policy () ;}}}} finally {I. destory ();}}};}}
Test class:
SocketDecorate is a subclass of InitAble, and ServerSocketProxy is used to start a server listener. You can implement it on your own. If there are too many codes, it will not be copied.
Package test. resource; import java. io. IOException; import java. util. arrayList; import java. util. list; import test. socket. serverServiceImpl; import junit. framework. testCase; import com. sunyard. frame. resource. resourceDetector; import com. sunyard. frame. socket. client. socketDecorate; import com. sunyard. frame. socket. server. serverSocketProxy; public class TestResource extends TestCase {public void testDetect () {// create a ServerSocketServerSocketProxy ss; try {ss = new ServerSocketProxy (1000, ServerServiceImpl. class, true); ss. startServer ();} catch (IOException e) {} SocketDecorate d1 = new SocketDecorate ("168.1.1.1", 1000); SocketDecorate d2 = new SocketDecorate ("168.1.1.2", 1000 ); export d3 = new SocketDecorate ("168.1.1.3", 1000); SocketDecorate d4 = new SocketDecorate ("168.1.1.4", 1000); SocketDecorate d5 = new SocketDecorate ("168.1.1.5", 1000 ); export d6 = new SocketDecorate ("168.1.1.6", 1000); SocketDecorate d7 = new partition ("168.1.1.7", 1000); SocketDecorate d8 = new SocketDecorate ("127.0.0.1", 1000); List
Resources = new ArrayList
(); Resources. add (d1); resources. add (d2); resources. add (d3); resources. add (d4); resources. add (d5); resources. add (d6); resources. add (d7); resources. add (d8); ResourceDetector detector = new ResourceDetector (resources); SocketDecorate s = (SocketDecorate) detector. detect (); System. out. println (s );}}