What Does 'Synchronized' Mean

What does 'synchronized' mean?

The synchronized keyword is all about different threads reading and writing to the same variables, objects and resources. This is not a trivial topic in Java, but here is a quote from Sun:

synchronized methods enable a simple
strategy for preventing thread
interference and memory consistency
errors: if an object is visible to
more than one thread, all reads or
writes to that object's variables are
done through synchronized methods.

In a very, very small nutshell: When you have two threads that are reading and writing to the same 'resource', say a variable named foo, you need to ensure that these threads access the variable in an atomic way. Without the synchronized keyword, your thread 1 may not see the change thread 2 made to foo, or worse, it may only be half changed. This would not be what you logically expect.

Again, this is a non-trivial topic in Java. To learn more, explore topics here on SO and the Interwebs about:

  • Concurrency
  • Java Memory Model

Keep exploring these topics until the name "Brian Goetz" becomes permanently associated with the term "concurrency" in your brain.

What does synchronized mean in Java?

There is no synchronized keyword in C++.

There is one in Java, though, where for methods it means the following two things:

  • It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
  • When a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

Similar rules apply to arbitrary blocks.

Also, I recommend learning from a peer-reviewed book, not some arbitrary non-authoritative website.

what does synchronized mean in C++

Synchronization means sharing of resources between threads and processes without leading to race around conditions and dead locks.

Without synchronization in 1st statement means that it does not lock the resource and unlock it when it is done.

In second statement he means to say as it is a const object it cannot be modified and is hence perfectly immutable and doesn't need synchronization.

A study on thread synchronization techniques using Mutex and Semaphore will help you better understand why it is needed and how it is done.

What does synchronized mean in context of Vector / ArrayList?

It means that multiple threads can modify the Vector in parallel without risk of data corruption.

If you want to do this with an ArrayList, you need to use the synchronized keyword.

What does (Synchronized) after the database name mean in SSMS.

High availability is a concept in sql which helps masks the effects of a hardware or software failure and maintains the availability of applications so that the perceived downtime for users is minimized.

There are multiple ways by which this can be achieved. One of the ways is using adding multiple servers containing the same database to the availability group using 'AlwaysOn high availibility' feature which is found in object explorer below the databases.

Sample Image

In this feature, only one machine\server is called primary and others in the group are considered secondary nodes. Primary node always has read-write permissions and secondary nodes will only be in read modes there by we can ensure data quality even. Secondary nodes always will be in synchronized mode as they continuoulsy sync data from primary nodes and if you have access to primary node, the DB there will be in synchronizing mode.

The machines in the availability group can be found as below

Sample Image

You can get more info here:

Always On availability groups: a high-availability and disaster-recovery solution

The Always On availability groups feature is a high-availability and disaster-recovery solution that provides an enterprise-level alternative to database mirroring. Introduced in SQL Server 2012 (11.x), Always On availability groups maximizes the availability of a set of user databases for an enterprise. An availability group supports a failover environment for a discrete set of user databases, known as availability databases, that fail over together. An availability group supports a set of read-write primary databases and one to eight sets of corresponding secondary databases. Optionally, secondary databases can be made available for read-only access and/or some backup operations.

An availability group fails over at the level of an availability replica. Failovers are not caused by database issues such as a database becoming suspect due to a loss of a data file, deletion of a database, or corruption of a transaction log.

What do the terms synchronized and thread-safe mean?

A thread is an execution path of a program. A single threaded program will only have one thread and so this problem doesn't arise. Virtually all GUI programs have multiple execution path and hence threads - one for processing the display of the GUI and handing user input, others for actually performing the operations of the program. This is so that the UI is still responsive while the program is working.

In the simplest of terms threadsafe means that it is safe to be accessed from multiple threads. When you are using multiple threads in a program and they are each attempting to access a common data structure or location in memory several bad things can happen. So, you add some extra code to prevent those bad things. For example, if two people were writing the same document at the same time, the second person to save will overwrite the work of the first person. To make it thread safe then, you have to force person 1 to wait for person 2 to complete their task before allowing person 1 to edit the document.

Synchronized means that in a multiple threaded environment, a Synchronizedobject does not let two threads access a method/block of code at the same time. This means that one thread can't be reading while another updates it.

The second thread will instead wait until the first is done. The overhead is speed, but the advantage is guaranteed consistency of data.

If your application is single threaded though, Synchronized has no benefit.

What does synchronize exactly do?

If T1 gets a lock on method do() i.e method is under synchronized block.
and other portion of program say method display() is not synchronized then other threads can access this method.
So your or is correct.

What does the use of synchronized() mean in this case?

In this case synchronized means accessing the data of mObservers with some sort of locking to ensure thread safety. Other threads cannot access mObservers until the current thread finished with the synchronized block.

Check out this documentation.

What does @synchronized mean

See Synchronization. You find that is "a convenient way to create mutex locks on the fly in Objective-C code". a related question is What does @synchronized() do?.



Related Topics



Leave a reply



Submit