For Java multithreading, I used to only implement the runnable interface or inherit the Thread class, then rewrite the run () method, and finally start () call is finished, however, once deadlocks are involved, access to shared resources, and the status and execution sequence of threads are monitored at any time, the thread cannot be accessed. Therefore, we will learn multiple threads from the beginning to view the concurrency of thinking in Java,
Next I will send a code about the task return in the thread and the Code for creating the thread pool.
Package COM. bird. thinking; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; /*** @ Use the thread object displayed by Executor * @ author bird **/class liftoff implements runnable {// create a class to implement the runable interface protected int countdown = 10; private Static int taskcount = 0; private final int id = taskcount ++; Public liftoff () {} public liftoff (INT countdown) {This. countdown = Countdown;} Public String Status () {return "#" + ID + "(" + (Countdown> 0? Countdown: "liftoff! "+") ");} Public void run () {While (countdown --> 0) {system. out. println (status (); thread. yield () ;}} public class cachedthreadpool {public static void main (string [] ARGs) {executorservice exec = executors. newcachedthreadpool (); // create a thread pool for (INT I = 0; I <5; I ?#=exec.exe cute (New liftoff (); // work} exec. shutdown ();}}
Executor is the executor, which will manage the thread object for you, thus simplifying concurrent editing.
. Executor provides an indirect layer between the client and the task execution.
The intermediary object will execute the task directly. Executor allows you
Manage the execution of asynchronous tasks without displaying the lifecycle of Management threads.
Executor is the preferred method for starting a task in Java se6/7.
The following code can monitor the status of each thread at any time.
Package COM. bird. thinking; import Java. util. arraylist; import Java. util. concurrent. callable; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; import Java. util. concurrent. future;/*** @ Use creates multiple threads that return values from the task, this interface can be used to determine its status * @ author bird **/class taskwithresult implements callable <string> {// call the call () method private int ID; public taskwithresult (int id) {This. id = ID;} Public String call () {return "result of taskwithresult" + ID;} public class callabledemo {public static void main (string [] ARGs) {executorservice exec = executors. newcachedthreadpool (); arraylist <future <string> Results = new arraylist <future <string> (); For (INT I = 0; I <10; I ++) {results. add (exec. submit (New taskwithresult (I);} For (Future <string> FS: Results) {try {system. out. println (FS. get (); // you can call many methods, including whether or not they work.} catch (exception e) {e. printstacktrace ();} finally {exec. shutdown ();}}}}
The submit method will generate a future object. He uses callable to return a specific type of result
Parameters are set. You can use the isdemo () method to query future ().
. When the task is completed, it has a result. You can call the get () method.
Obtain this result.
The best hibernation Method for Java 6/7 is timeunit. milliseconds. Sleep (100 );
It is best not to use thread. Sleep (100 );