1, The concept of multithreading
1. Process: every program being executed is called a process. Process is an independent unit of resource allocation and call. Each process has its own memory space and system resources.
2. Thread: a thread is an execution path of a process. A process with only one execution path is called a single threaded program. If a process has multiple execution paths, it is called a multithreaded program.
2, Implementation mode of multithreading (two kinds)
1. Inherit Thread class and override run() method
Start: create subclass object. start()
- public class MyThread extends Thread {//Inherit Thread class
- @Override
- public void run() {
- for(int i=1;i<101;i++) {
- System.out.println(getName()+"=="+i);
- }
- }
- }
- public class ComeTrue {
- public static void main(String[] args) {
- MyThread t1 =new MyThread(); //Create a subclass object
- MyThread t2 =new MyThread();
- t1.start(); //Start thread
- t2.start();
- }
- }
2. Implement the runnable interface and rewrite the run() method
Starting: using static agents
1) create a real role
2) create an agent role
3). Agent role. start()
- public class MyThread implements Runnable{//Implement the Runnable interface
- @Override
- public void run() {
- for(int i=1;i<101;i++) {
- System.out.println(Thread.currentThread().getName()+"=="+i);
- }
- }
- }
- public class ComeTrue {
- public static void main(String[] args) {
- MyThread my =new MyThread(); //Creating real objects
- Thread t1 = new Thread(my,"Thread one"); //Create proxy object
- Thread t2 = new Thread(my,"Thread two"); //Create proxy object
- t1.start(); //Start thread
- t2.start();
- }
- }
3, Thread life cycle
4, Common methods and thread control
1. Common methods
- public class MyThread implements Runnable{//Implement the Runnable interface
- @Override
- public void run() {
- for(int i=1;i<101;i++) {
- System.out.println(Thread.currentThread().getName()+"=="+i);
- }
- }
- }
- public class ComeTrue {
- public static void main(String[] args) {
- MyThread my =new MyThread(); //Creating real objects
- Thread t1 = new Thread(my,"Thread one"); //Create proxy object
- t1.start(); //Start thread
- /*
- * common method
- */
- t1.isAlive(); //Determine whether the thread is still alive, that is, whether the thread has not been terminated
- t1.setPriority(10); //Set the priority value of the thread to 1-10
- t1.getPriority(); //Get priority value of thread
- t1.setName("Thread one"); //Set the name of the thread
- t1.getName(); //Get the name of the thread
- t1.currentThread(); //Get the currently running thread object, that is, get itself
- }
- }
2. Thread control
- public class MyThread implements Runnable{//Implement the Runnable interface
- @Override
- public void run() {
- for(int i=1;i<101;i++) {
- try {
- Thread.sleep(1000); //Thread sleep time association in thread blocking state
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName()+"=="+i);
- }
- }
- }
- public class ComeTrue {
- public static void main(String[] args) {
- MyThread my =new MyThread(); //Creating real objects
- Thread t1 = new Thread(my,"Thread one"); //Create proxy object
- Thread t2 = new Thread(my,"Thread two"); //Create proxy object
- //Background thread
- //Set background (guard) thread - it needs to be set before the thread starts
- //Daemons: when the last non daemons end, the daemons end their work with the JVM, such as garbage collector
- t1.setDaemon(true);
- t1.start(); //Start thread
- t2.start(); //Start thread
- /*
- * Thread control
- */
- //Thread join
- try {
- t2.join(); //Thread join. Wait until the join thread is set to end, other threads will start running. Other threads are blocked
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- //Thread comity
- t2.yield(); //When t2 grabs the CPU time slice, it gives up once and scrambles again to be blocked
- //stop(): thread termination thread death
- //interrupt(): thread interrupt thread block
- }
- }