呼び出し方法:
/** * Clicks/month(year)callableの数 */public void yearlyclickCallable(){//パラメーターstring year = getPara( "year"); //統計データセットxリスト<string> xlist = new ArrayList <String>(); xlist.add( "1月"); xlist.add( "February"); xlist.add( "March"); xlist.add( "April"); xlist.add( "may"); xlist.add( "June"); xlist.add( "7月"); xlist.add( "7月"); xlist.add( "August"); xlist.add( "9月"); xlist.add( "10月"); xlist.add( "11月"); xlist.add( "12月"); // Statistics Dataset y List <Integer> ylist = new ArrayList <Integer>(); //スレッド値<future <future <list <map <string、object >>>>>>> futurelist = new arrayList <future <list <map <string、object >>>>(); // counter int count = 0; //スレッドプールを作成します(開始するスレッドの数を決定)executorservice pool = executors.newcachedthreadpool(); //(int m = 1; m <= 12; m ++){//日付パラメータリスト<string> datelist = new arrayList <string>(); // string date = ""; // int int days = calendarutil.weekformonth(integer.valueof(year)、m)を決定します。 //(int i = 1; i <= days; i ++){if(i <= 9){if(m <= 9){date = year + "-0" + m + "-0" + i; } else {date = year + " - " + m + "-0" + i; }} else {if(m <= 9){date = year + "-0" + m + " - " + i; } else {date = year + " - " + m + " - " + i; }} datelist.add(date); } // future <list <map <string、object >>> future = pool.submit(new readlogfilecallablebyyear(datelist)); FutureList.Add(Future); } //スレッドプールを閉じます。shutdown(); //(future <list <map <map <string、object >>>> future:futurelist){// parameter list <map <string、object >>> list = future.get(1、timeunit.seconds); //(int p = 0; p <list.size(); p ++){count+=(int)list.get(p).get( "clickCount");のパラメーターを設定します。 if(list.get(p).get( "month")。equals( "01")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "02")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "03")){ylist.add((integer)list.get(p).get( "clickcount"); } else if(list.get(p).get( "month")。equals( "03")){ylist.add((integer)list.get(p).get( "clickcount"); } else if(list.get(p).get( "month")。equals( "03")){ylist.add((integer)list.get(p).get( "clickcount"); } else if(list.get(p).get( "month")。equals( "04")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "05")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "06")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "06")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "06")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "07")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "08")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "09")){ylist.add((integer)list.get(p).get( "clickcount"); } else if(list.get(p).get( "month")。equals( "09")){ylist.add((integer)list.get(p).get( "clickcount"); } else if(list.get(p).get( "month")。equals( "09")){ylist.add((integer)list.get(p).get( "clickcount"); } else if(list.get(p).get( "month")。equals( "10")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "11")){ylist.add((integer)list.get(p).get( "clickcount")); } else if(list.get(p).get( "month")。equals( "12")){ylist.add((integer)list.get(p).get( "clickcount")); }}}} catch(Exception e){e.printstacktrace(); }} setattr( "totalcount"、count); setattr( "x"、xlist); setattr( "y"、ylist); renderjson(); }マルチスレッドメソッド:
package com.ninemax.util.loganalysis;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.map; Import java.util.concurrent.callable; Import com.ninemax.util.loganalysis.tool.constantutil;/** * Multithreaded Return Value * * @author Darker * * */public class class readlogfilecallablebyyear // return result set public list <map <string、object >>> list = new arraylist <map <string、object >>(); public readlogfilecallablebyyear(list <string> clickdate){this.clickdate = clickdate; } @Override public List <Map <String、object >> call()throws Exception {// pared parameters map <string、object> map = new hashmap <string、object>(); // fileinputStreamを使用してファイル情報fileinputStream fis = nullを読み取ります。 // inputStreamReaderを使用してinputStreamReader reader = nullをトランスコードします。 // bufferedreaderを使用してbufferedreader bufreader = null; // stringbufferを使用してファイルコンテンツコンテナstringbuffer buf = new StringBuffer(); // clicks/month int monthclick = 0; for(int i = 0; i <clickdate.size(); i ++){//ファイルclicklogfile = newファイルを取得する(constantutil.loglocation、 "article.click。"+ clickdate.get(i)+ ".txt"); //ファイルが存在するかどうかを決定する場合(!clicklogfile.exists()|| clicklogfile.isdirectory()){system.err.println(clickdate.get(i) + "ファイルは存在しません..."); map.put( "month"、clickdate.get(i).substring(5、7)); map.put( "ClickCount"、0); list.add(マップ);返品リスト。 } else {try {//ノードストリームfis = new fileinputStream(clicklogfile); //ストリームreader = new inputstreamReader(fis、 "utf-8"); //ストリームの処理bufreader = new BufferedReader(Reader); // counter int count = 0; // string line = ""を読み取ります。 // fileを読み取りhired((line = bufreader.readline())!= null){// count count ++; //データの受信if(!line.equals(null)&&!line.equals( "")){buf.append(line + "/n"); }} if(count == 0){count = 0; } else {count = count -1; } monthclick += count; } catch(Exception e){e.printstacktrace(); }最後に{// close stream try {bufreader.close(); reader.close(); fis.close(); } catch(ioexception e){e.printstacktrace(); }}}} // result set map.put( "month"、clickdate.get(0).substring(5、7)); if(monthclick == 0){map.put( "clickCount"、0); } else {map.put( "clickcount"、monthclick); } list.add(map);返品リスト。 }}ネチズンの例を共有しますが、これもとても良いです
java.util.concurrent.callable; Import java.util.concurrent.executorservice; Import java.util.concurrent.executors; import java.util.concurrent.future; Callableインターフェイスと実行可能な実装クラスを実装するクラスは、他のスレッドで実行できるタスクです。 * callableとrunnableにはいくつかの違いがあります。 *(1)callableで指定されたメソッドはcall()であり、runnableで指定されたメソッドは実行されます()。 *(2)呼び出し可能なタスクは、実行後に値を返すことができますが、実行可能なタスクは値を返すことができません。 *(3)call()メソッドは例外をスローできますが、run()メソッドは例外をスローできません。 *(4)呼び出し可能なタスクを実行すると、将来のオブジェクトを取得できます。将来は非同期計算の結果を表します。 *計算が完了したかどうかを確認し、計算が完了するのを待つ方法、計算の結果を取得する方法を提供します。 *将来のオブジェクトを介して、タスクの実行ステータスを理解し、タスクの実行をキャンセルし、タスク実行の結果を取得できます。 */public class callableandfuture {/***タスククラスをカスタマイズして呼び出し可能なインターフェイスを実装します*/public static class mycallableclass callable {// flag bit private int flass = 0; public mycallableclass(int flag){this.flag = flag;} public string call()throw {this flag = = 0のif(flag = = 0) 0 ";} if(this.flag == 1){//フラグの値が1の場合、無限ループをtry {while){system.out.println(" looping ... "); sleep(2000);}} catch(interruptedexception e){system.out.out.println(fill in" 1、例外がスローされます。新しい例外( "badフラグ値!");}}} public static void main(string [] args){//コール可能なタイプのタスクmycallableclass task1 = new mycallableclass task2 = new mycallableclass(1); mycallableclass task3 = new mycallablass(1);タスクexecutorservice es = executors.newfixedthreadpool(3); try {//タスクを送信して実行します。タスクが開始されると、将来のオブジェクトが返されます。 //タスクの実行または例外の結果を取得する場合は、将来のオブジェクトを操作できます。 Future Future1 = es.submit(task1); //最初のタスクの結果を取得します。 GETメソッドが呼び出された場合、現在のスレッドは、System.out.println( "estas1:" + future1.get()); future2 = esubmit(task2); // 2番目のタスクを停止する前に5秒間待機します。 2番目のタスクは無限のループスレッドであるためです。Sleep(5000); system.out.println( "task2キャンセル:" + future2.cancel(true)); // 3番目のタスクの出力を取得します。 future3.get());} catch(Exception e){system.out.println(e.tostring());} //タスク実行サービスes.shutdownnow();}}}を停止します上記は、この記事のコンテンツ全体です。必要な場合は、参照してください。