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.classic.issue.lbclassic135.lbclassic139;
015
016import org.slf4j.Logger;
017
018import ch.qos.logback.classic.LoggerContext;
019import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
020
021/**
022 * 
023 * @author Olivier Cailloux
024 * 
025 */
026public class Worker extends RunnableWithCounterAndDone {
027    static final int SLEEP_DUIRATION = 50;
028
029    private Logger logger;
030    private final Object lock = new Object();
031
032    final LoggerContext loggerContext;
033
034    Worker(LoggerContext lc) {
035        loggerContext = lc;
036        logger = lc.getLogger(this.getClass());
037    }
038
039    public void run() {
040        print("entered run()");
041        while (!isDone()) {
042            synchronized (lock) {
043                sleep();
044                logger.info("lock the logger");
045            }
046        }
047        print("leaving run()");
048    }
049
050    @Override
051    public String toString() {
052        print("In Worker.toString() - about to access lock");
053        synchronized (lock) {
054            print("In Worker.toString() - got the lock");
055            // sleep();
056            return "STATUS";
057        }
058    }
059
060    public void sleep() {
061        try {
062            print("About to go to sleep");
063            Thread.sleep(SLEEP_DUIRATION);
064            print("just woke up");
065        } catch (InterruptedException exc) {
066            exc.printStackTrace();
067        }
068    }
069
070    void print(String msg) {
071        String thread = Thread.currentThread().getName();
072        System.out.println("[" + thread + "] " + msg);
073    }
074}