31

something about static:

  • instances of class share static method

the similar questions:

I am confusing about:

  • static method just have only one memory block?
  • if i use static method in multithreading, will it block?
Community
  • 1
  • 1
freeeze king
  • 489
  • 2
  • 6
  • 13
  • Explain a bit more about what you mean by - **if i use static method in multithreading, will it block**? Also , go through [this](http://stackoverflow.com/questions/18356795/static-versus-non-static-lock-object-in-synchronized-block) first. – Sabir Khan Dec 10 '16 at 06:02

4 Answers4

18

I am confusing about:

static method just have only one memory block? if i use static method in multithreading, will it block?

The static keyword in Java simply means "without regard or knowledge of any particular instance of an object."

An instance method can use this to access the fields of its associated instance, but a static method has no associated instance and so this makes no sense.

In multithreading, thread safety involves protecting the consistency and integrity of mutable data. Because objects encapsulate the state of their instance fields, instance methods only need to be concerned about thread safety in those circumstances in which more than one thread will be accessing the same object.

So while thread confinement of an object is a valid thread safety policy for instances of a class, this same reasoning is invalid for static methods because they have no instance.

This has nothing to do with memory blocks at all. It just has to do with access. An object instance is accessed through a reference. If the reference is thread confined, then the object to which that reference points will always be thread safe. But any thread anywhere that can access your class can potentially get to its static members because no reference to an instance is needed to use them.

Static methods are non-blocking by default. You can implement your own synchronization/thread safety policy and have your static method block if you wish.

scottb
  • 9,908
  • 3
  • 40
  • 56
18

Each thread has its own stack space, each time a thread calls a method (static or virtual) that call allocates a stack frame, which holds local variables. nothing about this is specific to static methods.

Static methods can be called concurrently by multiple threads, unless you specifically do something to thwart that, such as requiring that the caller acquire a lock (such as using the synchronized keyword).

Static methods are good for cases where there is no shared state. They may be ok in cases accessing or modifying threadsafe shared state, depending on what level of concurrency is needed and how efficient the threadsafe things being accessed are.

Look out for bottlenecks. Putting the synchronized keyword on a static method may be a problem as that limits your application to calling it with only one thread at a time. Alternative strategies including using atomic objects, using threadsafe data structures designed for high concurrency, or using thread confinement may be preferable to locking.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
13

static method just have only one memory block?

No, methods don't have memory blocks. Threads executing those methods do. Each thread will have it's own memory on the stack where it stores all the method arguments and variables.

if i use static method in multithreading, will it block

A thread cannot access the memory of another thread, but if there is some resource that belongs to all instances and is supposed to be accessed sequentially, then you can synchronize or lock the static method, thus making it a blocking one. Otherwise, no.

The_Tourist
  • 2,048
  • 17
  • 21
  • In fact, methods use memory block (apart of their stack memory), methods are defined in the memory. So, if I have a class with 1 method and I have 100 objects defined by this class, then I have 100 methods defined in the memory. – magallanes Mar 30 '18 at 13:27
  • @magallanes I don't think that's actually true. Methods are already a part of the compiled program image, and when that image is loaded into memory, there is a single shared copy of the method code. They are not allocated relative to object creation. If you know otherwise, please share the source/reference. – The_Tourist Mar 30 '18 at 14:57
  • Yes and no. In general, a code is defined once in the memory, however, every definition of a method uses a pointer (and it creates a stack when it's used), and it uses memory per instance. However, and depending on the JVM, sometimes it does some kind of magic. Yet, a method per instance uses memory. – magallanes Mar 31 '18 at 11:25
  • @magallanes Yeah, and all that stack space for the pointers you talk about is a part of the thread's memory - which is what I said in my answer. Method invocations don't create a stack; they create a "stack frame" on the thread's stack. In the end, it's the _thread_ that needs all that memory, NOT _methods_ themselves. – The_Tourist Apr 03 '18 at 17:28
0

Even though there is one instance of a static method, each thread gets its own stack-frame, which means each thread can exercise the same method but in a separate "world" from other threads.

Threads always get their own stack, even for a singleton class (one instance): so, when to use static methods and when to not?

The main reason for not using static methods everywhere is that they are difficult to unit-test barring manipulating compiled code (Powermock), so static methods should have no dependencies that would require mocking, i.e. the test calls the real method with an input and asserts the output, verbatim, in two steps.

Non-static methods allow you to isolate your test solely to that method by stubbing, mocking, or spying on objects that the method depends on.

baumannalexj
  • 766
  • 1
  • 9
  • 20