Java threads have always been a difficult issue, and many beginners are not very clear about them,
Here, I have used a few small experiments to explain their basic concepts. First, we need to differentiate run () and start,
See why the difference between calling run () directly and starting a thread with START ()
1.
Package com. Dragon;
Import java. Lang. thread;
Public class threadtest extends thread {
Public void run (){
For (INT I = 0; I <10; I ++ ){
System. Out. println ("this is a thread test ");
}
}
Public static void main (string [] ARGs ){
Try {
Thread t = new threadtest ();
T. Run ();
System. Out. println ("main thread is over ");
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}
Output result:
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
Main thread is over
It indicates that the run () method is no different from the call of other methods. The main method executes it in order and prints the last sentence,
Exit the program. Let's change the call to start ()
2.
Package com. Dragon;
Import java. Lang. thread;
Public class threadtest extends thread {
Public void run (){
For (INT I = 0; I <10; I ++ ){
System. Out. println ("this is a thread test ");
}
}
Public static void main (string [] ARGs ){
Try {
Thread t = new threadtest ();
T. Start ();
System. Out. println ("main thread is over ");
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}
Output result:
Main thread is over
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This indicates that the START () method re-creates a thread. After the main method is executed
The thread has not finished running, so the main thread has not exited until thread t has finished running. Note that the default
The thread is a user thread (non-daemon thread), and then change the code.
3.
Package com. Dragon;
Import java. Lang. thread;
Public class threadtest extends thread {
Public void run (){
For (INT I = 0; I <10; I ++ ){
System. Out. println ("this is a thread test ");
}
}
Public static void main (string [] ARGs ){
Try {
Thread t = new threadtest ();
T. setdaemon (true );
T. Start ();
System. Out. println ("main thread is over ");
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}
Output:
Main thread is over
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
Why didn't we print ten sentences, because we set T thread to daemon thread,
Only when the daemon thread exists in the program, the program can exit. Therefore, the program exits after only seven sentences are printed.
Modify the code.
4.
Package com. Dragon;
Import java. Lang. thread;
Public class threadtest extends thread {
Public void run (){
For (INT I = 0; I <100; I ++ ){
System. Out. println ("this is a thread test ");
}
}
Public static void main (string [] ARGs ){
Try {
Thread t = new threadtest ();
T. Start ();
System. Out. println ("main thread is over ");
System. Exit (0 );
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
}
Main thread is over
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
This is a thread Test
The result shows that the user thread can be forced to kill by system. Exit (0), so only seven sentences are printed.