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 ("back to the main thread! ");
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 ("I Am a thread sub-class thread instance! ");
System. Out. println ("I will suspend for 10 seconds! ");
System. Out. println ("back to the main thread. Please wait. The main thread has not woken up just now! ");
Try
{
Sleep (10000); // hangs for 5 seconds
}
Catch (interruptedexception E)
{
Return;
}
// 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 ("I Am a Thread class thread instance and take the class that implements the runnable interface as the parameter! ");
System. Out. println ("I will suspend for 1 second! ");
System. Out. println ("back to the main thread. Please wait. The main thread has not woken up just now! ");
Try
{
Thread. Sleep (1000); // suspended for 5 seconds
}
Catch (interruptedexception E)
{
Return;
}
// 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 ()
Comment:
Multi-thread programming is implemented by inheriting the Thread class and implementing the runnable interface! |