Two types of thread implementation and thread alternate execution, two types of thread alternate execution
I learned a lot about the thread. record it.
I. Two main implementation methods of threads.
1. inherit the Thread class and override the run () method.
Create a subclass in the main method and call the start () method.
Example:
// Inherit the Thread class and override the run () method
Public class ThreadOne extends Thread {
Public void run (){
For (int I = 1; I <= 100; I ++ ){
System. out. println (this. getName () + ":" + I );
}
}
Public static void main (String [] args ){
// Create a Thread object
ThreadOne threadOne = new ThreadOne ();
// Call start ()
ThreadOne. start ();
}
}
2. Implement the Runnable () interface and run () method.
Instance class in the mian method, instance Thread class, and hold the reference of this class to call start ().
Instance:
// Implement the Runnable () interface and run () method
Public class MyThread implements Runnable {
Public void run (){
For (int I = 0; I <10; I ++ ){
System. out. println (Thread. currentThread (). getName () + "put in an apple:" + I );
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
Throw new RuntimeException (e );
}
}
}
Public static void main (String [] args ){
// Create a MyThread class
MyThread myThread = new MyThread ();
// Create a Thread and hold the MyThread Application
Thread thread = new Thread (myThread );
// Call start ()
Thread. start ();
}
}
Ii. Threads with alternating rules
For example, the problem is that two threads can print at intervals of one second, and each thread prints at intervals of five times and ends the thread.
Result requirements:
Thread-0 Sun Feb 28 11:21:52 CST 2016;
Thread-1 Sun Feb 28 11:21:53 CST 2016
Thread-0 sunshine Feb 28 11:21:54 CST 2016
Thread-1 Sun Feb 28 11:21:55 CST 2016
Thread-0 sunshine Feb 28 11:21:56 CST 2016
Thread-1 Sun Feb 28 11:21:57 CST 2016
Thread-0 sunshine Feb 28 11:21:58 CST 2016
Thread-1 Sun Feb 28 11:21:59 CST 2016
Thread-0 sunshine Feb 28 11:22:00 CST 2016
Thread-1 Sun Feb 28 11:22:01 CST 2016
Problem Analysis:
First, two threads must be implemented: the function is created at the same time, and the synchronous execution is required to lock synchronized. These are understandable.
The important thing is how to control the lock and how to use it. The method is to define a static state value state with a value of 1, 2;
When the state is 1, the execution thread 1 and thread 2 are waiting. After the execution ends, the state is 2 and the yyall () or Y () method is called;
When the state is 2, the execution thread 2 and thread 1 wait. After the execution ends, state = 1 and call the yyall () or Y () method;
At last, there are five control issues left. Two static int variables are defined respectively. The while control is used, and each thread executes the variable once. The corresponding variable value is added to 5 and ends.
Code:
Public class ThreadDate {
// Status value controls the program to be executed
Private static int state = 1;
// Number of times the thread 1 is executed
Private static int num1 = 1;
// Number of times the control thread 2 executes
Private static int num2 = 1;
Public static void main (String [] args ){
// Use this class as the lock
Final ThreadDate t = new ThreadDate ();
// Create the first thread and call the start () method
New Thread (new Runnable (){
Public void run (){
// Use while num1 to control the number of executions
While (num1 <= 5 ){
// Lock
Synchronized (t ){
// Check whether the execution is performed based on the status value.
If (state! = 1 ){
Try {
T. wait ();
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
System. out. println (Thread. currentThread (). getName () + "" + new Date ());
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// The execution ends and the status value changes.
State = 2;
T. policyall ();
}
Num1 ++;
}
}
}). Start ();;
// Create the second thread and call the start () method
New Thread (new Runnable (){
Public void run (){
While (num2 <= 5 ){
Synchronized (t ){
If (state! = 2 ){
Try {
T. wait ();
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
System. out. println (Thread. currentThread (). getName () + "" + new Date ());
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
State = 1;
T. policyall ();
}
Num2 ++;
}
}
}). Start ();
}
}