Java multithreading example

Source: Internet
Author: User

Import java. Io .*;

// Multi-thread programming
Public class multithread {
Public static void main (string ARGs []) {
System. Out. println ("I am the main thread! ");
// Create thread instance thread1 below
Threaduseextends thread1 = new threaduseextends ();
// When thread2 is created, the thhreaduserunnable class instance that implements the runnable interface is used as the parameter.
Thread thread2 = new thread (New threaduserunnable (), "secondthread ");
Thread1.start (); // start thread thread1 to make it ready
// Thread1.setpriority (6); // set the priority of thread1 to 6.
// The priority determines who occupies the CPU and starts running the ready thread when the CPU is empty.
// Priority ranges from 1 to 10, min_priority, max_priority, norm_priority ority
// The New thread inherits the parent thread priority created by her. The parent thread usually has the normal priority 5norm_priority.
System. Out. println ("the main thread will be suspended for 7 seconds! ");
Try {
Thread. Sleep (7000); // The main thread is suspended for 7 seconds.
} Catch (interruptedexception e ){
Return;
}
System. Out. println ("the main thread woke up! ");
If (thread1.isalive ()){
Thread1.stop (); // If thread1 still exists, kill it
System. Out. println ("thread1 sleep too long, the main thread killed thread1! ");
} Else
System. Out. println ("thread1 was not found in the main thread, and thread1 woke up and the execution ended! ");
Thread2.start (); // start thread2
System. Out. println ("the main thread will be suspended for another 7 seconds! ");
Try {
Thread. Sleep (7000); // The main thread is suspended for 7 seconds.
} Catch (interruptedexception e ){
Return;
}
System. Out. println ("back to the main thread! ");
If (thread2.isalive ()){
Thread2.stop (); // If thread2 still exists, kill it
System. Out. println ("thread2 sleep too long, the main thread killed thread2! ");
} Else
System. Out. println ("thread2 is not found in the main thread. thread2 is awake and the execution is finished! ");
System. Out. println ("Program End Press any key to continue! ");
Try {
System. In. Read ();
} Catch (ioexception e ){
System. Out. println (E. tostring ());
}

} // Main
} // Multithread

Class threaduseextends extends thread
// Inherit the Thread class and implement its abstract method run ()
// Create an instance of this thread subclass as appropriate to implement the multithreading mechanism
// After a thread is started (that is, it enters the ready state), once the CPU is obtained, its run () method is automatically called.
{

Threaduseextends (){
} // Constructor

Public void run (){
System. Out. println ("thread1 thread started! ");
System. Out. println ("thread1 thread suspended for 10 seconds! ");
Try {
Sleep (10000); // suspends for 10 seconds
} Catch (interruptedexception e ){
Return;
}
System. Out. println ("thread1 is returned to the main thread. Please wait. The main thread has been suspended and may not be awake! ");
// If the run () method is executed in sequence, the thread ends automatically without being killed by the main thread.
// However, if the sleep time is too long, the thread remains alive and may be killed by stop ().
}
}

Class threaduserunnable implements runnable
// Implement the run () method in the runnable interface, and then implement the class of the run () method.
// Create a thread instance for the parameter
{
// Thread thread2 = new thread (this );
// Use this class that implements the run () method in the runnable interface as the parameter to create a thread instance of the thread class
Threaduserunnable (){
} // Constructor

Public void run (){
System. Out. println ("thread2 thread started ");
System. Out. println ("thread2 thread suspended for 1 second! ");
Try {
Thread. Sleep (1000); // suspend for 1 second
} Catch (interruptedexception e ){
Return;
}
System. Out. println ("thread2 returns to the main thread. Please wait. The main thread has been suspended and may not be awake! ");
// If the run () method is executed in sequence, the thread ends automatically without being killed by the main thread.
// However, if the sleep time is too long, the thread remains alive and may be killed by stop ().
}

}

// Modify the sleep time or priority of the program, for example, setpriority ()

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.