Skip to content

Instantly share code, notes, and snippets.

@smartcatdev
Created December 16, 2015 18:07
Show Gist options
  • Save smartcatdev/cfb69c626b69f97a2159 to your computer and use it in GitHub Desktop.
Save smartcatdev/cfb69c626b69f97a2159 to your computer and use it in GitHub Desktop.
FuneralTech
For 1 : Objects do not get garbaged with the forced System.gc() and the System.runFinalization() because there is inner class Action instance inside the SomeSubscriber class. This instance of the inner class contains a hidden reference to its parent instance SomeSubscriber. The inner class Action will prevent the associated instances of the SomeSubscriber from being garbage collected.
For 2 : A call to methodB will hold the lock on the object sync, so no other threads are allowed to enter and synchronized block with this lock. Now, methodB calls methodA which also holds a lock on sync object. Method A will wait for methodA to complete and frees the lock which never happens. This causes a deadlock.
For 3 : Same thing as for (2) methodB calls isDepleted and these two methods hold a lock on this.processing. They will be waiting for each others.
For 4 :
class MyClass {
private final List<String> collection = new ArrayList<>();
public void addString(String stringToAdd){
synchronized(this.collection){
collection.add(stringToAdd);
}
}
@Override
public String toString() {
synchronized(this.collection){
return collection.toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment