Accp8.0 conversion material Chapter 1 multi-thread understanding and practice, accp8.0 chapter 1st
I. Word Section:
① Process ② current ③ thread ④ runnable can obtain
⑤ Interrupt ⑥ join 7⑦ yield to generate synchronous synchronize
Ii. Preview
1. Differences between threads and processes:
Process is the basic unit for running programs in the system.
A thread is the smallest unit of operations executed in a process.
2. describes the two ways to create a thread
① Inherit the thread class
② Implement the Runnable interface
3. The thread lifecycle can be divided into several stages.
Five phases: ① creation ② ready ③ running ④ blocking ⑤ death
4. How can I set the thread sleep, force execution, and courtesy?
Sleep (), join (), yield ()
5. Under what circumstances need thread synchronization, there are several ways to synchronize threads
In case of access conflict
Two Methods: ① synchronous method ② Synchronous Code Block
Iii. Exercises
1. Use the method inheriting the Thread class to create a Thread and display the corresponding content
First, create a Thread class:
Package oneOne;
Public class MyRunnableone extends Thread {
Public void run (){
For (int I = 1; I <= 20; I ++ ){
System. out. println (I + ". Hello, from Thread" + Thread. currentThread (). getName ());
}
}
}
Create the main method class and remove it.
Package oneOne;
Public class testone {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
MyRunnableone my = new MyRunnableone ();
MyRunnableone my1 = new MyRunnableone ();
My. start ();
My1.start ();
}
}
2. Use the Runnable interface to create a thread
The first implementation class is created first:
Package oneTwo;
Public class MyRunnabletwo implements Runnable {
Public void run (){
For (int I = 1; I <= 20; I ++ ){
System. out. println (I + ". Hello, from Thread" + Thread. currentThread (). getName ());
}
}
}
The main method is as follows:
Package oneTwo;
Public class testtwo {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
MyRunnabletwo my = new MyRunnabletwo ();
MyRunnabletwo my1 = new MyRunnabletwo ();
Thread tr = new Thread (my );
Thread tr1 = new Thread (my1 );
Tr. start ();
Tr1.start ();
}
}
3. Use multiple threads to simulate hiking
Create an inheritance or implementation class first (I used inheritance here ):
Package oneThree;
Public class MyRunnablethree extends Thread {
Private int time;
Public int num = 0;
Public MyRunnablethree (String name, int time, int kio ){
Super (name );
This. time = time;
This. num = kio * 1000/100;
}
Public void run (){
While (num> 0 ){
Try {
Thread. sleep (this. time );
} Catch (InterruptedException e ){
// TODO: handle exception
E. printStackTrace ();
}
System. out. println (Thread. currentThread (). getName () + "after crawling 100 meters! ");
Num --;
}
System. out. println (Thread. currentThread (). getName () + "reached the end! ");
}
}
The main method is as follows:
Package oneThree;
Public class testthree {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
MyRunnablethree young = new MyRunnablethree ("young man", 500, 1 );
MyRunnablethree old = new MyRunnablethree ("Elder", 1500, 1 );
MyRunnablethree child = new MyRunnablethree ("child", 600, 1 );
System. out. println *********");
Old. start ();
Young. start ();
Child. start ();
}
}
4. display, set thread priority
Inheritance or implementation class:
Package oneFour;
Public class MyRunnablefour extends Thread {
Public void run (){
Thread. currentThread (). setPriority (1 );
System. out. println ("sub-Thread name:" + Thread. currentThread (). getName () + ", priority:" + Thread. currentThread (). getPriority ());
}
}
Again main:
Package oneFour;
Public class testfour {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
MyRunnablefour myf = new MyRunnablefour ();
Myf. start ();
System. out. println ("************* display default priority ********");
System. out. println ("main Thread name:" + Thread. currentThread (). getName () + ", priority:" + Thread. currentThread (). getPriority ());
Thread. currentThread (). setPriority (10 );
System. out. println ("************ after modifying the default priority ***********");
// Myf. setPriority (1 );
System. out. println ("main Thread name:" + Thread. currentThread (). getName () + ", priority:" + Thread. currentThread (). getPriority ());
// System. out. println ("sub-thread name:" + MyRunnablefour. currentThread (). getName () + ", priority:" + MyRunnablefour. currentThread (). getPriority ());
}
}
5. Simulate Registration
Inherit or implement classes first:
Package oneFive;
Public class MyRunnablefive extends Thread {
Private int time;
// Public int pertail = 0;
Public MyRunnablefive (String common, int time ){
Super (common );
This. time = time;
}
Public void run (){
Thread. currentThread (). setPriority (8 );
For (int I = 1; I <= 10; I ++ ){
Try {
Thread. sleep (this. time );
} Catch (InterruptedException e ){
// TODO: handle exception
E. printStackTrace ();
}
System. out. println ("Special Account:" + I + "patient seeing a doctor! ");
}
}
}
Again main:
Package oneFive;
Public class testfive {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
// MyRunnablefive pertail = new MyRunnablefive ("special user ID", 1000 );
Thread temp = new Thread (new MyRunnablefive ("Special Account", 400 ));
Temp. start ();
Thread. currentThread (). setPriority (4 );
For (int I = 1; I <= 50; I ++ ){
If (I = 11 ){
Try {
Temp. join ();
} Catch (InterruptedException e ){
// TODO: handle exception
E. printStackTrace ();
}
}
Try {
Thread. sleep (200 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println ("normal number:" + I + "patient seeing a doctor ");
}
}
}
6. multi-thread simulated race run
Create an inheritance or implementation class first:
Package oneSix;
Public class runSix implements Runnable {
Private int meters = 1000;
Public runSix (){
}
@ Override
Public void run (){
// TODO Auto-generated method stub
// System. out. println ("in ");
While (true ){
// Type = (type) true. nextElement ();
Synchronized (this ){
If (meters <= 100 ){
Break;
}
System. out. println (Thread. currentThread (). getName () + "got the baton! ");
For (int I = 0; I <100; I + = 10 ){
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println (Thread. currentThread (). getName () + "run" + (I + 10) + "meter! ");
}
Meters-= 100;
}
}
}
}
Next, the main interface class:
Package oneSix;
Public class testsix {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
RunSix ru = new runSix ();
For (int I = 0; I <5; I ++ ){
New Thread (ru, (I + 1) + "contestant"). start ();
}
}
}
7. multi-thread simulated network Ticketing
Taotao run, tickets, scalpers, and so on. They can only get one ticket at a time.
Create an inheritance or implementation class first:
Package oneSeven;
Public class siteSeven implements Runnable {
Private int count = 10;
Private int num = 0;
Private boolean flag = false;
@ Override
Public void run (){
// TODO Auto-generated method stub
// System. out. println ("in ");
While (! Flag ){
Synchronized (this ){
// System. out. println ("in ");
If (count <= 0 ){
Flag = true;
Return;
}
Num ++;
Count --;
Try {
Thread. sleep (500 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
String name = Thread. currentThread (). getName ();
If (name. equals ("scalpers ")){
System. out. println (name + "grab the" + num + "ticket, remaining" + count + "ticket! ");
Break;
}
System. out. println (name + "grab the" + num + "ticket, remaining" + count + "ticket! ");
}
}
}
}
Create another main interface class:
Package oneSeven;
Public class testseven {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
SiteSeven si = new siteSeven ();
Thread per1 = new Thread (si, "dadong ");
Thread yellow = new Thread (si, "scalpers ");
Thread per2 = new Thread (si, "");
Per1.start ();
Yellow. start ();
Per2.start ();
}
}
Iv. Summary:
1. Methods in the Thread class perform operations on the Thread object
① Adjust the thread priority
② Thread sleep ()
③ Force-run join () of a thread ()
④ Thread courtesy yield ()
2. multithreading allows programmers to write efficient programs with the maximum CPU utilization
3. Two ways to create a thread:
① Declare a run () method that inherits the subclass of the Thread class and implements the Thread class
② Declare a class that implements the Runnable interface and then implement the run () method