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/0532ba15a0d15d939f0831c2af9ec853ff039360

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

Commit 0532ba1

Browse files
committed
Доработаны примеры с потоками (Thread).
1 parent d7ebedf commit 0532ba1

5 files changed

Lines changed: 256 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* File: HelloSingleton.java
3+
* Project: HelloJavaSE
4+
* Date: 12 авг. 2020 г. 00:38:18
5+
* Author: Igor Morenko <morenko at lionsoft.ru>
6+
*
7+
* Copyright 2005-2019 LionSoft LLC. All rights reserved.
8+
*/
9+
package ru.lionsoft.javase.hello.thread;
10+
11+
/**
12+
*
13+
* @author Igor Morenko (emailto:imorenko@yandex.ru)
14+
*/
15+
public class HelloSingleton {
16+
17+
public static void main(String[] args) throws InterruptedException {
18+
19+
Thread[] threads = new Thread[10];
20+
21+
System.out.println("#### Test MySingleton ####");
22+
MySingleton.setUp();
23+
System.out.println("Test Thread:");
24+
for (int i = 0; i < threads.length; i++) {
25+
threads[i] = new Thread(new Runnable() {
26+
@Override
27+
public void run() {
28+
System.out.println(Thread.currentThread().getName() + ": " + MySingleton.getInstance());
29+
}
30+
}, "Thread #" + i);
31+
}
32+
ThreadUtil.startAndJoinThreads(threads);
33+
MySingleton mySingleton1 = MySingleton.getInstance();
34+
mySingleton1.business();
35+
MySingleton mySingleton2 = MySingleton.getInstance();
36+
System.out.println("mySingleton1 == mySingleton2 => " + (mySingleton1 == mySingleton2));
37+
38+
System.out.println("\n#### Test MySingletonLazy ####");
39+
MySingletonLazy.setUp();
40+
System.out.println("Test Thread:");
41+
for (int i = 0; i < threads.length; i++) {
42+
threads[i] = new Thread(new Runnable() {
43+
@Override
44+
public void run() {
45+
System.out.println(Thread.currentThread().getName() + ": " + MySingletonLazy.getInstance());
46+
}
47+
}, "Thread #" + i);
48+
}
49+
ThreadUtil.startAndJoinThreads(threads);
50+
MySingletonLazy mySingletonLazy1 = MySingletonLazy.getInstance();
51+
mySingletonLazy1.business();
52+
MySingletonLazy mySingletonLazy2 = MySingletonLazy.getInstance();
53+
System.out.println("mySingletonLazy1 == mySingletonLazy2 => " + (mySingletonLazy1 == mySingletonLazy2));
54+
55+
System.out.println("\n#### Test NetBeansSingleton ####");
56+
NetBeansSingleton.setUp();
57+
System.out.println("Test Thread:");
58+
for (int i = 0; i < threads.length; i++) {
59+
threads[i] = new Thread(new Runnable() {
60+
@Override
61+
public void run() {
62+
System.out.println(Thread.currentThread().getName() + ": " + NetBeansSingleton.getInstance());
63+
}
64+
}, "Thread #" + i);
65+
}
66+
ThreadUtil.startAndJoinThreads(threads);
67+
NetBeansSingleton netBeansSingleton1 = NetBeansSingleton.getInstance();
68+
netBeansSingleton1.business();
69+
NetBeansSingleton netBeansSingleton2 = NetBeansSingleton.getInstance();
70+
System.out.println("netBeansSingleton2 == netBeansSingleton2 => " + (netBeansSingleton1 == netBeansSingleton2));
71+
}
72+
73+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* File: HelloThreadLocalRandom.java
3+
* Project: HelloJavaSE
4+
* Date: 12 авг. 2020 г. 00:38:18
5+
* Author: Igor Morenko <morenko at lionsoft.ru>
6+
*
7+
* Copyright 2005-2019 LionSoft LLC. All rights reserved.
8+
*/
9+
package ru.lionsoft.javase.hello.thread;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Random;
14+
import java.util.concurrent.Callable;
15+
import java.util.concurrent.ExecutorService;
16+
import java.util.concurrent.Executors;
17+
import java.util.concurrent.ThreadLocalRandom;
18+
19+
/**
20+
*
21+
* @author Igor Morenko (emailto:imorenko@yandex.ru)
22+
*/
23+
public class HelloThreadLocalRandom {
24+
25+
public static void main(String[] args) throws InterruptedException {
26+
27+
int boundedRandomValue = ThreadLocalRandom.current().nextInt(0, 100);
28+
System.out.println("boundedRandomValue: " + boundedRandomValue);
29+
30+
long startTime = System.currentTimeMillis();
31+
testRandom();
32+
System.out.println("duration: " + (System.currentTimeMillis() - startTime) + " ms");
33+
34+
startTime = System.currentTimeMillis();
35+
testThreadLocalRandom();
36+
System.out.println("duration: " + (System.currentTimeMillis() - startTime) + " ms");
37+
}
38+
39+
private static void testRandom() throws InterruptedException {
40+
System.out.println("#### Test Random ####");
41+
ExecutorService executor = Executors.newWorkStealingPool();
42+
List<Callable<Integer>> callables = new ArrayList<>();
43+
Random random = new Random();
44+
for (int i = 0; i < 1000; i++) {
45+
callables.add(() -> {
46+
return random.nextInt();
47+
});
48+
}
49+
executor.invokeAll(callables);
50+
}
51+
52+
private static void testThreadLocalRandom() throws InterruptedException {
53+
System.out.println("#### Test ThreadLocalRandom ####");
54+
ExecutorService executor = Executors.newWorkStealingPool();
55+
List<Callable<Integer>> callables = new ArrayList<>();
56+
for (int i = 0; i < 1000; i++) {
57+
callables.add(() -> {
58+
return ThreadLocalRandom.current().nextInt();
59+
});
60+
}
61+
executor.invokeAll(callables);
62+
}
63+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* File: MySingleton.java
3+
* Project: HelloJavaSE
4+
* Date: 12 авг. 2020 г. 00:38:18
5+
* Author: Igor Morenko <morenko at lionsoft.ru>
6+
*
7+
* Copyright 2005-2019 LionSoft LLC. All rights reserved.
8+
*/
9+
package ru.lionsoft.javase.hello.thread;
10+
11+
/**
12+
*
13+
* @author Igor Morenko (emailto:imorenko@yandex.ru)
14+
*/
15+
public class MySingleton {
16+
17+
private MySingleton() {
18+
System.out.println("@@@@ MySingleton Created!!!");
19+
}
20+
21+
private static final MySingleton INSTANCE = new MySingleton();
22+
23+
public static MySingleton getInstance() {
24+
return INSTANCE;
25+
}
26+
27+
public static void setUp() {
28+
System.out.println("@@@@ MySingleton.setUp()");
29+
}
30+
31+
public void business() {
32+
System.out.println("@@@@ MySingleton.business()");
33+
}
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* File: MySingletonLazy.java
3+
* Project: HelloJavaSE
4+
* Date: 12 авг. 2020 г. 00:38:18
5+
* Author: Igor Morenko <morenko at lionsoft.ru>
6+
*
7+
* Copyright 2005-2019 LionSoft LLC. All rights reserved.
8+
*/
9+
package ru.lionsoft.javase.hello.thread;
10+
11+
/**
12+
*
13+
* @author Igor Morenko (emailto:imorenko@yandex.ru)
14+
*/
15+
public class MySingletonLazy {
16+
17+
private MySingletonLazy() {
18+
System.out.println("@@@@ MySingletonLazy Created!!!");
19+
}
20+
21+
private static MySingletonLazy instance = null;
22+
23+
// Lazy Load and Thread Safe
24+
public static /*!!!synchronized - излишняя блокировка метода!!!*/ MySingletonLazy getInstance() {
25+
if (instance == null) { // double check
26+
synchronized(MySingletonLazy.class) {
27+
if (instance == null) {
28+
instance = new MySingletonLazy();
29+
}
30+
}
31+
}
32+
return instance;
33+
}
34+
35+
public static void setUp() {
36+
System.out.println("@@@@ MySingletonLazy.setUp()");
37+
}
38+
39+
public void business() {
40+
System.out.println("@@@@ MySingletonLazy.business()");
41+
}
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* File: NetBeansSingleton.java
3+
* Project: HelloJavaSE
4+
* Date: 12 авг. 2020 г. 00:38:18
5+
* Author: Igor Morenko <morenko at lionsoft.ru>
6+
*
7+
* Copyright 2005-2019 LionSoft LLC. All rights reserved.
8+
*/
9+
package ru.lionsoft.javase.hello.thread;
10+
11+
/**
12+
*
13+
* @author Igor Morenko (emailto:imorenko@yandex.ru)
14+
*/
15+
public class NetBeansSingleton {
16+
17+
static {
18+
System.out.println("@@@@ NetBeansSingleton Class Loaded!!!");
19+
}
20+
21+
private NetBeansSingleton() {
22+
System.out.println("@@@@ NetBeansSingleton Created!!!");
23+
}
24+
25+
private static class NetBeansSingletonHolder {
26+
static {
27+
System.out.println("@@@@ NetBeansSingletonHolder Class Loaded!!!");
28+
}
29+
private static final NetBeansSingleton INSTANCE = new NetBeansSingleton();
30+
}
31+
32+
// Lazy Load and Thread Safe
33+
public static NetBeansSingleton getInstance() {
34+
return NetBeansSingletonHolder.INSTANCE;
35+
}
36+
37+
public static void setUp() {
38+
System.out.println("@@@@ NetBeansSingleton.setUp()");
39+
}
40+
41+
public void business() {
42+
System.out.println("@@@@ NetBeansSingleton.business()");
43+
}
44+
}

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