Java Multithreading detects available IPs

Source: Internet
Author: User

Recently do the system performance optimization encountered a problem, after the solution to share to everyone.

When we establish a connection through the socket, if our computer cannot connect with the specified site, then this will cause the system to be stuck on the socket connect method,

We all know that the socket to establish a connection requires three handshake, the computer sends a message header to the server, the server returns, this time the socket basic connection is successful, but if the connection

Not on the words here will be card a timeout time, time is up, method returns failed, the socket default timeout seems to be 20 seconds,

Our system now has an available IP from the list of available IPs, which is taken out as available data to be saved to memory.

Before we were serial tests, one was not available to try the next, this operation stopped the orchestration for a long time in this step.

Now change to parallel test, turn on multiple thread tests, have the available IP to return directly, the time of this detection operation is reduced to the millisecond level, greatly improves the system performance.

The specific code is as follows:


Resource interface:

Package com.sunyard.frame.resource.base;/** * Initialized resources *  * @author Zhangwei * @since October 29, 2014 PM 6:58:55 * @version Z hw_1.1 */public abstract class Initable {/** * Initialize * @return True initialize successfully */public abstract Boolean init ();/** * Destroy the resource, such as closing the connection such as */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 Explorer, detects an available resource from a set of resources * * @ Author Zhangwei * @since October 29, 2014 PM 7:00:45 * @version zhw_1.1 */public class Resourcedetector {/** resources to be detected */private List <? Extends initable> resources;/** The thread that created the object */private thread mainthread;/** probe results */private initable result;/** The thread pool used for concurrent probing of available resources can be replaced with Java's executorservice */private ThreadPool pool = new ThreadPool (10),/** probe failed record Count */private Integer Failcount = 0;public Resourcedetector (list< extends initable> resources) {super (); this.resources = resources; This.mainthread = Thread.CurrentThread ();} /** * Probe starts probing available resources * * @author Zhangwei * @since October 29, 2014 PM 7:20:21 */public initable Detect () {if (listutils.isnotempty (resources)) {for (initable i:resources) {Pool.execute (Createdetecttask (i));} Synchronized (Mainthread) {try {mainthread.wait ();} catch (InterruptedexCeption e) {}}return result;} else {return null;}} /** Create a child thread that probes for 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.notify ();}}}} finally {i.destory ();}}};}}

Test class:

Here Socketdecorate is initable sub-class, Serversocketproxy is to start a service-side monitoring, we can implement, code too much will not copy up


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 (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); Socketdecorate 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); Socketdecorate d6 = new Socketdecorate ("168.1.1.6", 1000); Socketdecorate D7 = new Socketdecorate ("168.1.1.7", 1000); Socketdecorate D8 = new Socketdecorate ("127.0.0.1 ", 1000); list<socketdecorate> resources = new arraylist<socketdecorate> (); 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);}}



Java Multithreading detects available IPs

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.