synchronized实例解释

package synchronized_part;

/**
 * Created by zhangjiqiang on 2016/12/22.
 */
public class SynchroziedRange {


    public void normal() {
            int i = 5;
            while (i-- > 0) {
                System.out.println(Thread.currentThread().getName() + " : " + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                }
            }
    }


    public void test1() {
        synchronized (this) {
            int i = 5;
            while (i-- > 0) {
                System.out.println(Thread.currentThread().getName() + " : " + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                }
            }
        }
    }

    public synchronized void isSyncA() {
        int i = 5;
        while (i-- > 0) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
            }
        }
    }

    public synchronized void isSyncB() {
        int i = 5;
        while (i-- > 0) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
            }
        }
    }


    public static synchronized void cSyncA() {
        int i = 5;
        while (i-- > 0) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
            }
        }
    }

    public static synchronized void cSyncB() {
        int i = 5;
        while (i-- > 0) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
            }
        }
    }

    public static void main(String[] args) {
        final SynchroziedRange myt1 = new SynchroziedRange();
        final SynchroziedRange x = new SynchroziedRange();
        final SynchroziedRange y = new SynchroziedRange();
        //同一个实例,不同的synchronized方法,对象锁有约束(同一个对象——对象锁)——a. x.isSyncA()与x.isSyncB()
         /*Thread test1 = new Thread(  new Runnable() {  public void run() { x.isSyncA();  }  }, "test1"  );
         Thread test2 = new Thread(  new Runnable() {  public void run() { x.isSyncB();   }  }, "test2"  );    */

        //不同的实例,同一个synchronized方法,对象锁没有约束(不同的对象——对象锁)——b. x.isSyncA()与y.isSyncA()
         /*Thread test1 = new Thread(  new Runnable() {  public void run() {  x.isSyncA();  }  }, "test1"  );
         Thread test2 = new Thread(  new Runnable() {  public void run() { y.isSyncA();   }  }, "test2"  ); */

        //不同的实例,不同的static synchronized方法,类锁具有约束(不同的对象,类锁)c. x.cSyncA()与y.cSyncB()
         /*Thread test1 = new Thread(  new Runnable() {  public void run() {  x.cSyncA();  }  }, "test1"  );
         Thread test2 = new Thread(  new Runnable() {  public void run() { y.cSyncB();   }  }, "test2"  );  */

        //与实例无关,对象锁和类锁互不影响——d. x.isSyncA()与Something.cSyncA()
        Thread test1 = new Thread(new Runnable() {
            public void run() {
                x.isSyncA();
            }
        }, "test1");
        Thread test2 = new Thread(new Runnable() {
            public void run() {
                y.cSyncA();
            }
        }, "test2");

        test1.start();
        test2.start();

    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容