• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread & Synchronized argument

 
Ranch Hand
Posts: 1283
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..??

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic