//Sanctify them in your truth. Your word is truth. (John 17:17) package com.javarush.task.task17.task1717; /* ÐиÑнÑÑ ÑÐ¸Ð½Ñ ÑонизаÑÐ¸Ñ */ public class Solution { char[] value; int count; public Solution append(CharSequence s) { // synchronized (Solution.class) { if (s == null) { // synchronized (this) { s = "null"; // } } if (s instanceof String) { // synchronized (this) { return this.append((String) s); // } } if (s instanceof Solution) { // synchronized (this) { return this.appendThis((Solution) s); // } } // } return this.append(s); } public synchronized Solution appendThis(Solution s) { //do something here.... return this; } private static final java.io.ObjectStreamField[] serialPersistentFields = { new java.io.ObjectStreamField("value", char[].class), new java.io.ObjectStreamField("count", Integer.TYPE), new java.io.ObjectStreamField("shared", Boolean.TYPE), }; private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { java.io.ObjectOutputStream.PutField fields = s.putFields(); // synchronized (fields) { fields.put("value", value); fields.put("count", count); fields.put("shared", false); // } // synchronized (s) { s.writeFields(); // } } private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // synchronized (new java.io.IOException()) { java.io.ObjectInputStream.GetField fields = s.readFields(); value = (char[]) fields.get("value", null); count = fields.get("count", 0); // } } public static void main(String[] args) { } } /* ÐиÑнÑÑ ÑÐ¸Ð½Ñ ÑонизаÑÐ¸Ñ synchronized ÑÑÑеÑÑвенно замедлÑÐµÑ Ð¿ÑогÑаммÑ, поÑÑÐ¾Ð¼Ñ ÑбеÑи избÑÑоÑноÑÑÑ synchronized внÑÑÑи меÑодов ТÑебованиÑ: 1.âÐеÑод append(CharSequence s) клаÑÑа Solution не должен бÑÑÑ synchronized. 2.âÐеÑод appendThis(Solution s) клаÑÑа Solution должен бÑÑÑ synchronized. 3.âÐеÑод writeObject(java.io.ObjectOutputStream s) клаÑÑа Solution должен бÑÑÑ synchronized. 4.âÐеÑод readObject(java.io.ObjectInputStream s) клаÑÑа Solution не должен бÑÑÑ synchronized. 5.âÐеÑод main(String[] args) клаÑÑа Solution не должен бÑÑÑ synchronized. 6.âРклаÑÑе Solution не должно бÑÑÑ synchronized блоков. package com.javarush.task.task17.task1717; /* ÐиÑнÑÑ ÑÐ¸Ð½Ñ ÑонизаÑÐ¸Ñ */ public class Solution { char[] value; int count; public Solution append(CharSequence s) { synchronized (Solution.class) { if (s == null) { synchronized (this) { s = "null"; } } if (s instanceof String) { synchronized (this) { return this.append((String) s); } } if (s instanceof Solution) { synchronized (this) { return this.appendThis((Solution) s); } } } return this.append(s); } public synchronized Solution appendThis(Solution s) { //do something here.... return this; } private static final java.io.ObjectStreamField[] serialPersistentFields = { new java.io.ObjectStreamField("value", char[].class), new java.io.ObjectStreamField("count", Integer.TYPE), new java.io.ObjectStreamField("shared", Boolean.TYPE), }; private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { java.io.ObjectOutputStream.PutField fields = s.putFields(); synchronized (fields) { fields.put("value", value); fields.put("count", count); fields.put("shared", false); } synchronized (s) { s.writeFields(); } } private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { synchronized (new java.io.IOException()) { java.io.ObjectInputStream.GetField fields = s.readFields(); value = (char[]) fields.get("value", null); count = fields.get("count", 0); } } public static void main(String[] args) { } } */