Concurrent access to static methods

前端 未结 6 1848
面向向阳花
面向向阳花 2020-12-14 01:45

I have a static method with the following signature:

public static List processRequest(RequestObject req){
  // process the request obje         


        
相关标签:
6条回答
  • 2020-12-14 02:38

    You can check it yourself:

    public class ConcurrentStatic {
    
        public static void main(String[] args) {
            for (String name: new String[] {"Foo", "Bar", "Baz"}) {
                new Thread(getRunnable(name)).start();
            }
        }
    
        public static Runnable getRunnable(final String name) {
            return new Runnable() {
                public void run() {
                    longTask(name);
                }
            };
        }
    
        public static void longTask(String label) {
            System.out.println(label + ": start");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(label + ": end");
        }
    
    }
    
    0 讨论(0)
  • 2020-12-14 02:39

    I see a lot of answers but none really pointing out the reason.

    So this can be thought like this,
    Whenever a thread is created, it is created with its own stack (I guess the size of the stack at the time of creation is ~2MB). So any execution that happens actually happens within the context of this thread stack.
    Any variable that is created lives in the heap but it's reference lives in the stack with the exceptions being static variables which do not live in the thread stack.

    Any function call you make is actually pushed onto the thread stack, be it static or non-static. Since the complete method was pushed onto the stack, any variable creation that takes place lives within the stack (again exceptions being static variables) and only accessible to one thread.

    So all the methods are thread safe until they change the state of some static variable.

    0 讨论(0)
  • 2020-12-14 02:40

    Answering exactly your question:

    1. Method will be executed concurrently (multiple times in the same time if you have several threads).
    2. Requests will be handled concurrently.

    You need to add the synchronized modifier if you are working with objects that require concurrent access.

    0 讨论(0)
  • 2020-12-14 02:40

    All your calls to the method will be executed concurrently... but:

    You may have concurrency issue (and being in non thread-safe situation) as soon as the code of your static method modify static variables. And in this case, you can declare your method as synchronized

    If your method only use local variables you won't have concurrency issues.

    0 讨论(0)
  • 2020-12-14 02:41

    If you need to avoid concurrent execution, you need to explicitly synchronize. The fact that the method is static has nothing to do with it. If you declare the method itself to be synchronized, then the synchronization will be on the class object. Otherwise you will need to synchronize on some static object (since this doesn't exist for static methods).

    0 讨论(0)
  • 2020-12-14 02:44

    all method invocations from separate threads in java are concurrent by default.

    0 讨论(0)
提交回复
热议问题