Ava thread return value, sleep, priority, background thread sample code
Package org. rui. thread. basic; import java. util. arrayList; 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;/*** Callable interface application generated from the task * You can also use isDone () to check whether the Future is completed * or you can directly get () check ** @ author lenovo **/class TaskWithResult implements Callable <String> {private int id; public TaskWithResult (int id) {this. id = id ;}@ Overridepublic String call () throws Exception {return "result of TaskWotjResult" + id ;}} public class CallableDemo {// public static void main (String [] args) {ExecutorService exec = Executors. newCachedThreadPool (); ArrayList <Future <String> result = new ArrayList <Future <String> (); for (int I = 0; I <10; I ++) {result. add (exec. submit (new TaskWithResult (I);} for (Future <String> fs: result) try {System. out. println (fs. get ();} catch (InterruptedException e) {e. printStackTrace (); return;} catch (ExecutionException e) {e. printStackTrace ();} finally {exec. shutdown () ;}}/ *** output: result of TaskWotjResult0result of TaskWotjResult1result of TaskWotjResult2result of partition of capacity of TaskWotjResult9 */
Package org. rui. thread. basic; import java. util. concurrent. executorService; import java. util. concurrent. executors;/*** thread priority ** @ author lenovo **/public class SimpleProorities implements Runnable {private int countDown = 5; private volatile double d; // No optimizationprivate int priority; public SimpleProorities (int priority) {this. priority = priority;} public String toString () {return Thread. currentThread () + ":" + CountDown;} @ Overridepublic void run () {Thread. currentThread (). setPriority (priority); // set the priority at the beginning while (true) {for (int I = 1; I <100000; I ++) {// a double value closer to pi (that is, the ratio of circumference to diameter of the circle) than any other value. // Any other value is closer to the double value of e (that is, the base number of the natural logarithm. D + = (Math. PI + Math. e)/(double) I; if (I % 1000 = 0) Thread. yield ();} System. out. println (this); if (-- countDown = 0) return ;}} public static void main (String [] args) {ExecutorService exec = Executors. newCachedThreadPool (); for (int I = 0; I <5? I ===exec.exe cute (new simpleproorities(thread.min_priority={exec.exe cute (new SimpleProorities (Thread. MAX_PRIORITY); // high priority exec. shutdown ();}}
Package org. rui. thread. basic; import java. util. concurrent. timeUnit; /*** background thread ** refers to the thread that provides a common service in the background when the program is running * @ author lenovo **/public class SimpleDeamons implements Runnable {@ Overridepublic void run () {try {while (true) {TimeUnit. MILLISECONDS. sleep (1, 100); System. out. println (Thread. currentThread () + ">>>" + this) ;}} catch (InterruptedException e) {e. printStackTrace () ;}} public static void main (String [] args) {for (int I = 0; I <10; I ++) {Thread daemon = new Thread (new SimpleDeamons (); daemon. setDaemon (true); // set it to the background thread daemon. start ();} System. out. println ("all daemons started ------"); try {// try to adjust the sleep time to observe this behavior TimeUnit. MILLISECONDS. sleep (175);} catch (InterruptedException e) {e. printStackTrace () ;}}/ *** output: all daemons started ------ Thread [Thread-8, 5, main] >>> org. rui. thread. basic. simpleDeamons @ 1e4f7c2Thread [Thread-9, 5, main] >>> org. rui. thread. basic. simpleDeamons @ 10dc6b5Thread [Thread-4, 5, main] >>> org. rui. thread. basic. simpleDeamons @ 1d0fafcThread [Thread-6, 5, main] >>> org. rui. thread. basic. simpleDeamons @ c9d92cThread [Thread-0, 5, main] >>> org. rui. thread. basic. simpleDeamons @ 145f0e3Thread [Thread-2, 5, main] >>> org. rui. thread. basic. simpleDeamons @ 1e4f7c2Thread [Thread-5, 5, main] >>> org. rui. thread. basic. simpleDeamons @ b8f8ebThread [Thread-1, 5, main] >>> org. rui. thread. basic. simpleDeamons @ d0af9bThread [Thread-7, 5, main] >>> org. rui. thread. basic. simpleDeamons @ f471_thread [Thread-3, 5, main] >>> org. rui. thread. basic. simpleDeamons @ 170bea5 */
Package org. rui. thread. basic; import java. util. concurrent. threadFactory;/*** you can customize the attributes (backend, priority, and name) of a thread created by Executor by writing a custom threadFactory) * @ author lenovo **/public class DaemonThreadFactory implements ThreadFactory {@ Overridepublic Thread newThread (Runnable r) {Thread thread = new Thread (r); thread. setDaemon (true); return thread ;}}
Package org. rui. thread. basic; import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. timeUnit;/*** the only difference between this and general ThreadFactory is that it sets all background States to true * now you can use a new DaemonThreadFactory as a parameter to pass to executory. newCachedThreadPool () * @ author lenovo **/public class DeamonFromFactory implements Runnable {@ Overridepublic void run () {try {while (true) {TimeUnit. MILLISECONDS. sleep (1, 100); System. out. println (Thread. currentThread () + ">>" + this) ;}} catch (Exception e) {}/// public static void main (String [] args) throws InterruptedException {// every ExecutorService creation method is overloaded to accept a threadfactory object // and this object will be used to create a new thread ExecutorService exec = Executors. newCachedThreadPool (new DaemonThreadFactory (); for (int I = 0; I <1010000i00000000000000000000exec.exe cute (new DeamonFromFactory ();} System. out. println ("all daemons started"); TimeUnit. MILLISECONDS. sleep (500) ;}}/** output: all daemons startedThread [Thread-0, 5, main]> org. rui. thread. basic. deamonFromFactory @ 8ab708Thread [Thread-3, 5, main]> org. rui. thread. basic. deamonFromFactory @ 9934d4Thread [Thread-5, 5, main]> org. rui. thread. basic. deamonFromFactory @ f6ac0bThread [Thread-4, 5, main]> org. rui. thread. basic. deamonFromFactory @ 1c0f2e5Thread [Thread-2, 5, main]> org. rui. thread. basic. deamonFromFactory @ 13c71_thread [Thread-1, 5, main]> org. rui. thread. basic. deamonFromFactory @ 54c4adThread [Thread-6, 5, main]> org. rui. thread. basic. deamonFromFactory @ 5b8827Thread [Thread-7, 5, main]> org. rui. thread. basic. deamonFromFactory @ d09ad3Thread [Thread-8, 5, main]> org. rui. thread. basic. deamonFromFactory @ 147c1dbThread [Thread-9, 5, main]> org. rui. thread. basic. deamonFromFactory @ 82d37Thread [Thread-0, 5, main]> org. rui. thread. basic. deamonFromFactory @ 8ab708Thread [Thread-5, 5, main]> org. rui. thread. basic. deamonFromFactory @ f6ac0bThread [Thread-3, 5, main]> org. rui. thread. basic. deamonFromFactory @ 9934d4Thread [Thread-1, 5, main]> org. rui. thread. basic. deamonFromFactory @ 54c4adThread [Thread-4, 5, main]> org. rui. thread. basic. deamonFromFactory @ 1c0f2e5Thread [Thread-2, 5, main]> org. rui. thread. basic. deamonFromFactory @ 13c71_thread [Thread-6, 5, main]> org. rui. thread. basic. deamonFromFactory @ 5b8827Thread [Thread-8, 5, main]> org. rui. thread. basic. deamonFromFactory @ 147c1dbThread [Thread-7, 5, main]> org. rui. thread. basic. deamonFromFactory @ d09ad3Thread [Thread-9, 5, main]> org. rui. thread. basic. deamonFromFactory @ 82d37Thread [Thread-5, 5, main]> org. rui. thread. basic. deamonFromFactory @ f6ac0bThread [Thread-1, 5, main]> org. rui. thread. basic. deamonFromFactory @ 54c4adThread [Thread-0, 5, main]> org. rui. thread. basic. deamonFromFactory @ 8ab708Thread [Thread-3, 5, main]> org. rui. thread. basic. deamonFromFactory @ 9934d4Thread [Thread-7, 5, main]> org. rui. thread. basic. deamonFromFactory @ d09ad3Thread [Thread-4, 5, main]> org. rui. thread. basic. deamonFromFactory @ 1c0f2e5Thread [Thread-2, 5, main]> org. rui. thread. basic. deamonFromFactory @ 13c71_thread [Thread-6, 5, main]> org. rui. thread. basic. deamonFromFactory @ 5b8827Thread [Thread-8, 5, main]> org. rui. thread. basic. deamonFromFactory @ 147c1dbThread [Thread-9, 5, main]> org. rui. thread. basic. deamonFromFactory @ 82d37Thread [Thread-1, 5, main]> org. rui. thread. basic. deamonFromFactory @ 54c4adThread [Thread-5, 5, main]> org. rui. thread. basic. deamonFromFactory @ f6ac0bThread [Thread-3, 5, main]> org. rui. thread. basic. deamonFromFactory @ 9934d4Thread [Thread-0, 5, main]> org. rui. thread. basic. deamonFromFactory @ 8ab708Thread [Thread-8, 5, main]> org. rui. thread. basic. deamonFromFactory @ 147c1dbThread [Thread-6, 5, main]> org. rui. thread. basic. deamonFromFactory @ 5b8827Thread [Thread-2, 5, main]> org. rui. thread. basic. deamonFromFactory @ 13c71_thread [Thread-4, 5, main]> org. rui. thread. basic. deamonFromFactory @ 1c0f2e5Thread [Thread-7, 5, main]> org. rui. thread. basic. deamonFromFactory @ d09ad3Thread [Thread-9, 5, main]> org. rui. thread. basic. deamonFromFactory @ 82d37Thread [Thread-0, 5, main]> org. rui. thread. basic. deamonFromFactory @ 8ab708Thread [Thread-3, 5, main]> org. rui. thread. basic. deamonFromFactory @ 9934d4Thread [Thread-1, 5, main]> org. rui. thread. basic. deamonFromFactory @ 54c4adThread [Thread-5, 5, main]> org. rui. thread. basic. deamonFromFactory @ f6ac0bThread [Thread-8, 5, main]> org. rui. thread. basic. deamonFromFactory @ 147c1dbThread [Thread-6, 5, main]> org. rui. thread. basic. deamonFromFactory @ 5b8827Thread [Thread-2, 5, main]> org. rui. thread. basic. deamonFromFactory @ 13c71_thread [Thread-4, 5, main]> org. rui. thread. basic. deamonFromFactory @ 1c0f2e5Thread [Thread-7, 5, main]> org. rui. thread. basic. deamonFromFactory @ d09ad3 */
Package org. rui. thread. basic; import java. util. concurrent. blockingQueue; import java. util. concurrent. synchronousQueue; import java. util. concurrent. threadPoolExecutor; import java. util. concurrent. timeUnit;/*** each static executorService creation method is overloaded to accept a threadfactory object, * This object will be used to create a new thread * @ author lenovo **/public class DaemonThreadPoolExecutor extends ThreadPoolExecutor {public DaemonThreadPoolExecutor () {super (0, Integer. MAX_VALUE, 60L, TimeUnit. SECONDS, new SynchronousQueue <Runnable> (), new DaemonThreadFactory ());}}
Package org. rui. thread. basic; import java. util. concurrent. TimeUnit;/*** you can call the isDaemon method to determine whether a thread is a background thread. If it is a background thread, * any thread created will be automatically set as a background thread, as shown in the following example, * @ author lenovo **/class Daemon implements Runnable {private Thread [] t = new Thread [10]; @ Overridepublic void run () {for (int I = 0; I <t. length; I ++) {t [I] = new Thread (new DaemonSpawn (); t [I]. start (); System. out. println ("daemonSpawn" + I + "started,") ;}for (int I = 0; I <t. length; I ++) {System. out. println ("t [" + I + "]. isDaemon () = "+ t [I]. isDaemon () + ",") ;}/ *** is part of the java thread mechanism of the thread scheduler, A suggestion for transferring a cpu from one thread to another * It is declared that I have completed the most important part of the lifecycle, at this moment, it is a good time to switch to other tasks for a period of time * completely optional, **/while (true) Thread. yield (); // give control to other threads} class DaemonSpawn implements Runnable {@ Overridepublic void run () {while (true) Thread. yield ();}} /// ///public class Daemons {public static void main (String [] args) throws InterruptedException {Thread d = new Thread (new Daemon (); d. setDaemon (true); d. start (); System. out. println ("d. isDaemon () = "+ d. isDaemon () + ","); TimeUnit. MILLISECONDS. sleep (1) ;}}/** d. isDaemon () = true, generation 0 started, daemonSpawn 1 started, daemonSpawn 2 started, daemonSpawn 3 started, Generation 4 started, Generation 5 started, daemonSpawn 6 started, generation 7 started, daemonSpawn 8 started, daemonSpawn 9 started, t [0]. isDaemon () = true, t [1]. isDaemon () = true, t [2]. isDaemon () = true, t [3]. isDaemon () = true, t [4]. isDaemon () = true, t [5]. isDaemon () = true, t [6]. isDaemon () = true, t [7]. isDaemon () = true, t [8]. isDaemon () = true, t [9]. isDaemon () = true ,*/
Package org. rui. thread. basic; import java. util. concurrent. timeUnit;/*** you should realize that the background process will terminate its run () without executing the finally clause () method * @ author lenovo **/class ADeamon implements Runnable {@ Overridepublic void run () {try {System. out. println ("starting adaemon"); TimeUnit. MILLISECONDS. sleep (1);} catch (InterruptedException e) {System. out. println ("exiting InterruptedException");} finally {// which should be run? System. out. println ("this shoshould always run? ") ;}} Public class DaemonDontRunFinally {// you will see that the finally clause will not be executed if the annotation t. setDaemon (true); public static void main (String [] args) {Thread t = new Thread (new ADeamon (); t. setDaemon (true); t. start (); // The background thread will be immediately closed once main exits jvm}/** output: starting adaemon */