Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synchronize access to private members of a javascript object

I have a Javascript object created as follows:

var ccStatTracker = (function (){
  ccmap:{
    "1":["1","2","3","4"],
    "2":["4","5"];       
  }

  return {
    modifyCCMap: function (){
      // Code which takes following actions: 
      // - adds/removes keys.
      // - modifies arrays stored as values against the keys in the map.  
    }  
  }
)();

I have a DHTMLXGrid component which displays grid in the form of rows and columns. When I edit any cell in the grid, "onEditCell" event is called. Now, I want to call ccStatTracker.modifyCCMap() from an event handler function attached to "onEditCell" event. As I go on modifying the cells, this event will be called asynchronously which will in turn call a function "modifyCCMap" which will modify private member "CCMap" of my Javascript object. So the latest state of my CCMap as seen by two calls might be different right? So what is the best way to handle this? Is there something as "Synchronized" in Javascript as in Java?

Please help me as it will determine the approach we want to take for implementing this.

like image 641
Shailesh Vaishampayan Avatar asked Jun 09 '12 11:06

Shailesh Vaishampayan


People also ask

Can synchronized method be private?

Even though the code works correctly when the private method is not synchronized, it would seem prudent from a maintainability perspective to make the private method synchronized. Intrinsic locks are re-entrant, there is no harm in adding the synchronized keyword to the private method.

How do I get synchronization in Javascript?

Synchronous JavaScript: As the name suggests synchronous means to be in a sequence, i.e. every statement of the code gets executed one by one. So, basically a statement has to wait for the earlier statement to get executed. Let us understand this with the help of an example.

Can a variable be synchronized Java?

You can have both static synchronized method and nonstatic synchronized method and synchronized blocks in Java but we can not have synchronized variable in java. Using synchronized keyword with a variable is illegal and will result in compilation error.

Can we make static variable synchronized in Java?

To maintain the Synchronized behavior, we need a class-level lock rather than an instance-level lock which can be achieved by Static Synchronization. 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.


1 Answers

JavaScript is single-threaded (web-workers aside for a moment), nothing happens asynchronously (or everything for that matter) - all code: event handlers, timeouts, callbacks, etc. - run in the same thread, one after another.

Thus you don't need any synchronization in JavaScript. Any given piece of code in JavaScript is guaranteed to be executed by only a single thread. How cool is that?

See also

  • JavaScript equivalent of SwingUtilities.invokeLater()
  • "atomic" operation desturbed by asynchronous ajax callbacks
  • Are there any atomic javascript operations to deal with Ajax's asynchronous nature?
  • how is async programming (promises) implemented in javascript? isn't javascript a ui-threaded environment?
  • ...
like image 199
Tomasz Nurkiewicz Avatar answered Oct 19 '22 22:10

Tomasz Nurkiewicz