• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronization confirmation

 
amithk kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two classes factor and addition

factor is having val variable that means it is shared across multiple threads (in the current application we are using two threads).

addition class is having a add variable it is also shared across multiple threads, because it's instantiated in factor class.

my question is

1)if synchronized(this) is used, which means that any of the two threads will lock on factor instance and increment val variable value till the loop exits.
    so synchronized(this) means here that we should not use any other instance variables. We have to use only the variables of factor instance inside the synchronized block?

2) if synchronized(addition) means here that we have to use only  add variable not the val variable of factor instance class?


There is a big confusion regarding this synchronization block .
what i understood  is synchronization block will lock on the object's instance and guard the operation and make it thread safe. But using different instance really means that it should guard only that particular instance variables not any other instance variables. Can anyone explain in depth concept regarding this relating with the code provided below

The factor class



The addition class



    The main class



    
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the program output when it executes?
Is there anything unexpected?

synchronization is a gate keeper for a block of code that ensures that only one thread executes it at a time.  I don't think it has any control over a class's instance variables.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amithk kumar wrote:
There is a big confusion regarding this synchronization block .
what i understood  is synchronization block will lock on the object's instance and guard the operation and make it thread safe. But using different instance really means that it should guard only that particular instance variables not any other instance variables. Can anyone explain in depth concept regarding this relating with the code provided below


Well, first, I would like you to elaborate what you mean by "guard the operation" or "make it thread safe". What is the lock "guarding"? And how does this "guarding" makes anything thread safe?

Simply... Boiling it down to the bare basics, synchronizing on an object does one thing -- it prevents another thread from synchronizing on the same object (at the same time). In effect, any other thread can't synchronize on the same object, until the first thread releases the lock.

This means that "guarding" any operation, or making anything "thread safe" is completely cooperative. The JVM makes sure that only one thread owns the lock at any given time. And it is the coding that the threads are executing, that decides what to do when it owns the thread -- which it can use to correctly use shared resources, and hence, is thread safe (if done correctly).

Henry
 
Santosh Kumar Nayak
Ranch Hand
Posts: 149
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use synchronized keyword then this is what going to happen.

When thread1 is accessing currInsLock then thread2 cannot access currInsLock.
Similarly when thread2 is accessing diffInsLock then thread1 cannot access it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!