View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.issue;
15  
16  import java.util.concurrent.locks.Lock;
17  import java.util.concurrent.locks.ReentrantLock;
18  
19  /**
20   * Example code illustrating locking policies in the JDK. See
21   * http://jira.qos.ch/browse/LBCORE-97 for a discussion.
22   * 
23   * @author Joern Huxhorn
24   */
25  public class LBCORE97 {
26  
27      static int THREAD_COUNT = 10;
28  
29      public static void main(String args[]) throws InterruptedException {
30  
31          System.out.println("Environment:");
32          System.out.println("java.runtime.name    = " + System.getProperty("java.runtime.name"));
33          System.out.println("java.runtime.version = " + System.getProperty("java.runtime.version"));
34          System.out.println("java.vendor          = " + System.getProperty("java.vendor"));
35          System.out.println("java.version         = " + System.getProperty("java.version"));
36          System.out.println("java.vm.name         = " + System.getProperty("java.vm.name"));
37          System.out.println("java.vm.info         = " + System.getProperty("java.vm.info"));
38  
39          System.out.println("os.name              = " + System.getProperty("os.name"));
40          System.out.println("os.version           = " + System.getProperty("os.version"));
41          System.out.println("os.arch              = " + System.getProperty("os.arch"));
42          System.out.println("##########################################");
43  
44          usingSynchronized(THREAD_COUNT);
45          usingUnfairLock(THREAD_COUNT);
46          usingFairLock(THREAD_COUNT);
47      }
48  
49      public static void execute(String text, Thread[] threads) throws InterruptedException {
50          System.out.println("About to execute " + text + "...");
51          int threadCount = threads.length;
52          for (int i = 0; i < threadCount; i++) {
53              threads[i].start();
54          }
55  
56          Thread.sleep(10000);
57  
58          for (int i = 0; i < threadCount; i++) {
59              threads[i].interrupt();
60          }
61          Thread.sleep(1000); // wait a moment for termination, to lazy for join ;)
62      }
63  
64      public static void print(String text, Runnable[] runnables) {
65          System.out.println("Results for " + text + ":");
66          for (int i = 0; i < runnables.length; i++) {
67              System.out.println("runnables[" + i + "]: " + runnables[i]);
68          }
69          System.out.println("##########################################");
70      }
71  
72      public static void usingSynchronized(int threadCount) throws InterruptedException {
73          Object lockObject = new Object();
74          Runnable[] runnables = new Runnable[threadCount];
75          Thread[] threads = new Thread[threadCount];
76  
77          for (int i = 0; i < threadCount; i++) {
78              runnables[i] = new SynchronizedRunnable(lockObject);
79              threads[i] = new Thread(runnables[i]);
80          }
81          String text = "usingSynchronized";
82          execute(text, threads);
83          print(text, runnables);
84      }
85  
86      public static void usingUnfairLock(int threadCount) throws InterruptedException {
87          Lock lock = new ReentrantLock();
88          Runnable[] runnables = new Runnable[threadCount];
89          Thread[] threads = new Thread[threadCount];
90  
91          for (int i = 0; i < threadCount; i++) {
92              runnables[i] = new LockRunnable(lock);
93              threads[i] = new Thread(runnables[i]);
94          }
95  
96          String text = "usingUnfairLock";
97          execute(text, threads);
98          print(text, runnables);
99      }
100 
101     public static void usingFairLock(int threadCount) throws InterruptedException {
102         Lock lock = new ReentrantLock(true);
103         Runnable[] runnables = new Runnable[threadCount];
104         Thread[] threads = new Thread[threadCount];
105 
106         for (int i = 0; i < threadCount; i++) {
107             runnables[i] = new LockRunnable(lock);
108             threads[i] = new Thread(runnables[i]);
109         }
110 
111         String text = "usingFairLock";
112         execute(text, threads);
113         print(text, runnables);
114     }
115 
116     public static class SynchronizedRunnable implements Runnable {
117         private final Object lockObject;
118         private int counter;
119         private boolean running;
120 
121         public SynchronizedRunnable(Object lockObject) {
122             this.lockObject = lockObject;
123             this.counter = 0;
124             this.running = false;
125         }
126 
127         public void run() {
128             running = true;
129             for (;;) {
130                 synchronized (lockObject) {
131                     counter++;
132                     try {
133                         Thread.sleep(10);
134                     } catch (InterruptedException ex) {
135                         break;
136                     }
137                 }
138             }
139             running = false;
140         }
141 
142         public String toString() {
143             return "SynchronizedRunnable[counter=" + counter + ", running=" + running + "]";
144         }
145     }
146 
147     public static class LockRunnable implements Runnable {
148         private final Lock lock;
149         private int counter;
150         private boolean running;
151 
152         public LockRunnable(Lock lock) {
153             this.lock = lock;
154             this.counter = 0;
155             this.running = false;
156         }
157 
158         public void run() {
159             running = true;
160             for (;;) {
161                 lock.lock();
162                 try {
163                     counter++;
164                     Thread.sleep(10);
165                 } catch (InterruptedException ex) {
166                     break;
167                 } finally {
168                     lock.unlock();
169                 }
170             }
171             running = false;
172         }
173 
174         public String toString() {
175             return "LockRunnable[counter=" + counter + ", running=" + running + "]";
176         }
177     }
178 }