wood burning stoves 2.0*
The moose likes Java in General and the fly likes Thread & Synchronized argument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thread & Synchronized argument" Watch "Thread & Synchronized argument" New topic
Author

Thread & Synchronized argument

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

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..??



No Kaustubh No Fun, Know Kaustubh Know Fun..
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread & Synchronized argument