标签:多线程 synchronized 并发 static
如果用synchronized修饰一个类成员方法A,那么就不会出现下面的情况:package thread;
public class SyncTest implements Runnable {
    
    public synchronized void m1() throws Exception{
        System.out.println("m1开始");
        Thread.sleep(2000);
        System.out.println("m1结束");
    }
    
    public  void m2() throws Exception {
        System.out.println("m2开始");
        Thread.sleep(2000);
        System.out.println("m2结束");
    }
    
    public void run() {
        try {
            m1();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) throws Exception {
        SyncTest tt = new SyncTest();
        Thread t = new Thread(tt);
        t.start();
        Thread.sleep(1000); //保证先运行m1
        tt.m2();
    }
}大家可以先猜一下运行结果/**
 * @author Jack Zhang
 * @version vb1.0
 * @Email virgoboy2004@163.com
 * @Date 2012-5-20
 */
public class Test
{
    public static synchronized void staticX() throws InterruptedException
    {
        for (int i = 0; i < 10; i++){
            Thread.sleep(1000);
            System.out.println("staticX.......................");
        }
    }
    public synchronized void x() throws InterruptedException{
        for (int i = 0; i < 10; i++){
            Thread.sleep(1000);
            System.out.println("x.......................");
        }
    }
    public static void main(String[] args)
    {
        final Test test1 = new Test();
        Thread thread = new Thread(new Runnable(){
            public void run(){
                try{
                    test1.x();
                }catch (InterruptedException e){
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, "a");
        Thread thread1 = new Thread(new Runnable(){
            public void run(){
                try{
                    Test.staticX();
                } catch (InterruptedException e){
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, "b");
        thread1.start();
        thread.start();
    }
}/**
 * @author Jack Zhang
 * @version vb1.0
 * @Email virgoboy2004@163.com
 * @Date 2012-5-20
 */
public class Test
{
    public final static Byte[] locks = new Byte[0];
    public static void staticX() throws InterruptedException
    {
        synchronized (locks)
        {
            for (int i = 0; i < 10; i++)
            {
                Thread.sleep(1000);
                System.out.println("staticX.......................");
            }
        }
    }
    public void x() throws InterruptedException
    {
        synchronized (locks)
        {
            for (int i = 0; i < 10; i++)
            {
                Thread.sleep(1000);
                System.out.println("x.......................");
            }
        }
    }
    public static void main(String[] args)
    {
        final Test test1 = new Test();
        final Test test2 = new Test();
        Thread thread = new Thread(new Runnable()
        {
            public void run()
            {
                try
                {
                    test1.x();
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, "a");
        Thread thread1 = new Thread(new Runnable()
        {
            public void run()
            {
                try
                {
                    Test.staticX();
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }, "b");
        thread1.start();
        thread.start();
    }
}结果标签:多线程 synchronized 并发 static
原文地址:http://blog.csdn.net/dlf123321/article/details/42373451