Java thread concept Case Study

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.