The Java thread is implemented by thread, usually we create a thread to make a complex operation, then process the calculation result in the main thread, but the Run function of Thread does not return value, then we operate, how to notify Other threads, this paper tells several ways to return information. One. There are two ways to create a thread, one is to inherit the Thread class, and the other is to implement RunNable, then pass it to a Thread constructor, instance, now we want to calculate 1 to 10,000 in a thread ): 1. Inherited Thread: Public Class Addthread Extends thread {public void Run () {int result = 0; for (int i = 1; i <= 10000; i ) {result = i;}}} Run addthread: addthread thread = New ADDTHREAD (); thread.start (); 2. Implementation Interface Runnable: Public Class Add Implements Runnable {PUBLIC VOID RUN () {Int Result = 0; for (INT I = 1; I <= 10000; I ) {Result = I;}}} Running the thread: Thread Thread = New THREAD (New Add ()); thread.start (); Second, the method of returning the calculation result Now we start this addition thread, you need to get the result of the calculation from this thread, for example, we have to operate in the main thread The result is shown. So how do we achieve it? Below, there are several common methods, pay attention to some of which is wrong. Using a GET method (wrong) we can join a getResult function in Addthread to get the results: public class addthread extends thread {private int result = 0; public void run () {for (int i = 0; i <= 10000; i ) Result = i;} public int getResult () {return result;}} / ** Get an operation result and display on the screen * / public class test {public static void main (String [] args) {addthread thread = New addthread (); thread.start (); system.out.println ("result is" thread.getResult ());}} The result is: Result is 0 Because the main thread and addthread thread run at the same time, The operation of AddThread has not been completed (or even not yet started), the main thread starts the output operation result, so this approach is wrong. 2. The second method is the use of a variable HasDone to indicate whether the operation is completed if the Hasdone is not completed, otherwise the operation has been completed.
The main thread constantly queries this variable, if the operation has not been completed, then enter the loop wait, otherwise the output operation result is output. Public class addthread extends thread {private int result = 0; private boolean Hasdone = false; public void run () {for (int i = 0; i <= 10000; i ) Result = i; Hasdone = true;} public boolean hasDone () {return hasDone;} public int getResult () {return result;}} public class Test {public static void main (String [] args) {AddThread thread = new AddThread (); thread.start (); file: //Such as
If the cost is not completed, the loop waits for while (! Thread.hasdone ()) {TRY {thread.sleep (100);} catch (interruptedException ex)}} f (thread.hasdone ()) System.out.println "Result is" thread.getResult ());}} The result is displayed: Result IS 50005000 The state of the loop query operation in the main thread, if the operation is not completed, then the main thread sleep100 milliseconds, then continue to query, this method is feasible, However, due to the main thread cycle query, a large amount of CPU is consumed, so the efficiency is very low. 3. Wait / Notify mode (better) The third method uses Wait / Notify form, when the operation is not over, the main thread enters the sleep state, then it does not take up the CPU, so the efficiency is higher. Public class addthread extends thread {
File: //
This Object is on Wait Private Object Lock; Private Int Result = 0; Private Boolean Hasdone = False; Public Addthread (Object Lock) {this.lock = LOCK;} public void Run () {for (int i = 0; i <= 1000; i ) Result = i;
File: //
End, notify the waiting thread synchronized (Lock) {Hasdone = true; Lock.NotifyAll ();}} public boolean Hasdone ()} public boolean;} public int getResult () {Return Result;}}
file: // main thread public class Test {public static void main (String [] args) {Object lock = new Object (); AddThread thread = new AddThread (lock); thread.start (); synchronized (lock) {while (! thread.hasdone ()) {TRY {
File: //
The calculation did not end, the main thread entered the sleep state, wake-up the main thread lock.wait () when addthread executed NotifyAll (});}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} (thread.hasdone ()) system.out.println "Result is" thread.getResult ());}} 4. I think this is the best way using Callback. When the operation is completed, the AddThread automatically calls the result processing class. The extension can be processed to make multiple Listener processes the results. This method is used, which is simple. Do not consider the synchronization mechanism, the implementation is as follows:
File: //
Results Interface public interface ResultProcessor processed {public void process (int result);} public class AddThread extends Thread {private ResultProcessor processor; public AddThread (ResultProcessor processor) {this.processor = processor;} public void run () {int result = 0; for (int i = 0; i <= 10000; i ) {result = i;}
File: //
Processing the results processor.process (result);}} public class Test implements ResultProcessor {public void process (int result) {System.out.println ( "result is" result);} public static void main (String [] args ) {Test Test = new test (); addthread thread = new addthread (test); thread.start ();}}} results show: Result IS 50005000 code, as above, the constructor of AddThread passes a result processing class, as an operation When completing, the process function of this class is automatically called. Compared, I think this method is best.