当前位置:网站首页>Didi's two-sided summary
Didi's two-sided summary
2022-06-22 08:59:00 【haohulala】
Record the two questions didi didn't answer
Question 1 : How the thread pool knows the status of threads
I don't know if I should answer the five states of threads
Question two :synchronize Modifies the difference between static and non static methods
Modified static method , Locks are added to classes , All objects of the class compete for a lock ; Modify non static methods , Lock on a single object , There is no competition between different objects .
synchronized Modifies static and non static methods - Simple books
First , stay java in , Class can only be loaded once , There will be multiple references .
then , Static methods are not referenced , It belongs to this class .
synchronized If the modification method ,jvm In fact, the implementation is to use all code blocks synchronized Wrapped .
synchronized(this){
// Business logic
}
If it's a modification of a static method , This this It is not a quotation , It's a class .
If it is a common way to modify , This this It is the reference of this class .
https://www.baidu.com/link?url=2eRXCZP6LaSNArmclltMoim4nPuz88fGnCP8zbdt3YE3BUC03xWi2qfOL2GR20F8sGOokX58_99iSlDylpZB9a&wd=&eqid=bfec08b00000ffdd0000000362b037d5
Let's do an experiment
public class Solutionaaa {
public synchronized static void sayhello(){
for(int i=0; i<100; i++) {
System.out.println("hello...");
}
}
public synchronized void sayhi(){
for(int i=0; i<100; i++) {
System.out.println(Thread.currentThread().getName() + " -> hi...");
}
}
public static void main(String[] args) {
Solutionaaa solutionaaa1 = new Solutionaaa();
Solutionaaa solutionaaa2 = new Solutionaaa();
new Thread(new Runnable() {
@Override
public void run() {
solutionaaa1.sayhi();
}
}, "Thread1").start();
new Thread(new Runnable() {
@Override
public void run() {
solutionaaa2.sayhi();
}
}, "Thread2").start();
}
}
You can see synchronized Decorate non static methods and use different instances The method is thread unsafe when executed
Change the code , Use the same instance to execute the method
public class Solutionaaa {
public synchronized static void sayhello(){
for(int i=0; i<100; i++) {
System.out.println("hello...");
}
}
public synchronized void sayhi(){
for(int i=0; i<100; i++) {
System.out.println(Thread.currentThread().getName() + " -> hi...");
}
}
public static void main(String[] args) {
Solutionaaa solutionaaa1 = new Solutionaaa();
new Thread(new Runnable() {
@Override
public void run() {
solutionaaa1.sayhi();
}
}, "Thread1").start();
new Thread(new Runnable() {
@Override
public void run() {
solutionaaa1.sayhi();
}
}, "Thread2").start();
}
}
You can find Use synchronized Decorate a non static method and use the same instance It is thread safe to execute this method
Let's take another look at decorating static methods
public class Solutionaaa {
public synchronized static void sayhello(){
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + " -> hello...");
}
}
public synchronized void sayhi(){
for(int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + " -> hi...");
}
}
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
sayhello();
}
}, "Thread1").start();
new Thread(new Runnable() {
@Override
public void run() {
sayhello();
}
}, "Thread2").start();
}
}
I did it many times , The results are orderly , therefore synchronized Modified static method Is thread - safe
So let's summarize
Use synchronized It is thread safe to decorate static methods
Use synchronized It is thread safe to decorate a non static method and execute the method with the same instance
Use synchronized It is thread unsafe to decorate non static methods and execute methods with different instances
Finally, let's talk about the two-sided time algorithm problem , There are two questions in total . The first is to implement the stack by yourself , Write out the methods of inbound and outbound ; The second is binary search , It's not that hard .
边栏推荐
猜你喜欢

14 职责链模式
![Luogu p4292 [wc2010] reconstruction plan](/img/09/5417e0b62e5205c4dabf4571823492.png)
Luogu p4292 [wc2010] reconstruction plan

【路径规划】辅助点与多段贝塞尔平滑RRT

Chapter II exercise | MNIST dataset | Titanic dataset | image enhancement

16 解释器模式

18 intermediary model

Web knowledge 3 (cookie+session)

Use record of rabbit nest

深度学习——(1)ResNet实现

Remove the restriction of video memory occupied by tensorflow GPU
随机推荐
When easypoi imports the secondary header data of an excel file, the data in the first column of the @excelentity entity class is null
新型冠状病毒疫情
Flask blog practice - realize the article list page and details page
10.File/IO流-bite
Report: in the technical field, men are more likely to get job interviews
【自适应控制】最小二乘法离线辨识
Interview shock 59: can there be multiple auto increment columns in a table?
WebRTC系列-网络传输之IceConfig及stunPing失败处理
The third-party libraries commonly used in golang development are not the most complete, but more complete
Thread.start()方法源码分析
深度学习——(1)ResNet实现
Thread. Source code analysis of start() method
10 装饰模式
电机学感应电动机重点知识总结(现有题目中反映的)
Flask blog practice - realize article management
15 命令模式
One hot and embedding
Matrix decomposition
How much do you know about the required encryption industry terms in 2022?
Deep learning - (1) RESNET implementation