Let's count

  • 9
  • Locked
1. Make it so that the result is calculated for all the array elements, WITHOUT using Thread.sleep in the main(String[] args) method. 2. Correct the synchronized block so that the values array is filled with 1s
You can't complete this task, because you're not signed in.
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Alaskian7134
Level 22 , Iasi, Romania
23 August 2021, 17:59
this task makes absolutely no sense. they ask you to correct the code so the array is filled with '1's but the "right solution" doesn't do that.
Willi Ehrmann
Level 32 , Germany
2 March, 22:02
I think they mean the array from 1-100 which gets checked in the main method. Index[0] will be 0 because the first thread uses incrementCount(); first on the count variable which is 0 at that moment and only then will increment the Index[1] of the array with values[getCount()]++. You have to look carefully i overlooked that at first too and was wondering why no array out of bounds exception is thrown when the 2. thread starts. But the array got initialized at new int[105] with 105 places and because we have a do while loop in the run method thread 2, 3, 4 will each increment once the index[101],[102] and [103] before stopping due to the while condition. Index[104] will stay 0 because no thread touches it.
Jen P
Level 26
4 August 2019, 22:25
Help! I don't understand why I should synchronized `values` but not syncrhonized `Solution.values` ? `values` is a static variable in class Solution and all counters are actually accessing the same `Solution.values`, then why the answer is just `values` ? Thank you!!
Sela
Level 20 , Poland
3 August 2020, 06:00
this is the same. although Solution.values is legal we are calling it from within a Solution class so we are in the scope of this variable. using Solution.values instead of values is superfluous.