当前位置:网站首页>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

Concurrent programming :java Five states of thread pool _ Fang's programmer's blog -CSDN Blog _java Thread pool state

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 .

原网站

版权声明
本文为[haohulala]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220841448124.html