synchronized 分析-模拟静态方法加synchronized的情况

public class TestSynchronizeKey {

    public static void main(String[] args) {
        LastWater water = new LastWater();
        for(int i = 0 ; i < 3000; i ++)
        {
            Thread thread = new Thread(water, "thread-"+i+":");
            thread.start();
        }
    }
}


class LastWater implements Runnable
{
    private SimulateClass simulateClass  = new SimulateClass();
    
    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            Thread.sleep(3);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        countThreads();
    }
    
    public void countThreads()
    {
        simulateClass.addCount();
        Helper.println(simulateClass.getCount());
    }
    
}

class SimulateClass
{
    private String className;
    private String[] methodsName;
    private int count = 0;// simulate the global variable in LastWater
    
    public  synchronized  void addCount()
    {
        this.count ++;
    }
    
    public synchronized  int getCount()
    {
        return count;
    }
}

posted @ 2013-12-18 11:21  nosmall  阅读(207)  评论(0编辑  收藏  举报