How I can make my constructor synchronized?

Raj

I have a class.

public CarSold{

static int count;

CarSold(){count++;}


// Other code
}

Now this is running in multithreading enviroment. And I read here that constructors are not implicitly syncronized.

In that case, there is a chance that I don't have the correct count of cars sold in the count variable.

What is the way to make sure that it has a correct count?

I can think of 2 ways.

  1. Class level locking inside the constructor using synchronized(CarSold.class)
  2. I can use AtomicInteger.

Can this 2 approach solves the problem ? And is there is any other way?

Thanks.

user177800

You don't synchronize a constructor, that doesn't make any sense semantically, you synchronize the access to the variable. Which can happen from anywhere that has access to it.

What you are looking for in this case is AtomicInteger.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make my own piecewise_construct constructor?

From Dev

How can I make my java program not create multiple instances when calling the constructor with parameters?

From Dev

Can I make an overridden method synchronized?

From Dev

How can I make my navbar responsive?

From Dev

How can I make my program faster?

From Dev

How can I make my REGEX general?

From Dev

How can i make my variable defined?

From Dev

How can I make '=' invoke a copy constructor in Perl?

From Dev

Javascript TypeError: ... is not a Constructor - How can I make Constructors from Constructors?

From Dev

How can I make the size of an array dependent on a constructor parameter?

From Java

How can I assign variables to my class constructor if they are static?

From Java

How can I pass my class into its own constructor?

From Dev

How can I call my js constructor function?

From Dev

How can I keep folders synchronized between several machines over my home network?

From Dev

How can i Implement synchronized in Java?

From Dev

How can I make my vim behave like my cat?

From Dev

how can i make my linux service trigger my signal?

From Dev

How can I make a derived constructor pass a concatenated value to its parent constructor?

From Dev

How to make phpspec evaluate my constructor

From Dev

How can I make TVTime find my mixer?

From Dev

How can I compare BigDecimals to make my tests pass?

From Java

How can I make my BFS algorithm run faster?

From Dev

Jira: How can I make images part of my ticket description?

From Dev

Dijkstra's Dilema: How can I make my Algorithm abstract?

From Dev

How can I make my custom control fully clickable at designtime?

From Dev

How can I make my Python script faster?

From Java

How can I make my own base image for Docker?

From Dev

How can I make the borders of my map thicker Google Geomap?

From Java

How can I make my picture display on a half of the screen?

Related Related

  1. 1

    How can I make my own piecewise_construct constructor?

  2. 2

    How can I make my java program not create multiple instances when calling the constructor with parameters?

  3. 3

    Can I make an overridden method synchronized?

  4. 4

    How can I make my navbar responsive?

  5. 5

    How can I make my program faster?

  6. 6

    How can I make my REGEX general?

  7. 7

    How can i make my variable defined?

  8. 8

    How can I make '=' invoke a copy constructor in Perl?

  9. 9

    Javascript TypeError: ... is not a Constructor - How can I make Constructors from Constructors?

  10. 10

    How can I make the size of an array dependent on a constructor parameter?

  11. 11

    How can I assign variables to my class constructor if they are static?

  12. 12

    How can I pass my class into its own constructor?

  13. 13

    How can I call my js constructor function?

  14. 14

    How can I keep folders synchronized between several machines over my home network?

  15. 15

    How can i Implement synchronized in Java?

  16. 16

    How can I make my vim behave like my cat?

  17. 17

    how can i make my linux service trigger my signal?

  18. 18

    How can I make a derived constructor pass a concatenated value to its parent constructor?

  19. 19

    How to make phpspec evaluate my constructor

  20. 20

    How can I make TVTime find my mixer?

  21. 21

    How can I compare BigDecimals to make my tests pass?

  22. 22

    How can I make my BFS algorithm run faster?

  23. 23

    Jira: How can I make images part of my ticket description?

  24. 24

    Dijkstra's Dilema: How can I make my Algorithm abstract?

  25. 25

    How can I make my custom control fully clickable at designtime?

  26. 26

    How can I make my Python script faster?

  27. 27

    How can I make my own base image for Docker?

  28. 28

    How can I make the borders of my map thicker Google Geomap?

  29. 29

    How can I make my picture display on a half of the screen?

HotTag

Archive