• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Racecondition example in SCJD book

 
Ronald Wouters
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
on page 101 of the SCJD book there is the following :


No matter what the implementation of the run method of these two threads, in my opinion the above lines of code wrongly give the impression or might make someone think that it makes a difference in what order the two threads are started. The start() method only makes a thread eligable to run. It is still up to the Thread Scheduler to decide which thread will actually gets to run first.
This means that even though say smith.start() is called before johnson.start() it might still be the case, depending on the thread scheduler, that actually johnson will be in the running state first.
So, should the "if" still be there in this example or can it be removed ?
I think it is the latter.

All comments welcome...
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ronald,

You are correct that it is up to the scheduler to decide which order the threads would run in, however the point of listing 4-9 was to produce an demonstratable example of race conditions occuring - this is something that is very hard to demonstrate when just relying on the JVM. I would not want to remove the "if statement" as without it I cannot guarantee that I can reproduce the problem for the book, or that somebody reading along would be able to reproduce it for themselves.

Hmmm.

Perhaps I should add a sentence in the eratta stating that even without that "if statement" there is still no guarantee of which thread will run first.

I would be interested in hearing other people's opinions on this as well.

Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic