Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am confused about using static method in Multithreading java?

something about static:

  • instances of class share static method

the similar questions:

  • Java: when to use static methods
  • What does the 'static' keyword do in a class?

I am confusing about:

  • static method just have only one memory block?
  • if i use static method in multithreading, will it block?
like image 916
freeeze king Avatar asked Dec 10 '16 03:12

freeeze king


People also ask

Can we use static method in multithreading Java?

Static Synchronized method is also a method of synchronizing a method in java such that no two threads can act simultaneously static upon the synchronized method. The only difference is by using Static Synchronized. We are attaining a class-level lock such that only one thread will operate on the method.

Can static method be called by multiple threads?

accessing the code is no problem, static methods can be called with multiple threads. It depends on how it is programmed in the method, if the code is not thread safe, it will cause problems.

What can I use instead of static methods?

You can use the full power of inheritance and overriding since your methods are no longer static. You can use the constructor to do any initialisation, including associating SQL with the table (SQL that your methods can use later). This should make all your problems above go away, or at least get much simpler.

Why should we avoid static in Java?

Static variables are generally considered bad because they represent global state and are therefore much more difficult to reason about. In particular, they break the assumptions of object-oriented programming.


1 Answers

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.

like image 66
scottb Avatar answered Sep 20 '22 13:09

scottb