Skip to content
Advertisement

Two threads prints one character of a string twice, one by one?

I have troubles with the following task:

Both threads access the object of the class Print and print in reverse order the value of the variable type String, letter by letter, where each letter is printed with a hyphen (–). Example : Input = NAME. Output = E-E-M-M-A-A-N-N.

What I’ve done is the following:

JavaScript

And it prints usually E-M-A-N-E-M-A-N, but it can be different. How do I make sure that it always prints the desired result? Thank you very much

Advertisement

Answer

As I mentioned in the comment, you need CyclicBarrier, which will coordinate the two threads and not let one or the other run ahead.

However, if you don’t want the last - character printed, there needs to be another coordination between the two threads for i == 0. One thread would print the last letter plus one -, then notify the other thread to go ahead and print just the last letter. This can be done with an atomic variable and a CountDownLatch. Who wins the update on the atomic variable will print letter-dash and count down the latch, the loser will wait on the latch and print the letter.

JavaScript

Note that this setup works only for 2 threads.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement