answersLogoWhite

0

How do you synchronize java?

Updated: 8/11/2023
User Avatar

Wiki User

16y ago

Best Answer

Synchronizaion is the process by which only one Thread can access an object at a time,until and unless thread releases it's lock no other thread can acess the object. there are two forms of syncronization method and statement.

to make a method synchronized just add the keyword synchronized to the signature ie: public synchronized void increment() {

c++;

}

to create syncrhonized statements are like this:

synchronized(this) {

lastName = name;

nameCount++;

}

note: constructors can not be syncronized

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

Synchronization is a process where by a mutually-exclusive (mutex) lock is placed on a particular object for the duration of the execution of a block of code. It cannot be applied to primitives (double, int, etc.)

Synchronization ensures that only one thread has access to the synchronized object at a time, such as to prevent two threads from modifying a variable or to prevent the contents of a collection being changed while it is being iterated over.

Whole methods and arbitrary individual parts of one may be declared as synchronized.

An example of a synchronized method is:

public synchronized void setBalance(int amount) { ... }

Which effectively locks the object to which that method belongs (this), so it may be modified by only one thread at a time.

An example of a synchronized block, which requires exclusive access to the object myBankAccount is:

public void addMoney(Account act, int amount) {

// ... non-critical code can go here ...

synchronized(act) {

// it is imperative that nothing else is able to change

// the balance in this person's account while we do this!

act.setBalance(act.getBalance() + amount);

}

// ... more non-critical code can go here ...

}

In both cases, while either object is locked any additional threads which try to call either the setBalance or addMoney methods will block, meaning they will 'freeze' until they can gain exclusive access. Any code before or after the synchronized section can run as normal before and after the synchronized section completes: the block happens when a synchronized code section is encountered and it is not possible to get exclusive access to the target object.

Synchronization in Java is very expensive (in terms of time), and can easily become a performance bottleneck, however it is sometimes possible to overcome such performance loss simply moving superfluous synchronization to another place. In a situation where a data structure is being iterated over continually but rarely changed, it would be far slower to synchronize MyDataStructure.getData() resulting in continual, largely unnecessary locking/unlocking, than it would be simply to synchronize the add-/remove element methods.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Synchronization is the capability od controlling the access of multiple threads to any shared resource. It is used : 1) to prevent thread interface. 2)to prevent consistency problem

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you synchronize java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How does one get java synchronized?

The Java program needs to be synchronized. Doing so can be complicated, but can be done. The best way to synchronize would be using the website and walk through the steps.


Where can one find a guide on how to synchronize Java?

There are a number of websites which specialise in Java tips and how-to guides. Websites from AvaJava, CSC and JavaMex are just some of the websites available as well as blogs such as that from 'Javarevisited'.


What is the root word of synchronize?

The suffix of synchronize is -ize. The suffix is the bit at the end of the word.


What the prefix of synchronize?

the prefix of synchronize is syn


What are servlets and where can they be downloaded?

Servlets are something that can handle multiple requests on-line. They can synchronize things such as conferencing and other networking components depending on the protocols. They can be downloaded from several websites for example java.


How many syllables are there in synchronize?

There are three syllables in synchronize.


What is the past tense of synchronize?

Synchronized is the past tense of synchronize.


Need a sentence for synchronize?

I am about to synchronize my watch to the atomic clock.


What part of speech is synchronize?

The word synchronized is a verb. It is the past tense form of the verb synchronize.


What is synchronize in java?

Synchronization is a feature by which only one thread can access a resource in a particular time of instance. No other thread can interrupt for that resource. In java to make our method synchronize we use synchronized keyword with that method. So Synchronization is just managing the resources among Threads. Vijai Shanker Just to add with the invent of Java 5 we have more option to write synchronized code in Java instead of using synchronized keyword we can use Lock Classes available in concurrency package.


What are some example sentences we can use for the word synchronize?

Lets synchronize our watches.


What are some ways to synchronize folders?

There are several ways that some one can synchronize folders. Depending on what a persons preference is they can synchronize by color, shape, or content.