|
| 1 | + |
| 2 | +//His disciples said to him, "Behold, now you speak plainly, and speak no figures of speech. (John 16:29) |
| 3 | + |
| 4 | +---------------------------------------------Solution.java---------------------------------------------------- |
| 5 | +package com.javarush.task.task17.task1706; |
| 6 | + |
| 7 | +/* |
| 8 | +Синхронизированный президент |
| 9 | +*/ |
| 10 | + |
| 11 | +public class Solution { |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + OurPresident expectedPresident = OurPresident.getOurPresident(); |
| 15 | + OurPresident ourPresident = OurPresident.getOurPresident(); |
| 16 | + System.out.println(expectedPresident == ourPresident ); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +-------------------------------------------------------------------------------------------------------------- |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +--------------------------------------------OurPresident.java------------------------------------------------- |
| 25 | +package com.javarush.task.task17.task1706; |
| 26 | + |
| 27 | +public class OurPresident { |
| 28 | + private static OurPresident president; |
| 29 | + |
| 30 | + static{ |
| 31 | + synchronized(OurPresident.class) { |
| 32 | + president = new OurPresident(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private OurPresident() { |
| 37 | + } |
| 38 | + |
| 39 | + public static OurPresident getOurPresident() { |
| 40 | + return president; |
| 41 | + } |
| 42 | +} |
| 43 | +-------------------------------------------------------------------------------------------------------------- |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +/* |
| 51 | +Синхронизированный президент |
| 52 | +И снова Singleton паттерн — синхронизация в статическом блоке |
| 53 | +Внутри класса OurPresident в статическом блоке создайте синхронизированный блок. |
| 54 | +Внутри синхронизированного блока инициализируйте president. |
| 55 | +
|
| 56 | +
|
| 57 | +Требования: |
| 58 | +1. Класс OurPresident должен содержать приватную статическое поле OurPresident president. |
| 59 | +2. Класс OurPresident должен содержать публичный статический метод OurPresident getOurPresident(). |
| 60 | +3. Класс OurPresident должен содержать приватный конструктор. |
| 61 | +4. Класс OurPresident должен содержать статический блок. |
| 62 | +5. Внутри статического блока класса OurPresident должен быть синхронизированный блок. |
| 63 | +6. Внутри синхронизированного блока должно быть проинициализировано поле president. |
| 64 | +
|
| 65 | +package com.javarush.task.task17.task1706; |
| 66 | +
|
| 67 | +* |
| 68 | +Синхронизированный президент |
| 69 | +* |
| 70 | +
|
| 71 | +public class Solution { |
| 72 | + public static void main(String[] args) { |
| 73 | + OurPresident expectedPresident = OurPresident.getOurPresident(); |
| 74 | + OurPresident ourPresident = OurPresident.getOurPresident(); |
| 75 | + System.out.println(expectedPresident == ourPresident ); |
| 76 | + } |
| 77 | +} |
| 78 | +
|
| 79 | +
|
| 80 | +*/ |
0 commit comments