Superfluous synchronization

  • 9
  • Locked
The synchronized keyword significantly slows a program, so remove unnecessary synchronized statements in methods.
You can't complete this task, because you're not signed in.
Comments (6)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Justin Smith
Level 38 , Greenfield, USA, United States
27 September 2021, 01:51
This is kind of a weird task because you don't have to know what any of the code does (there's even a sneaky "do something here..." that you can ignore completely). All you have to do is follow the conditions to the letter.
Angel Stefan
Level 23 , Sibiu, Romania
30 January 2022, 15:15
This task have no sense at all for me, at least not now. All i did it was to follow the Requirements.
Олег Байбула
Level 32 , Ukraine
Expert
4 February, 05:24
I think this was made to teach a student to remember the place of synchronised keyword in the signature.
Ewerton Backend Developer
3 July 2019, 18:14
Why shouldn't this part be synchronized:
public Solution append(CharSequence s) {
	if (s == null) {
		s = "null";
	}
	if (s instanceof String) {
		return this.append((String) s);
	}
	if (s instanceof Solution) {
		return this.appendThis((Solution) s);
	}
	return this.append(s);
}
What if some thread put a value in s, while another just checked it as a null string?
Sela
Level 20 , Poland
3 August 2020, 03:58
CharSequence always is a String therefore this method is an infinite loop calling itself recursively in second if block and modifies nothing especially not a shared resources. even if third if statement was somehow reached then CharSequence would never be a Solution because it neither implements this interface nor extends other class which implements it. other words boolean expression of third if
s instanceof Solution
always falls to false.
DarthGizka
Level 24 , Wittenberg, Germany
6 June 2021, 11:25
In addition to what Sela said, the only object accesses in this function are delegating calls; there is never a situation where multiple accesses occur in sequence (and hence might require synchronisation).