Developer community
0 votes
3.5k views
asked by Sr Member (4.8k points) 10 25 31 | 3.5k views

1 Answer

0 votes

 When multiple threads wants to access the object at the same time avoiding multiple threads to access the same and giving access to one of the thread is called as thread synchronization. Thread synchronization can be done into two ways.

1.     Synchronized Block

2.     synchronized  Method

1.Synchronized Block: Synchronizing a group of statements or part of a code is called as Synchronized Block.

Syntax: Synchronized(object){

                     Statements;    }

2.Synchronized Method: when we want to Synchronized all the statements in a method we go for synchronized method.

Syntax: Synchronized returnType methodName(){

                        Statements;

                                    }

Note: In the previous program multiple threads acting on the same object leading to data in consistency, to avoid the data inconsistency problem. We have to synchronize the threads acting on the same object.

Ex: public Synchronized void run(){

            Same code previous programs;

                        }

When multiple threads are acting on synchronized objects then there is chance of other problems like ‘Deadlock’ occurring in the application.

answered by Expert (15k points) 15 23 31

Related questions

0 votes
1 answer
0 votes
1 answer
asked by john Guru (27.1k points) 35 52 102 | 30 views
+1 vote
2 answers
asked by keem Expert (13.2k points) 27 43 61 | 36 views
0 votes
2 answers
asked by sara Guru (24.6k points) 30 54 84 | 39 views
0 votes
0 answers
asked by Adam Jr Member (380 points) 3 | 90 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,754 questions
4,046 answers
117 comments
10,709 users