OCPJP Exam Threading

I came across a question that I am trying to understand, although I found the answer. Look at that and give me an explanation in return.

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 ... Which statement is true?

a. Compilation error.

B. An exception is thrown at runtime.

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

E. The data in the variable "x" is protected from concurrent access problems.

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

F. Wrapping statements in doThings () in a synchronized (new object ()) {} block will make the class thread safe.

The answer is in bold. Thanks for the answers in advance!

+5
source share
4 answers

If you have a similar synchronized instance method, it is synchronized with the instance, that is, each instance can access this method on its own. But x is static, so any instance of TestSeven can access it at the same time. If doThings() is static, it synchronizes with the class, so only one instance can access the synchronized code at a given time.

+6
source

Here is what will happen.

 public static syncronized void doThings(); 

This will synchronize the method at the class level. This means that only one instance of the class will be able to access the code in the time instance. This means that the static variable x cannot be changed by other instances.

Otherwise

 public syncronized void doThings(); 

This means doThings (); the method is synchronized by the current object. that is, an instance of TestSeven , so several instances can access the method, which in turn can modify the static shared variable x , which is undesirable.

+1
source

Creating a static method will make it available at the class level, not at the instance level, so the behavior of the method will be the same for all objects / instances of this class, such as a static variable. Thus, the class will become thread safe, since the behavior of the method is not dependent on the instance

+1
source

A safe thread means that if it is used simultaneously from several threads, it should not cause any problems.

Execution 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 it run() , it will return the result as 1 each time. But wait x is static , so should you not increase the output by 1 every time you call it? Thus, this means that the method is not thread safe . To do this, we would:

Run 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 the "way" to make threads safe, but there are other ways.

See more details.

+1
source

Source: https://habr.com/ru/post/1246379/


All Articles