• 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

why is start method in Thread class synchronized

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
usually we start new threads from main method.I dont feel the need to make the start method synchronized.Another method in same class join is also synchronized any once pls explain me why.
[ March 29, 2008: Message edited by: vijaya saradhi ]
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Straight from the Javadoc of Thread.start():

I think that's fairly self explainitory.
[ March 29, 2008: Message edited by: Jelle Klap ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What? Isn't it?
 
vijaya saradhi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im confused please explain , thanks in advance
 
vijaya saradhi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okai but that does not prevent us from calling the start method more than once.So can you show me using a sample code the situation where in a thread's start method may be called automatically more than once.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, seeing that it's clearly defined that starting a particular Thread more than once is illegal, how else would you prevent multiple threads of execution from running the start() method concurrently, thereby breaking that rule? It may be difficult to imagine a reasonable scenario in which multiple threads would attempt to invoke start() on the same Thread object, but it's possible nonetheless. Atleast now you have the guarentee that start() will only ever run once, and any consecutive attempts at running it will result in an IllegalThreadStateException.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wasn't getting it, but I see it now.

In Thread, the implementation of start is...

...where "started" is the resource that requires synchronization. If the start method were not synchronized, then it's conceivable that the thread could be started more than once, because the scheduler could allow a competing thread to run (and call start) between the line that reads "started" and the line that reassigns "started" to "true."
[ March 29, 2008: Message edited by: marc weber ]
 
vijaya saradhi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot marc.Atlast I found good explaination here.I was looking for this kind of detailed answer for the whole night.I was practising threads.


Now I have realized that using jad(java decompiler) we can explore more for these kind of doubts.


But ultimately an expert like you can only explain .

Thanks again...
 
reply
    Bookmark Topic Watch Topic
  • New Topic