Java—Multithreading (2)

Java programming consists of three parts:

1. JDK system library

JRE: Java Runtime Enviroment (Java Runtime Environment), only for running programs.

JDK: Java Development Kit (Java Development Kit), if program development is required, JDK must be installed.

String, Scanner, packaging class. . .

java.lang.Thread

javax.servlet.Servlet

2. Third-party libraries

Some mature and useful tools provided by non-Java official organizations, C3P0 database connection pool, Spring framework, DBUtils, Dom4J...

github: The world’s largest gay dating site

3. Developer-defined code

Business code written according to specific business requirements.

Use of threads in Java

  • Inherit the Thread class

1. Create a custom class and inherit the Thread class.

2. Rewrite the run method in the Thread class and write the business logic code of the thread.

package com.southwind.test;

public class MyThread extends Thread {
    
    
	
	@Override
	public void run() {
    
    
		// TODO Auto-generated method stub
		//定义业务逻辑
		for(int i = 0;i<10;i++) {
    
    
			System.out.println("-------------MyThread");
		}
	}
	
}

3. Use.

package com.southwind.test;

public class Test {
    
    
	public static void main(String[] args) {
    
    
		//开启两个子线程
		MyThread thread1 = new MyThread();
		MyThread2 thread2 = new MyThread2();
		thread1.start();
		thread2.start();
	}
}

Note: You cannot use the run method to call the task of a thread, because the run method call is equivalent to the execution of an ordinary object, and it will not grab CPU resources.

Only through the start method can the thread be started to seize CPU resources. When a thread seizes the CPU resources, the run method is automatically called.

  • Implement Runnable interface

1. Create a custom class and implement the Runnable interface.

2. Implement the run method and write the business logic code of the thread.

package com.southwind.test;

public class MyRunnable implements Runnable {
    
    

	@Override
	public void run() {
    
    
		// TODO Auto-generated method stub
		for(int i=0;i<1000;i++) {
    
    
			System.out.println("========MyRunnable=======");
		}
	}
	
}

3. Use.

MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
MyRunnable2 runnable2 = new MyRunnable2();
Thread thread2 = new Thread(runnable2);
thread2.start();

Threads and tasks:

Threads preempt CPU resources. Tasks specifically execute business logic. The thread contains a task. The thread starts. When the resources are preempted, the task starts to execute (run).

The difference between the two methods:

1. MyThread, inheriting the Thread class, directly override the run method in the class. When using it, instantiate MyThread directly and start it, because there is Runnable inside Thread.

2. MyRunnbale, the method that implements the Runnable interface, rewrites the run method in the implementation class. When using it, you need to create a Thread object first, and inject MyRunnable into the Thread, Thread.start.

The second method is recommended in actual development.

Online drawing software:

https://www.processon.com/diagrams

Thread status

There are 5 states of thread. Under certain circumstances, the thread can switch between different states. The 5 states are as follows.

  • Creation state: instantiate a new thread object, which has not been started yet.
  • Ready state: The created thread object calls the start method to complete the startup, enters the thread pool and waits for CPU resources.
  • Running status: The thread object has acquired CPU resources and executes tasks within a certain period of time.
  • Blocked state: The running thread suspends the execution of tasks, releases the CPU resources occupied, and can not directly return to the running state after unblocking, but returns to the ready state, waiting for CPU resources.
  • Termination status: the thread has finished running or the thread is terminated due to an exception.

Guess you like

Origin blog.csdn.net/qq_40220309/article/details/107420923