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 * Short sample code illustrating locking policies in the JDK. See
018 * http://jira.qos.ch/browse/LBCORE-97 for a discussion.
019 * 
020 * @author Joern Huxhorn
021 * @author Ceki Gulcu
022 */
023public class LockingInJava implements Runnable {
024
025    static int THREAD_COUNT = 5;
026    static Object LOCK = new Object();
027    static LockingInJava[] RUNNABLE_ARRAY = new LockingInJava[THREAD_COUNT];
028    static Thread[] THREAD_ARRAY = new Thread[THREAD_COUNT];
029
030    private int counter = 0;
031    private boolean done = false;
032
033    public static void main(String args[]) throws InterruptedException {
034        printEnvironmentInfo();
035        execute();
036        printResults();
037    }
038
039    public static void printEnvironmentInfo() {
040        System.out.println("java.runtime.version = " + System.getProperty("java.runtime.version"));
041        System.out.println("java.vendor          = " + System.getProperty("java.vendor"));
042        System.out.println("java.version         = " + System.getProperty("java.version"));
043        System.out.println("os.name              = " + System.getProperty("os.name"));
044        System.out.println("os.version           = " + System.getProperty("os.version"));
045    }
046
047    public static void execute() throws InterruptedException {
048        for (int i = 0; i < THREAD_COUNT; i++) {
049            RUNNABLE_ARRAY[i] = new LockingInJava();
050            THREAD_ARRAY[i] = new Thread(RUNNABLE_ARRAY[i]);
051        }
052        for (Thread t : THREAD_ARRAY) {
053            t.start();
054        }
055        // let the threads run for a while
056        Thread.sleep(10000);
057
058        for (int i = THREAD_COUNT - 1; i <= 0; i--) {
059            RUNNABLE_ARRAY[i].done = true;
060        }
061
062    }
063
064    public static void printResults() {
065        for (int i = 0; i < RUNNABLE_ARRAY.length; i++) {
066            System.out.println("runnable[" + i + "]: " + RUNNABLE_ARRAY[i]);
067        }
068    }
069
070    public void run() {
071        for (;;) {
072            synchronized (LOCK) {
073                counter++;
074                try {
075                    Thread.sleep(10);
076                } catch (InterruptedException ex) {
077                }
078                if (done) {
079                    return;
080                }
081            }
082        }
083    }
084
085    public String toString() {
086        return "counter=" + counter;
087    }
088
089}