11/*
2- * The MIT License
3- * Copyright © 2014-2021 Ilkka Seppälä
2+ *The MIT License
3+ *Copyright © 2014-2021 Ilkka Seppälä
44 *
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:
1111 *
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.
1414 *
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.
2222 */
2323
2424package com .iluwatar .monitor ;
2525
26- import java .util .* ;
26+ import java .security . SecureRandom ;
2727import java .util .concurrent .ExecutorService ;
2828import java .util .concurrent .Executors ;
29- import java . util . logging . Logger ;
29+ import lombok . extern . slf4j . Slf4j ;
3030
3131/**
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.
3333 *
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.
3636 *
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.
3838 */
39-
39+ @ Slf4j
4040public class Main {
4141
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 );
5971 }
60- }
72+ }
73+ }
0 commit comments