Multi-threaded programs may regularly go to a circumstance where numerous threads attempt to get to similar kind of resources and unexpected outcomes.
So it should be ensured by some synchronization strategy that just a single string can get to the asset at a given purpose of time.
Java gives a method for making threads and synchronizing their assignment by utilizing synchronized blocks. Synchronized blocks in Java are set apart with the synchronized catchphrase. A synchronized square in Java is synchronized on some protest(object). Every single synchronized block synchronized on a similar protest can just have one string executing inside them at once. Every other string endeavoring to enter the synchronized block are obstructed until the thread inside the synchronized block leaves the block.
synchronized(sync_object)
{
// Access shared variables and other
// shared resources
}
No comments:
Post a Comment