February Home for the New year, home no net, so the blog has stopped for a while, work has been a week, generally still relatively busy! Weekend or not summed up the knowledge points to summarize, easy to read in the future!
Callable and future
Runnable encapsulates a task that runs asynchronously, you can think of it as an asynchronous method without parameters and return values. Callable is similar to runnable, but has a return value. The callable interface is a parameterized type with only one method call.
publicinterface Callable<V>{ V call() throws Exception;}
The type parameter is the type of the return value.
The future saves the results of an asynchronous calculation. You can start a calculation, give the future object to a thread, and then forget it. The owner of the future object can capture it after the result has been calculated.
The future interface has the following methods:
public interface Future<V>{ ...; ...; void calcel(boolean mayInterrupt); boolean isCancelled(); boolean isDone(); }
The call to the first get method is blocked until the calculation is complete. If the call of the second method times out before the calculation is complete, a TimeoutException exception is thrown. If the thread running the calculation is interrupted, two methods will throw a interruptedexception. If the calculation is complete, the Get method returns immediately.
If the calculation is still in progress, the Isdone method returns false, or True if it is completed.
You can cancel the calculation by using the Cancel method. If the calculation has not started, it is canceled and no longer starts. If the calculation is running, it is interrupted if the Mayinterrupt parameter is true.
Futuretask Wrapper is a convenient mechanism to convert callable into future and runnable, which simultaneously implements both interfaces.
We use the future to modify the example in the previous blog post and test it.
Macthcounter class:
/** * @author Xzzhao * * Public class macthcounter implements callable<Integer> { Private FinalFile directory;Private FinalString keyword;Private int Count; PublicMacthcounter (File directory, String keyword) {Super(); This. directory = Directory; This. keyword = keyword; } @Override PublicInteger call () throws Exception {Count=0; file[] files = directory.listfiles (); list<future<integer>> results =NewArraylist<> (); for(File file:files) {if(File.isdirectory ()) {Macthcounter counter =NewMacthcounter (file, keyword); futuretask<integer> task =NewFuturetask<> (counter); Results.add (Task); Thread T =NewThread (Task); T.start (); }Else{if(Search (file)) {Count++; } } } for(future<integer> result:results) {Count+ = Result.get (); }return Count; } Public BooleanSearch (file file) {Try{Try(Scanner in =NewScanner (file)) {BooleanFound =false; while(!found && in.hasnextline ()) {String line = In.nextline ();if(line.contains (keyword)) {found =true; } }returnFound } }Catch(Exception e) {E.printstacktrace (); }return false; }}
Macthcounter class:
/** * @author Xzzhao * * Public class futuretest { Public Static void Main(string[] args) {Scanner in =NewScanner (system.in); System.out.println ("Please enter the root directory:"); String directory = In.nextline (); System.out.println ("Please enter the keyword:"); String keyword = in.nextline (); Macthcounter counter =NewMacthcounter (NewFile (directory), keyword); futuretask<integer> task =NewFuturetask<> (counter); Thread T =NewThread (Task); T.start ();Try{System.out.println ("Number of documents to match:"+ Task.get ()); }Catch(Interruptedexception e) {E.printstacktrace (); }Catch(Executionexception e) {E.printstacktrace (); } }}
Java Multithreading--callable and future