Hi guys,
I got 3 questions
1. Why is param a class variable? It doesn`t have the keyword static.
2.How do I know what is a shared ressource?
3. Why do need the methods 3,4,6 a synchronizsed?
Many thanks in advance!
Greetings
Fabian
Solution but I don't understand! - Java Core Level 7, Lesson 10, exercise synchronised methods
Under discussion
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Jcode
25 June 2022, 15:43
Really good questions Fabian.
Would be nice if someone could shed some light on them ... CodeGym?
0
Thomas
26 June 2022, 13:11
OK, here's a sample main method so that you can see how a Solution object can possibly used by threads.
You see that I have two Runnables and in total 4 threads. 2 threads always use the same Runnable and each Runnable uses the same Solution object s. Means s is used in four threads.
You know that you need to synchronize methods or resources if the resource is shared or a shared resource is used in a method.
If one of our threads accesses method1, then a new Solution object is created and just this new object is used. That's not a shared resource, that's a local object. No synchronization needed.
Same here, an argument is passed, each thread uses it's own primitive for that, an argument is similar to a local variable. No shared resource involved -> no synchronization needed. 0
Thomas
26 June 2022, 13:23
all of our four threads will access here the same resource param. It's an instance variable of our s object. All runnables use s, all threads use only runnables that use s and therefore all threads use the same s.param field. So we have a shared resource here and a shared resource means synchronization needed.
sb is a field of the s object that's shared among our threads. s is referencing a mutable object (StringBuilder) and that object isn't already synchronized -> we need to synchronize the method or at least the part where sb is accessed. If only immutable objects would be used we won't need to synchronize (immutable objects can not be modified -> no problems) or if the object is already synchronized we wouldn't need to either (like if a StringBuffer object would be used here).
That's already answered... we have no shared resource here, each thread creates it's own dedicated object and uses that
sb is answerded above. method7 call's return value isn't used so we don't need to look at this any further.
+1
Thomas
26 June 2022, 13:26
method7 is using a paramater (similar to a local variable). We answered that above already.
So we just have left method0
here method3 is called and as we have seen above we need to synchronize that method. Cause method3 is already synchronized we don't need to do that here again. That's all...
good luck
+1
Jcode
27 June 2022, 23:24
Thanks, Thomas for your input.
Interesting that a StringBuilder is not thread-safe and StringBuffer is thread-safe.
Multi-threading has so much potential to screw up your application that choosing a thread-unsafe Class in a multi-thread app is a risk not worth taking.
Here is an interesting video I found while researching threads 'Java's SimpleDateFormat is a Disaster Waiting to Happen'
https://www.youtube.com/watch?v=JdNQoTJmzis
0