What is the difference between synchronized block and normal block ?

Questions by sadashivarao   answers by sadashivarao

Showing Answers 1 - 5 of 5 Answers

hsharma12

  • Apr 8th, 2006
 

A synchronized block is a thread safe block which means it cant be accessed by more than one object at a time. It is accessed by objects in a queue on a priority basis.So it can be used to make data integrity.

While a normal block can be accessed by any no of objects at a time. Data Integrity cant be made with this.

  Was this answer useful?  Yes

sbarik

  • Apr 10th, 2006
 

Synchronized Block:-->

Sysnchronization is Used in a case of Multithreading where Acess of multiple threads to a resource concuurently can alter the integrity of data.Synchronization is used to controll acess to a shared resource.

Synchronized block allows execution of arbitary code on the lock of arbitary object-->

eg:--synchronized(<objref>)

        {

       /*code*/ 

       }

with this to  no two threads can execute this code block on the Object denoted by objref .Any thread which acess to exceute this code first have to aquire the lock og object<objref> thebn it can proceed..at the same time any other thread wishing to execute the code block have to wait to acquire the lock of the object as it has been acquired by another thread.

Whereas without the keyword of synchronization there is no such restiction..i.e multiple threads can acess to the resource simultaneously..

Hope this can solve ur problem...

  Was this answer useful?  Yes

Deepthi

  • Apr 13th, 2006
 

              Synchronized block is a restricted block which can be accessed by the objects which acquired lock. 

              Normal block is a free block , that any object can enter into that block.

  Was this answer useful?  Yes

Agreed that for a synchronized block to get accessed to, the thread has to acquire object lock.

With the above condition:

a. Thread2 cannot access the synch. block until Thread1 is out of it.

b. Thread2 can access normal block/method/class variable without acquiring object lock.

Scenario:

While Thread1 is accessing synch. block and that block is accessing un-synch (normal) block/call method/modifying class variable.

At the sametime Thread2 is accessing un-synch block which is also accessing un-synch (normal) block/call method/modifying class variable. How can it maintain data-integrity? Please throw light on this scenario.

-ashruf

  Was this answer useful?  Yes

sbarik

  • Apr 19th, 2006
 

If U want to perserve data integrity Make those methods or code blocks also synchronized..Basically its a programming issue .It depends entirely on the progarmmer which resources to synchronize 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions