Java multithreading one of those things

Source: Internet
Author: User

1. Callable Interface

Gets the thread run state (GET, get (long timeout)), and cancels the thread (mayinterruptifrunning). Iscancel,isdone and so on.

1. Implement the Callable interface,

2. Create a thread pool. Submit Thread


For example, see the following:

future<? > processfuture = Submittask (            new Processpbxphonethread (queue, Phonehandler, Runninglock, Runningflagmap));

 Private class Processpbxphonethread implements callable<boolean> {private blockingqueue<pbxphone> que      Ue      Private ipbxnotificationhandler<pbxphone> Phonehandler;      Private Readwritelock Runninglock;      Private map<string, boolean> Runningflagmap;      private int processedphonecount = 0;      Private long currenttime; Public Processpbxphonethread (blockingqueue<pbxphone> queue, ipbxnotificationhandler<pbxphone> phone Handler, Readwritelock Runninglock, map<string, boolean> runningflagmap) {this.queue = Queu         E         This.phonehandler = Phonehandler;         This.runningflagmap = Runningflagmap;      This.runninglock = Runninglock; } @Override public Boolean call () throws Exception {while (true) {PBXP            Hone phone = Queue.take (); if (processedphonecount% = = 0) {Long now = System.currenttimemIllis (); if (currenttime! = 0) {Double speed = Processedphonecount * 1000.0 */(Now-currentti                  ME);               Log.debug ("Processpbxphonethread phone process speed:{}*m", speed);               } currenttime = Now;            Processedphonecount = 0;            } processedphonecount++;               if (null! = Phone.getlines ()) {currenttime ("Invokephonehander"); pbxnotification<pbxphone> notification = new Pbxnotification<pbxphone> (phonehandler.getno               Tificationtype (), Notificationoperation.insert, Phone.getuuid (). toString (), phone);                  if (!phonehandler.objectchanged (notification)) {logspenttime ("Invokephonehander");                     try {runninglock.writelock (). Lock ();      Runningflagmap.put ("Runningflag", false);               Break                  } finally {Runninglock.writelock (). Unlock ();               }} else {logspenttime ("Invokephonehander");            }} else {break;      }} return true; }   }

Submittask submits the thread using the encapsulated thread pool:

Protected future<? > Submittask (callable< > Task)   {      return this.cucmdriverfactory.<strong style= "Background-color : RGB (255, 0, 0); " >getExecutorService</strong> (). Submit (Task);   }

The thread pool is created such as the following code:

  @Override public   Void Initialize ()   {      Executorservice = Executors.newcachedthreadpool (new Defaultthreadfactory ("cucmdriverfactor-"));   }
@Override public Executorservice Getexecutorservice () {if (Executorservice.isshutdown () | | | executorservice.ister      Minated ()) {initialize ();   } return This.executorservice; }

Threadfactory creates such as the following:

/** * the Thread factory, with name Prefix. Copied from Threadpoolexecutor.defaultthreadfactory */public static class Defaultthreadfactory implements Threadfact       Ory {private final threadgroup group;       Private final Atomicinteger threadnumber = new Atomicinteger (1);       Private final String Nameprefix; Public defaultthreadfactory (String nameprefix) {SecurityManager s = 

Java multithreading One of those things

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.