This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes another thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "another thread question" Watch "another thread question" New topic
Author

another thread question

arie zwijnsma
Greenhorn

Joined: Jul 28, 2001
Posts: 5

suppose 2 threads are concurrently using the same integer m_i.
as in m_i++; or if ( m_i == 1 ) or j = m_i%2==0?m_i:-1
i am wondering are these statements thread safe 'on their own' ???
i mean is it possible that the os switches to the 2nd thread when
the 1st thread has not yet finished the statement it was working on ?
(see also code below)
thanks arie.
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
volatile.??!!!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Arie,
I would say that the statements ARE NOT thread safe because it is quite possible for a time-sharing thread handler to remove one thread when its' time is up and it has not completely finsihed the statement. Since thread handling is up to each vendor you can never be completely sure what will happen to threads.
Regards,
Manfred.
 
jQuery in Action, 2nd edition
 
subject: another thread question