Synchronizing a Block of Code
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.
1. Create a class and extend the Thread class.
2. Override the run() method of Thread. This is where the synchronized
block of code will go.
3. For our three thread objects to share the same object, we will need to create
a constructor that accepts a StringBuffer object in the argument.
4. The synchronized block of code will obtain a lock on the StringBuffer
object from step 3.
5. Within the block, output the StringBuffer 100 times and then increment
the letter in the StringBuffer. You can check Chapter 6 for StringBuffer
methods that will help with this.
6. Finally, in the main() method, create a single StringBuffer object using the
letter A, then create three instances of our class and start all three of them.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan wrote:It's OK, but, what about the output? It seems to be like, there may be 100 time C first, and then 100 times B, and then 100 time A.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Thread ONE (0) = A
Thread ONE (1) = A
Thread ONE (2) = A
Thread ONE (3) = A
Thread ONE (4) = A
Thread ONE (5) = A
Thread ONE (6) = A
Thread ONE (7) = A
Thread ONE (8) = A
Thread ONE (9) = A
Thread ONE (10) = A
Thread ONE (11) = A
Thread ONE (12) = A
Thread ONE (13) = A
Thread ONE (14) = A
Thread ONE (15) = A
Thread ONE (16) = A
Thread ONE (17) = A
Thread ONE (18) = A
Thread ONE (19) = A
Thread THREE (0) = B
Thread THREE (1) = B
Thread THREE (2) = B
Thread THREE (3) = B
Thread THREE (4) = B
Thread THREE (5) = B
Thread THREE (6) = B
Thread THREE (7) = B
Thread THREE (8) = B
Thread THREE (9) = B
Thread THREE (10) = B
Thread THREE (11) = B
Thread THREE (12) = B
Thread THREE (13) = B
Thread THREE (14) = B
Thread THREE (15) = B
Thread THREE (16) = B
Thread THREE (17) = B
Thread THREE (18) = B
Thread THREE (19) = B
Thread TWO (0) = C
Thread TWO (1) = C
Thread TWO (2) = C
Thread TWO (3) = C
Thread TWO (4) = C
Thread TWO (5) = C
Thread TWO (6) = C
Thread TWO (7) = C
Thread TWO (8) = C
Thread TWO (9) = C
Thread TWO (10) = C
Thread TWO (11) = C
Thread TWO (12) = C
Thread TWO (13) = C
Thread TWO (14) = C
Thread TWO (15) = C
Thread TWO (16) = C
Thread TWO (17) = C
Thread TWO (18) = C
Thread TWO (19) = C
Regards,
Avishkar Nikale
Avishkar Nikale wrote:The question also does not state which thread to start first.
So the logic is to see a thread completes printing out a single character for its execution.
If you remove the synchronized block & x loops to a high value,
single character per thread will not be guaranteed.
Ankit Garg wrote:
I don't think the output can be in the order you are saying. There will be 100As then 100Bs then 100Cs. The thread execution order might change so thread TWO might print 100As then thread THREE might print 100Bs and Thread ONE 100Cs...
Abimaran Kugathasan wrote:........, And other combination is also there! Is it OK?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan wrote:It seems to be like, there may be 100 time C first, and then 100 times B, and then 100 time A.
Abimaran Kugathasan wrote:
Ankit Garg wrote:
There will be 100As then 100Bs then 100Cs. The thread execution order might change so thread TWO might print 100As then thread THREE might print 100Bs and Thread ONE 100Cs...
I mentioned about that already.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|