File tree Expand file tree Collapse file tree
src/main/java/ru/lionsoft/javase/hello/thread Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -100,11 +100,12 @@ public static void main(String[] args) throws InterruptedException {
100100
101101 Semaphore sem = new Semaphore (1 ); // 1 разрешение
102102 CommonResource res = new CommonResource ();
103- for (int i = 0 ; i < 5 ; i ++) {
104- new Thread (new CountThread (res , sem ), "Thread #" + i ).start ();
105- }
106103
107- Thread .sleep (5000 );
104+ Thread [] threades = new Thread [5 ];
105+ for (int i = 0 ; i < threades .length ; i ++) {
106+ threades [i ] = new Thread (new CountThread (res , sem ), "Thread #" + i );
107+ }
108+ ThreadUtil .startAndJoinThreads (threades );
108109
109110 /*
110111 * Семафоры отлично подходят для решения задач, где надо ограничивать
Original file line number Diff line number Diff line change 1414 */
1515public class MySingleton {
1616
17+ static {
18+ System .out .println ("@@@@ MySingleton Class Loaded!!!" );
19+ }
20+
1721 private MySingleton () {
1822 System .out .println ("@@@@ MySingleton Created!!!" );
1923 }
Original file line number Diff line number Diff line change 1414 */
1515public class MySingletonLazy {
1616
17+ static {
18+ System .out .println ("@@@@ MySingletonLazy Class Loaded!!!" );
19+ }
20+
1721 private MySingletonLazy () {
1822 System .out .println ("@@@@ MySingletonLazy Created!!!" );
1923 }
@@ -32,6 +36,20 @@ private MySingletonLazy() {
3236 return instance ;
3337 }
3438
39+ // Way 2
40+ public static MySingletonLazy getInstance2 () {
41+ if (instance == null ) {
42+ newInstance ();
43+ }
44+ return instance ;
45+ }
46+
47+ private static synchronized void newInstance () {
48+ if (instance == null ) {
49+ instance = new MySingletonLazy ();
50+ }
51+ }
52+
3553 public static void setUp () {
3654 System .out .println ("@@@@ MySingletonLazy.setUp()" );
3755 }
You can’t perform that action at this time.
0 commit comments