Java thread understanding

Source: Internet
Author: User

 

// There are two methods to understand the thread. One is to directly use the new thread. You can imagine why the start method is dropped.
// Can I run the run method? You can refer to the source code of thread.
/**
* Causes this thread to begin execution; the Java Virtual Machine
* Callthe <code> RUN </code> method of this thread.
* <P>
* The result is that two threads are running concurrently:
* Current thread (which returns from the call to
* <Code> Start </code> method) and the other thread (which executes its
* <Code> RUN </code> method ).
* <P>
* It is never legal to start a thread more than once.
* In particle, a thread may not be restarted once it has completed
* Execution.
*
* @ Exception illegalthreadstateexception if the thread was already
* Started.
* @ See # Run ()
* @ See # Stop ()
*/
Public synchronized void start (){
/**
* This method is not invoked for the main method thread or "system"
* Group threads created/set up by the VM. Any new functionality added
* To this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "new ".
*/
If (threadstatus! = 0)
Throw new illegalthreadstateexception ();
Group. Add (this );
Start0 ();
If (stopbeforestart ){
Stop0 (throwablefromstop );
}
}
The method is quite clear. the Java Virtual Machine calls the run method of this thread.
The result is that two threads run concurrently. The current thread (returned from the call to the start method) and the other thread (executing its run method ).
It is invalid to start a thread multiple times. In particular, the thread cannot be restarted after it has completed execution.
Eg:
// Thread = new thread (){
// @ Override
// Public void run (){
// While (true ){
// Try {
// Thread. Sleep (500 );
//} Catch (interruptedexception e ){
// E. printstacktrace ();
//}
//}
//}
//};
// Thread. Start ();
//

// This is the second method. You can see the second construction method of thread. java.
/**
* If this thread was constructed using a separate
* <Code> runnable </code> run object, then that
* <Code> runnable </code> object's <code> RUN </code> method is called;
* Otherwise, this method does nothing and returns.
* <P>
* Subclasses of <code> thread </code> shocould override this method.
*
* @ See # Start ()
* @ See # Stop ()
* @ See # thread (threadgroup, runnable, string)
*/
Public void run (){
If (target! = NULL ){
Target. Run ();
}
}
We can see that when targer is not equal to null, we execute the run method of target. This target is input during construction
Runnable object. Yes. below is the second thread method.
// Thread thread2 = new thread (New runnable (){
// @ Override
// Public void run (){
// While (true ){
// Try {
// Thread. Sleep (500 );
//} Catch (interruptedexception e ){
// E. printstacktrace ();
//}
//}
//
//}
//});
// Thread2.start ();

Think about the following code: What is printed? Is it runnable or thread? The answer is yes. Promise thread.
Because New thread () represents the sub-class of thread, and we overwrite the run method of the parent class, when
When the start method is used, we look for the Child class. If run is found, we don't look for the parent class. When the subclass does not exist,
Then, judge whether the target is null in the parent class, that is, the runnable run method,

Here, let's think about why we use the second thread to write code? What are the benefits of him?
In fact, the advantage is not mentioned. It is just said that the second thread method can better reflect Java's Object-oriented thinking. Constructor
The input parameters are also objects, and their structure is well encapsulated.
New thread (
New runnable (){
Public void run (){
While (true ){
Try {
Thread. Sleep (500 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
System. Out. println ("runnable ");

}
}
}
){
Public void run (){
While (true ){
Try {
Thread. Sleep (500 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
System. Out. println ("Thread ");

}
}
}. Start ();


}

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.