I. Two ways to create a thread
1 inherit the Runnable interface
Public class ThreadImpRunnable implements Runnable
{
/**
* Method of execution during thread running
*/
Public void run ()
{
For (int I = 0; I <500; I ++)
{
System. out. println (Thread. currentThread (). getName () + I );
}
}
}
Public class Test
{
/**
* The main thread. The start thread must be the start method.
*/
Public static void main (String [] args)
{
ThreadImpRunnable tr = new ThreadImpRunnable ();
Thread t = new Thread (tr );
T. start ();
For (int I = 0; I <500; I ++)
{
System. out. println (Thread. currentThread (). getName () + I );
}
}
}
2. inherit the Thread class
Public class ThreadExtends extends Thread
{
/**
* Method of execution during thread running
*/
Public void run ()
{
For (int I = 0; I <500; I ++)
{
System. out. println (Thread. currentThread (). getName () + I );
}
}
}
Public class Test
{
/**
* The main thread. The start thread must be the start method.
*/
Public static void main (String [] args)
{
ThreadExtends tr = new ThreadExtends ();
Tr. start ();
For (int I = 0; I <500; I ++)
{
System. out. println (Thread. currentThread (). getName () + I );
}
}
}
Ii. Thread Methods
1 sleep
Public class SleepThread implements Runnable
{
/**
* Method of execution during thread running
*/
Public void run ()
{
Try
{
// This thread enters the blocking status for 5 seconds
Thread. sleep (5000 );
For (int I = 0; I <500; I ++)
{
System. out. println (Thread. currentThread (). getName () + I );
}
}
Catch (InterruptedException e)
{
// If the thread's interrupt method is called, this exception is reported. Some resources can be closed in real programs.
E. printStackTrace ();
}
}
}
Public class SleepTest
{
/**
* Main thread
*/
Public static void main (String [] args)
{
SleepThread tr = new SleepThread ();
Thread t = new Thread (tr );
T. start ();
For (int I = 0; I <500; I ++)
{
System. out. println (Thread. currentThread (). getName () + I );
}
}
}
2 Join
Public class JoinThread implements Runnable
{
Public void run ()
{
For (int I = 0; I <100; I ++)
{
System. out. println (Thread. currentThread (). getName () + I );
}
}
}
Public class JoinTest
{
Public static void main (String [] args)
{
JoinThread jt = new JoinThread ();
Thread t = new Thread (jt );
T. start ();
Try
{
// Call this method to merge the current thread (the main thread here) into the current thread. After this thread is executed, execute the current thread.
T. join ();
}
Catch (InterruptedException e)
{
E. printStackTrace ();
}
For (int I = 0; I <100; I ++)
{
System. out. println (Thread. currentThread (). getName () + I );
}
}
}
3 yield
Public class YieldThread extends Thread
{
Public void run ()
{
For (int I = 0; I <100; I ++)
{
System. out. println (Thread. currentThread (). getName () + I );
If (I % 10 = 0)
{
// When the code can be divisible by 10, this thread gives priority and allows other threads to execute the code for a while. You can see the result of t1_10 followed by t2, that is, the result of t2_20 followed by t1.
Yield ();
}
}
}
}
Public class YieldTest
{
Public static void main (String [] args)
{
YieldThread yt = new YieldThread ();
Thread t1 = new Thread (yt, "t1 _");
T1.start ();
Thread t2 = new Thread (yt, "t2 _");
T2.start ();
}
}
4 setPriority
Public class PriorityThread extends Thread
{
Public void run ()
{
For (int I = 0; I <100; I ++)
{
System. out. println (Thread. currentThread (). getName () + I );
}
}
}
Public class PriorityTest
{
/**
* The thread priority is 5 by default.
*/
Public static void main (String [] args)
{
Int norm = Thread. NORM_PRIORITY; // 5
Int max = Thread. MAX_PRIORITY; // 10
Int min = Thread. MIN_PRIORITY; // 1
PriorityThread yt = new PriorityThread ();
Thread t1 = new Thread (yt, "t1 _");
T1.setPriority (norm + 3 );
T1.start ();
Thread t2 = new Thread (yt, "t2 _");
T2.start ();
}
}