What is a process?
There are many separate programs running in the computer. Each program has an independent process, and the processes exist independently of each other. For example, idea,360, qq and so on in the figure below.
Note: a process can contain multiple threads
What is a thread?
Processes that want to perform tasks need to rely on threads. In other words, the smallest execution unit in a process is a thread, and there is at least one thread in a process.
So what is multithreading? When it comes to multithreading, we have to say two concepts: serial and parallel. Only by understanding this can we better understand multithreading.
Serial:
It is to execute tasks according to the execution order. The next task can be executed only after the previous task is executed
Parallel:
Download multiple files, start multiple threads, and download multiple files at the same time. In the strict sense, it occurs at the same time, and parallelism overlaps in time.
Three implementation methods of thread
Method 1: integrate Thread class
1: Custom Thread class integrated Thread class
2: Rewrite the run() method and write the thread entity
3: Create a thread object and call the start() method to start the thread
// Thread creation method 1: inherit the tree class, override the run() method, and call start() to start the thread public class Test01 extends Thread { @Override public void run() { //run method for (int i = 0; i < 200; i++) { System.out.println("run method-- "+i); } } public static void main(String[] args) { //main thread //Create a thread object Test01 thread = new Test01(); thread.start(); for (int i = 0; i < 1000; i++) { System.out.println("main thread -- "+i); } }
Part of the implementation results:
It can be seen that they are parallel.
Note: it may not be executed immediately after startup, but it is scheduled by cpu
- Subclass integration Thread class has Thread function
- Start thread: subclass object. start()
- Not recommended: avoid the limitation of oop single inheritance
Mode 2: implement the Runable interface
//Thread creation mode 2: implement the runnable interface, rewrite the run method, and execute the thread //The implementation class of disgraced runnable interface, call the implementation class of runnable interface and call the start method public class Test02 implements Runnable{ public static void main(String[] args) { //Create an implementation class object for the runnable interface Test02 test02 = new Test02(); //Create a thread object and start our thread through the thread object Thread thread = new Thread(test02); thread.start(); for (int i = 0; i < 1000; i++) { System.out.println("I'm learning multithreading---"+i); } } public void run() { //run method thread body for (int i = 0; i < 200; i++) { System.out.println("I'm looking at the code----" + i); } } }
- The implementation interface Runnable has multithreading capability
- Start Thread: pass in target object + Thread object. satrt()
- Recommended: it avoids the limitation of single inheritance, is flexible and convenient, and is convenient for the same object to be used by multiple threads
Method 3: implement Callable interface
You can understand it online!
Concurrency problem
example:
//Multiple threads operate on an object at the same time //Example of buying a train ticket public class Test03 implements Runnable { //Number of votes private int ticketNums = 10; public void run() { while (true) { if (ticketNums<=0) { break; } //Analog delay try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"-->Got the third "+ ticketNums + "ticket"); //Number of votes-- ticketNums--; } } public static void main(String[] args) { Test03 test03 = new Test03(); new Thread(test03,"Xiao Wang").start(); new Thread(test03,"Xiao Ming").start(); new Thread(test03,"Scalpers").start(); } }
result:
You can see that the same ticket is robbed by two people at the same time. This is the concurrency problem
Extended learning
Lamda expression:
advantage:
- Avoid internal class definitions
- It can make your code look neat
- Removed a pile of meaningless code, leaving only the core logic