pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/mil-lion/HelloJavaSE/commit/51b2d92461b59d22fc62d423f30a295e01010a3c

href="https://github.githubassets.com/assets/global-d18f184ea1a06a2c.css" /> Доработаны примеры по потокам (Thread) · mil-lion/HelloJavaSE@51b2d92 · GitHub
Skip to content

Commit 51b2d92

Browse files
committed
Доработаны примеры по потокам (Thread)
1 parent 16033ea commit 51b2d92

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/main/java/ru/lionsoft/javase/hello/thread/HelloSemaphore.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff 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
* Семафоры отлично подходят для решения задач, где надо ограничивать

src/main/java/ru/lionsoft/javase/hello/thread/MySingleton.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
*/
1515
public class MySingleton {
1616

17+
static {
18+
System.out.println("@@@@ MySingleton Class Loaded!!!");
19+
}
20+
1721
private MySingleton() {
1822
System.out.println("@@@@ MySingleton Created!!!");
1923
}

src/main/java/ru/lionsoft/javase/hello/thread/MySingletonLazy.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
*/
1515
public 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
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy