1. How threads are created : The following ways to create threads , Are essentially the same , With the help of Thread class , Create a new PCB, Add to the bidirectional linked list in the kernel , It's just that the subject describing the task is different ;

runnable Literally , Used to describe a task ; Body of thread

(1) By showing inheritance thread Class , Or anonymous inner classes ;
Thread t1=new Thread(){ @Override public void run() { System.out.println(1); }
}; t1.start();
(2) Create a class by displaying , realization Runnable Interface , And then inherit this runnable The instantiated object of is associated to Thread On instances of
public class Hello { static class myrunnable implements Runnable { public
void run() { System.out.println(" I am a thread "); } } public static void main(String[]
args) throws InterruptedException{ Thread t1=new Thread(new myrunnable());
t1.start(); }}
3
adopt runnable Anonymous Inner Class ( An inner class without anonymity , All you need to do is create an instance , This requires this instance , No more instances , This way of writing and creating a Thread class , Re inheritance Thread No difference ) To create a thread , rewrite run method , Just put this Runnable Class to associate Thread inside
public class Hello { public static void main(String[] args) throws
InterruptedException{ Runnable myrunnable=new Runnable(){ public void run() {
System.out.println(" I am a thread "); } }; Thread t1=new Thread(myrunnable);
t1.start(); }} // Thread thread =new Thread(new Runnable() { @Override public
void run() { System.out.println(" I am a thread "); } }); thread.start(); }
4 adopt lamda To create a thread
public class Hello { public static void main(String[] args) throws
InterruptedException{ Thread t1=new
Thread(()->{System.out.println(" I am a thread ");}); t1.start(); Functional interface }}
Interview questions :thread In class run And start Differences in methods

answer :

When we click on the program to run , First, the system will create a process , Code executed by this thread , namely main method , There are many in the system PCB, Execute code , Will create a ·PCB, Through linked list and other PCB Connect , Now this PCB It represents main method ;
But at this point t.start(), Will create one again PCB, implement t.start() When , Will put this PCB Hang it on the linked list ( these two items. PID Is the same , At this point PCB It will automatically call us run() method ;
t.run(), The thread that appears will not be created , A PCB, this PCB Yes on behalf of main function , In this PCB This will be called in main method , This process does not create new threads

call start Method can start the thread directly , And make the thread ready , When run Method execution completed , thread , It's over . But if you execute directly run method , Will be called as a normal method , A new thread will not be created ;

2. To understand multithreading and join Usage of , Let's write some code

1. Let's first look at the execution effect of a single thread

notes :sleep Is to let the thread give up temporarily CPU
public class Hello { public static void main(String[] args){ long
beg1=System.currentTimeMillis(); int a=0; for(long i=0;i<1000000000;i++) { a++;
} int b=0; for(int j=0;j<1000000000;j++) { b++; } long
beg2=System.currentTimeMillis(); System.out.println(" Execution time is ");
System.out.println(beg2-beg1); }}
576

2 Look at the execution effect of multithreading
class Hello{ public static void main(String[] args) throws
InterruptedException{ long beg1=System.currentTimeMillis(); Thread t1=new
Thread(){ public void run(){ long a=0; for(long i=0;i<1000000000;i++) { a++; }
} }; Thread t2=new Thread(){ public void run(){ long b=0; for(long
j=0;j<1000000000;j++) { b++; } } }; t1.start(); t2.start(); t1.join();
t2.join(); long beg2=System.currentTimeMillis(); System.out.println(" Execution time is ");
System.out.println(beg2-beg1); } }
    374

From this we can see that : Multithreading is more efficient than single threading

join Function is : Let thread 1 And thread 2. Execute the main thread after the execution is completed ( No join before , thread 1, thread 2, Is executed simultaneously with the main thread ); in other words
At this time join Is to block the main thread ,t1,t2 Is executed concurrently

But if so
t1.start(); t1.join(); t2.start(); t2.join();
At this time  t1,t2 Is executed serially .

3. Additional contents :

1, The main thread is still going down , But the new thread will execute RUN method , For the main thread ,run Method execution completed , The new thread ends , For the main thread ,main Method execution completed , The main thread ends

2, Between threads , Is a relationship of concurrent execution , Who executes first , Who will execute after , Who executes and where gives up CPU, It's all uncertain , As a programmer, I can't feel it , Fully responsible for the kernel of the operating system . For example, when creating a new thread , Next, the main thread executes first , New thread , It's not guaranteed .

3. implement join Method , The thread will always block , Block until the corresponding thread ends , Will continue , In essence, it is to control the sequence of thread execution , And for sleep Say , Whoever calls will block ;

4, The main thread divides the task into several parts , Each thread calculates its own task , When all the tasks have been calculated , The main thread will summarize again ( You must ensure that the main thread is the last thread to execute ).

5 Get a reference to the current object Thread.currentThread()

6
If the thread is running , Perform calculation of its logic , At this point, it is sorting in the ready queue , The scheduler will find the appropriate one in the ready queue PCB Let him in CPU implement , If a thread calls Sleep Will make the corresponding PCB Enter blocking queue , Unable to go up CPU;

7 about sleep There is a limit to the time it takes to get into the blocking queue , When the time comes , The system will PCB So back to the original ready queue ;

8 join The restored condition is that the corresponding thread ends .

By shared resources, we mainly refer to two aspects
1) Memory , Thread one and thread two , Can share the same memory ( Same variable ) 2) file , Thread 1 open file , Thread 2 can also be used
In essence , Variables are memory , Two threads can access the same variable , This indicates that the two threads are using the same memory , But for multiple processes , Process one cannot access the variables of process two
Thread Common usage in class ,Thread Class is a class used to manage threads , In other words , Each thread has a unique Thread Class

Thread Common construction methods of
Thread() Create thread object Thread(Runnable target) With the help of Runnable Object to create a thread object Thread(String
name), Create thread object , And name ; Thread(Runnable target,String name) adopt runnable To create a thread object , And name it
The construction method with name is to facilitate debugging

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?