Today, a Java example of the way to return the value of multithreading, found that one of their own is not very understanding of the problem is that in the main thread to start a few worker threads, the main thread is not a join, the worker thread actually output the callback result. This and Linux C + + thread knowledge is not the same, in C + +, if the main function exits, then all the sub-threads are exited, I began to doubt that the book is not carefully written, no test, so I wrote a few classes of simple test. The code is as follows:
Threadcallback.java Code:
Package Thread.callback; Public class Threadcallback { publicvoid callBack (String msg) { System.out.println (msg); }}
Workthread.java Code:
PackageThread.callback; Public classWorkthreadextendsthread{PrivateThreadcallback callback; PrivateString ThreadName; PublicWorkthread (Threadcallback _callback, String _threadname) {//TODO auto-generated Constructor stub This. Callback =_callback; This. ThreadName =_threadname; } @Override Public voidrun () {//TODO auto-generated Method Stub Try{Thread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } This. Callback.callback ( This. ThreadName); } }
Test.java Code:
PackageThread.callback; Public classTest { Public Static voidMain (string[] args) { for(inti=0; i<5; ++i) {Threadcallback callback=NewThreadcallback (); Workthread Workthread=NewWorkthread (Callback, "thread" +integer.tostring (i));Workthread.start (); } System.out.println ("To IS End"); }}
The code is very simple, is workthread in the simple callback callback in the interface, and then print in callback, the result of the operation is as follows:
To be ENDTHREAD0THREAD2THREAD3THREAD1THREAD4
From the output (the thread output order, each time not necessarily the same) can be seen, after the main thread exits, its creation of several sub-threads are still working, until the output of the specified results. Then it is not the same as the master-slave thread under Linux C + +. After searching, it was found that the Java thread has a property of whether it is a daemon thread, by default this property is not set, false, means that the thread is a user thread, if the thread's Setdaemon interface is set, then the threading attribute becomes the daemon thread.
The user thread does not end with the main thread exit, but continues to run in the JVM until it ends, and the JVM waits for all user threads to execute before exiting.
The daemon quits when all user threads (including the main thread) exit, and the JVM exits.
We changed the Test.java to this:
Package Thread.callback;public class Test {public static void main (string[] args) {for (int i=0; i<5; ++i) {THREADCALLB ACK callback = new Threadcallback (); Workthread workthread = new Workthread (callback, "thread" + integer.tostring (i)); Workthread.setdaemon (true); Workthread.start ();} System.out.println ("To Is End");}}
New Setdaemon, set before the thread runs, and then run the program, the program only prints "to is end" and then exits.
What if we set 4 of the 5 child threads to be the daemon thread and one for the user thread?
Test.java Code
Package Thread.callback;public class Test {public static void main (string[] args) {for (int i=0; i<5; ++i) {THREADCALLB ACK callback = new Threadcallback (); Workthread workthread = new Workthread (callback, "thread" + integer.tostring (i)); if (i! = 4) Workthread.setdaemon (true); Workthread.start ();} System.out.println ("To Is End");}}
It is also possible to output all thread results. Why is it possible, it depends on the only user thread thread4 when it quits, and when it exits, other daemons will be forced to quit, whether or not they are executed. In our above example is more difficult to appear, because Thread4 is the last to start, and we all do the same simple thing, if the thread4 changed to Thread1, then it is very easy to test out.
For example, we changed Workthread.java's run to this:
Public voidrun () {//TODO auto-generated Method Stub Try { if(Threadname.equals ("Thread4")) Thread.Sleep (1000); ElseThread.Sleep (3000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } This. Callback.callback ( This. ThreadName); }
Then the output is:
To be ENDTHREAD4
The difference between a user thread and a daemon thread can be seen from the results of the above operation!
Java User threads and daemon threads