1
1
/*
2
- * The MIT License
3
- * Copyright © 2014-2021 Ilkka Seppälä
2
+ *The MIT License
3
+ *Copyright © 2014-2021 Ilkka Seppälä
4
4
*
5
- * Permission is hereby granted, free of charge, to any person obtaining a copy
6
- * of this software and associated documentation files (the "Software"), to deal
7
- * in the Software without restriction, including without limitation the rights
8
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- * copies of the Software, and to permit persons to whom the Software is
10
- * furnished to do so, subject to the following conditions:
5
+ *Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ *of this software and associated documentation files (the "Software"), to deal
7
+ *in the Software without restriction, including without limitation the rights
8
+ *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ *copies of the Software, and to permit persons to whom the Software is
10
+ *furnished to do so, subject to the following conditions:
11
11
*
12
- * The above copyright notice and this permission notice shall be included in
13
- * all copies or substantial portions of the Software.
12
+ *The above copyright notice and this permission notice shall be included in
13
+ *all copies or substantial portions of the Software.
14
14
*
15
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- * THE SOFTWARE.
15
+ *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ *THE SOFTWARE.
22
22
*/
23
23
24
24
package com .iluwatar .monitor ;
25
25
26
- import java .util .* ;
26
+ import java .security . SecureRandom ;
27
27
import java .util .concurrent .ExecutorService ;
28
28
import java .util .concurrent .Executors ;
29
- import java . util . logging . Logger ;
29
+ import lombok . extern . slf4j . Slf4j ;
30
30
31
31
/**
32
- * <p> The Monitor pattern is used in concurrent algorithms to achieve mutual exclusion.</p>
32
+ * The Monitor pattern is used in concurrent algorithms to achieve mutual exclusion.
33
33
*
34
- * <p>Bank is a simple class that transfers money from an account to another account using
35
- * {@link Bank#transfer}. It can also return the balance of the bank account stored in the bank.</p>
34
+ * <p>Bank is a simple class that transfers money from an account to another account using {@link
35
+ * Bank#transfer}. It can also return the balance of the bank account stored in the bank.
36
36
*
37
- * <p>Main class uses ThreadPool to run threads that do transactions on the bank accounts.</p>
37
+ * <p>Main class uses ThreadPool to run threads that do transactions on the bank accounts.
38
38
*/
39
-
39
+ @ Slf4j
40
40
public class Main {
41
41
42
- public static void main (String [] args ) {
43
- Logger logger = Logger .getLogger ("monitor" );
44
- var bank = new Bank (4 , 1000 , logger );
45
- Runnable runnable = () -> {
46
- try {
47
- Thread .sleep ((long ) (Math .random () * 1000 ));
48
- Random random = new Random ();
49
- for (int i = 0 ; i < 1000000 ; i ++)
50
- bank .transfer (random .nextInt (4 ), random .nextInt (4 ), (int ) (Math .random () * 1000 ));
51
- } catch (InterruptedException e ) {
52
- logger .info (e .getMessage ());
53
- }
54
- };
55
- ExecutorService executorService = Executors .newFixedThreadPool (5 );
56
- for (int i = 0 ; i < 5 ; i ++) {
57
- executorService .execute (runnable );
58
- }
42
+ /**
43
+ * Runner to perform a bunch of transfers and handle exception.
44
+ *
45
+ * @param bank bank object
46
+ */
47
+ public static void runner (Bank bank ) {
48
+ try {
49
+ SecureRandom random = new SecureRandom ();
50
+ Thread .sleep (random .nextInt (1000 ));
51
+ for (int i = 0 ; i < 1000000 ; i ++) {
52
+ bank .transfer (random .nextInt (4 ), random .nextInt (4 ), random .nextInt ());
53
+ }
54
+ } catch (InterruptedException e ) {
55
+ LOGGER .info (e .getMessage ());
56
+ Thread .currentThread ().interrupt ();
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Program entry point.
62
+ *
63
+ * @param args command line args
64
+ */
65
+ public static void main (String [] args ) {
66
+ var bank = new Bank (4 , 1000 );
67
+ Runnable runnable = () -> runner (bank );
68
+ ExecutorService executorService = Executors .newFixedThreadPool (5 );
69
+ for (int i = 0 ; i < 5 ; i ++) {
70
+ executorService .execute (runnable );
59
71
}
60
- }
72
+ }
73
+ }
0 commit comments