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  /**
17   * Measure throughput without any locking policy
18   * 
19   * @author Joern Huxhorn
20   * @author Ceki Gulcu
21   */
22  public class NoLockingInJava implements Runnable {
23  
24      static int THREAD_COUNT = 5;
25      static Object LOCK = new Object();
26      static NoLockingInJava[] RUNNABLE_ARRAY = new NoLockingInJava[THREAD_COUNT];
27      static Thread[] THREAD_ARRAY = new Thread[THREAD_COUNT];
28  
29      private int counter = 0;
30      private boolean done = false;
31  
32      public static void main(String args[]) throws InterruptedException {
33          printEnvironmentInfo();
34          execute();
35          printResults();
36      }
37  
38      public static void printEnvironmentInfo() {
39          System.out.println("java.runtime.version = " + System.getProperty("java.runtime.version"));
40          System.out.println("java.vendor          = " + System.getProperty("java.vendor"));
41          System.out.println("java.version         = " + System.getProperty("java.version"));
42          System.out.println("os.name              = " + System.getProperty("os.name"));
43          System.out.println("os.version           = " + System.getProperty("os.version"));
44      }
45  
46      public static void execute() throws InterruptedException {
47          for (int i = 0; i < THREAD_COUNT; i++) {
48              RUNNABLE_ARRAY[i] = new NoLockingInJava();
49              THREAD_ARRAY[i] = new Thread(RUNNABLE_ARRAY[i]);
50          }
51          for (Thread t : THREAD_ARRAY) {
52              t.start();
53          }
54          // let the threads run for a while
55          Thread.sleep(10000);
56  
57          for (int i = THREAD_COUNT - 1; i <= 0; i--) {
58              RUNNABLE_ARRAY[i].done = true;
59          }
60  
61      }
62  
63      public static void printResults() {
64          for (int i = 0; i < RUNNABLE_ARRAY.length; i++) {
65              System.out.println("runnable[" + i + "]: " + RUNNABLE_ARRAY[i]);
66          }
67      }
68  
69      public void run() {
70          for (;;) {
71              counter++;
72              if (done) {
73                  return;
74              }
75          }
76      }
77  
78      public String toString() {
79          return "counter=" + counter;
80      }
81  
82  }