001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.issue;
015
016/**
017 * Measure throughput without any locking policy
018 * 
019 * @author Joern Huxhorn
020 * @author Ceki Gulcu
021 */
022public class NoLockingInJava implements Runnable {
023
024    static int THREAD_COUNT = 5;
025    static Object LOCK = new Object();
026    static NoLockingInJava[] RUNNABLE_ARRAY = new NoLockingInJava[THREAD_COUNT];
027    static Thread[] THREAD_ARRAY = new Thread[THREAD_COUNT];
028
029    private int counter = 0;
030    private boolean done = false;
031
032    public static void main(String args[]) throws InterruptedException {
033        printEnvironmentInfo();
034        execute();
035        printResults();
036    }
037
038    public static void printEnvironmentInfo() {
039        System.out.println("java.runtime.version = " + System.getProperty("java.runtime.version"));
040        System.out.println("java.vendor          = " + System.getProperty("java.vendor"));
041        System.out.println("java.version         = " + System.getProperty("java.version"));
042        System.out.println("os.name              = " + System.getProperty("os.name"));
043        System.out.println("os.version           = " + System.getProperty("os.version"));
044    }
045
046    public static void execute() throws InterruptedException {
047        for (int i = 0; i < THREAD_COUNT; i++) {
048            RUNNABLE_ARRAY[i] = new NoLockingInJava();
049            THREAD_ARRAY[i] = new Thread(RUNNABLE_ARRAY[i]);
050        }
051        for (Thread t : THREAD_ARRAY) {
052            t.start();
053        }
054        // let the threads run for a while
055        Thread.sleep(10000);
056
057        for (int i = THREAD_COUNT - 1; i <= 0; i--) {
058            RUNNABLE_ARRAY[i].done = true;
059        }
060
061    }
062
063    public static void printResults() {
064        for (int i = 0; i < RUNNABLE_ARRAY.length; i++) {
065            System.out.println("runnable[" + i + "]: " + RUNNABLE_ARRAY[i]);
066        }
067    }
068
069    public void run() {
070        for (;;) {
071            counter++;
072            if (done) {
073                return;
074            }
075        }
076    }
077
078    public String toString() {
079        return "counter=" + counter;
080    }
081
082}