This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
New to threads. I am not sure if my solution is the simplest/correct. It compiles and runs. If there is a better/neater way to do this, please let me know.
Here is the question, page 738 :
In this exercise we will attempt to synchronize a block of code. Within that block of code we will get the lock on an object, so that other threads cannot modify it while the block of code is executing. We will be creating three threads that will all attempt to manipulate the same object. Each thread will output a single letter 100 times, and then increment that letter by one. The object we will be using is StringBuffer. We could synchronize on a String object, but strings cannot be modified once they are created, so we would not be able to increment the letter without generating a new String object. The final output should have 100 As, 100 Bs, and 100 Cs all in unbroken lines.
Your code is achieving the desired output but its not correct. You need to synchronize an instance method. Basically you need to get a lock on the object which is being manipulated. How about moving the modifyStringBuff method to Job class (without any parameters) and making it an instance method...