5

I have encountered a question which I am struggled to understand though I found the answer. Please look at this and provide me an explanation on the answer.

public class TestSeven extends Thread {
  private static int x;
  public synchronized void doThings() {
    int current = x;
    current++;
    x = current;
  }
  public void run() {
    doThings();
  }
} 

question and given answer is... Which statement is true?

A. Compilation fails.

B. An exception is thrown at runtime.

C. Synchronizing the run() method would make the class thread-safe.

D. The data in variable "x" are protected from concurrent access problems.

E. Declaring the doThings() method as static would make the class thread-safe.

F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the class thread-safe.

Bold one is given as the answer. Thanks for the replies in advance !!

eis
  • 51,991
  • 13
  • 150
  • 199

4 Answers4

6

If you have a synchronized instance method like that, it synchronizes on the instance, i.e. every instance can access the method on its own. But x is static, so any instance of TestSeven can access it concurrently. If doThings() is static, it synchronizes on the class, so only one instance can access synchronized code at a given time.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
user140547
  • 7,750
  • 3
  • 28
  • 80
1

Here is what will happen.

public static syncronized void doThings();

This will synchronize method at class level. Which means only one instance of class will be able to access the code at an instance of time, Which means there is no possibility of static variable x to be modified by other instances.

In other case,

public syncronized void doThings();

This means that doThings(); method is synchronized on current object. i.e. an instance of TestSeven, so multiple instances can access the method which in turn can change the static shared variable x which is not desirable.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • So if there is a static synchronized method lies in a class means that whole class will be locked ?? Though there are more synchronized and non-synchronized method available in that class. – Sidath Bhanuka Randeniya Apr 04 '16 at 12:45
  • It means only one instance will be able to execute code that is synchronized on class leve. – Prasad Kharkar Apr 04 '16 at 12:48
  • What I meant was one static synchronized method would enough to make a class synchronized ?? (Assume that class has more synchronized and non-synchronized method available ) – Sidath Bhanuka Randeniya Apr 04 '16 at 15:30
  • @SidathBhanukaRandeniya: It's not about synchronizing access to a class, but the access to.the variable `x`. If the access to `x` should be synchronized, every method which accesses `x` has to be synchronized. Methods which don't access `x` have not to be synchronized. – user140547 Apr 04 '16 at 17:23
1

Making the method static will make it available at class level and not at the instance level, so method behaviour will be same for all the objects/instances of this class like a static variable. So class will become thread safe because method behaviour is not instance specific

Pushpendra Pal
  • 640
  • 6
  • 19
1

Thread safe means that if it is used from multiple threads at the same time, it should not cause any problems.

Executing without static

    public class TestSeven extends Thread {
      private static int x;
      public synchronized void doThings() {
       int current = x;
       current++;
       x = current;
    }
  public void run() {
    doThings();
  }
} 

If you make an instance of TestSeven and call its run(), it would give the output as 1 every time. But wait x is static so isn't the output should be incremented by 1 every time you call it? So this indicates that method is not thread safe. To make it so we would:

Execute with static

    public class TestSeven extends Thread {
  private static int x;
  public static synchronized void doThings() {
    int current = x;
    current++;
    x = current;
  }
  public void run() {
    doThings();
  }
} 

Remember synchronized is 'a way' to make things thread safe, but there are other ways.

For more info see this

Community
  • 1
  • 1
rusted brain
  • 1,052
  • 10
  • 23