• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread & Synchronized argument

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read about Thread and synchronization on different- different places and I am confused over it

Please correct me:- thread is a leight weight process... If I have a process A then divinding it into parts such as A1, A2....An, which run concurrently known as its threads..? or A1,A2 ...An each of them full part of process A and run concurrently??

Synchronized (Argument) keeps lock only for threads asking for lock which are matching argument's instance threads.... like if it is Synchronized(A) then thread on instance A can hold lock ... and other instance thread will pass by with this lock?? or is it locked for every thread, doesn't matter which instance thread is..??

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure I understand the question, probably because you are trying to substitute A for different things... but let me see if I can respond in a way that helps...

Kaustubh G Sharma wrote:thread is a leight weight process... If I have a process A then divinding it into parts such as A1, A2....An, which run concurrently known as its threads..? or A1,A2 ...An each of them full part of process A and run concurrently??

A process is an executable stream with its own memory. A thread is an executable stream which shares memory space with all other threads in the same process. A process can have multiple threads. Threads should be considered to run concurrently, but whether they run simultaneously or split time depends on the environment.

Kaustubh G Sharma wrote:Synchronized (Argument) keeps lock only for threads asking for lock which are matching argument's instance threads.... like if it is Synchronized(A) then thread on instance A can hold lock ... and other instance thread will pass by with this lock?? or is it locked for every thread, doesn't matter which instance thread is..??

The argument to the synchronized block can be any object, and is not dependent on a particular thread - in fact it is only useful when the Object is available in multiple threads. When a thread enters a synchronized block, it takes the Object's 'lock' and holds it. Any other thread which reaches the same code would try to get the Object's lock, but would not be able to (since the first thread has it), and so would wait until the first thread releases the lock. The locks come from the Object, so you would need to synchronize on the same instance, and that instance would need to be shared between threads in order for synchronization to work.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic