Guava on the basis of JDK1.5, the extension of the contract, there are some ease of use extension (such as monitor), some are functional perfect (such as listenablefuture), coupled with a number of functional programming features, so that the flexibility of the contract is greatly improved ...
Use of Monitor:
Import Com.google.common.util.concurrent.Monitor;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.concurrent.atomic.AtomicBoolean; /** * Monitor class semantics are the same as synchronized or reentrantlocks, allowing only one thread to enter */public class Monitorsample {private static FINA
l int max_size = 3;
Private Monitor monitor = new monitor ();
Private list<string> List = new arraylist<string> (); Monitor.guard listbelowcapacity = new Monitor.guard (Monitor) {@Override publi
C Boolean issatisfied () {return list.size () < max_size;
}
}; public void Addtolist (String item) throws Interruptedexception {//over max_size, will lock dead//monitor.enterwhen (li
stbelowcapacity);
More than return false will not lock dead Boolean a = Monitor.tryenterif (listbelowcapacity);
try {list.add (item);
} finally {//ensures that the thread will eject the monitor lock Monitor.leave ();
}} public static void Main (string[] args) {monitorsample monitorsample = new Monitorsample ();
for (int count = 0; count < 5; count++) {try {monitorsample.addtolist (count + "");
} catch (Exception e) {System.out.println (e);
}} Iterator Iteratorstringlist = MonitorSample.list.iterator ();
while (Iteratorstringlist.hasnext ()) {System.out.println (Iteratorstringlist.next ()); }
}
}
Future expansion: A recognizable return result, a change in the return result
Package com.wenniuwuren.listenablefuture;
Import java.util.concurrent.Callable;
Import java.util.concurrent.ExecutionException;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.Future;
Import Com.google.common.util.concurrent.FutureCallback;
Import Com.google.common.util.concurrent.Futures;
Import Com.google.common.util.concurrent.ListenableFuture;
Import com.google.common.util.concurrent.MoreExecutors; /** * Before using listenablefuture, it is best to look at the future of the JDK using * * @author wenniuwuren * */public class Listenablefuturetest {Publi c static void Main (string[] args) {//Guava package with function to perform end of listener task execution Executorservice executorservice = Mor
Eexecutors.listeningdecorator (Executors.newfixedthreadpool (5)); Listenablefuture<string> listenablefuture = (listenablefuture<string>) executorService. Submit (New Callable<string> () {public String call () throws Exception {return "task success";
}
});
/* Futrue initial version//JDK comes with thread pool//executorservice executor = Executors.newcachedthreadpool (); JDK future<integer> future = Executor.submit (new callable<integer> () {public Integer call () th
Rows Exception {return 1;
}
});
The JDK future really gets the result where try {Integer count = Future.get ();
} catch (Exception e) {e.printstacktrace (); }*///Listenablefuture End listening Version//compared to the JDK's future wait result, the guava uses the listener to call when the task is completed//But there is a flaw that the final result cannot be processed for operation success/failure, that is, run
The method does not return a value of Listenablefuture.addlistener (new Runnable () {@Override public void run () {System.out.println ("run Complete"); }}, Executorservice); *//run successfully, will return "task success successfully" resolves the listenablefuture End Listener version does not work on the results Futu
Recallbackimpl callback = new Futurecallbackimpl ();
Run//futures.addcallback (Listenablefuture, callback) synchronously with the calculated results; If the calculation is large, the resulting access using async will use the Executorservice thread to execute Futures.addcallback asynchronously (Listenablefuture, callback, EXecutorservice);
System.out.println (Callback.getcallbackresult ()); }} class Futurecallbackimpl implements futurecallback<string> {private StringBuilder builder = new Stringbuil
Der ();
@Override public void onsuccess (String result) {Builder.append (Result). Append ("successfully");
} @Override public void OnFailure (Throwable t) {builder.append (t.tostring ());
} public String GetCallbackResult () {return builder.tostring ();
}
}