package com.fqc.concurrency;


import org.apache.commons.logging.LogFactory;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
import org.apache.commons.logging.Log;
import org.apache.log4j.lf5.viewer.LogFactor5Dialog;

/**
 * com.fqc.concurrency
 *
 * @author fqc
 * @date 16/3/16
 */
public class SyncThread implements  Runnable{
    static {
        BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%m%n")));
    }

    public static final Log LOGGER = LogFactory.getLog(SyncThread.class);

    private Integer value;
    private static Integer NOWVALUE;

    public SyncThread(Integer value) {
        this.value = value;
    }

    public SyncThread() {

    }

    /**
     * 对实例对象的value值进行检查
     * public synchronized void doSomething 【脏读问题】
     * public synchronized static void doSomething
     */
    public  void doSomething(String thread_name) {
        synchronized (this.getClass()) {
            NOWVALUE = this.value;
            LOGGER.info(NOWVALUE + "---" + thread_name);
        }
    }

    @Override
    public void run() {
        Thread t0 = Thread.currentThread();
        String thread_name = t0.getName();
        this.doSomething(thread_name);
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new SyncThread(1),"t1");
        Thread t2 = new Thread(new SyncThread(100),"t2");
        t1.start();
        t2.start();
    }

}

@@ -600,24 +600,46 @@
 teger value;
+ //%E5%A6%82%E6%9E%9C%E8%AF%A5%E5%80%BC%E4%B8%BAstatic%E5%88%99%E5%B0%86%E8%A2%AB%E6%9C%80%E5%90%8E%E8%80%85%E8%A6%86%E7%9B%96
 %0A    private

add static condition to variable