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