频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

使用synchronized和Lock对象获取对象锁

来源: 作者:子 孑 出处:巧巧读书 2008-04-23 进入讨论组
上一页 1 2 3 
     Thread-0:not synchronized in f()
    Thread-0:synchronized in f()
    main:not synchronized in h()
    main:synchronized in h()
    Thread-1:not synchronized in g()
    Thread-1:synchronized in g()
    Thread-0:synchronized in f()
    main:synchronized in h()
    Thread-1:synchronized in g()
    Thread-0:synchronized in f()
    main:synchronized in h()
    Thread-1:synchronized in g()
    Thread-0:synchronized in f()
    main:synchronized in h()
    Thread-1:synchronized in g()
    Thread-0:synchronized in f()
    main:synchronized in h()
    Thread-1:synchronized in g()

    3.Lock对象锁
    除了使用synchronized外,还可以使用Lock对象来创建临界区。Resource3.java的演示效果同Resource1.java;

 Resource4.java的演示效果同Resource2.java。
Resource3.java
package com.zj.lock;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Resource3 {
    private Lock lock = new ReentrantLock();

    public void f() {
       // other operations should not be locked...
       System.out.println(Thread.currentThread().getName()
              + ":not synchronized in f()");
       lock.lock();
       try {
           for (int i = 0; i < 5; i++) {
              System.out.println(Thread.currentThread().getName()
                     + ":synchronized in f()");
              try {
                  TimeUnit.SECONDS.sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       } finally {
           lock.unlock();
       }
    }

    public void g() {
       // other operations should not be locked...
       System.out.println(Thread.currentThread().getName()
              + ":not synchronized in g()");
       lock.lock();
       try {
           for (int i = 0; i < 5; i++) {
              System.out.println(Thread.currentThread().getName()
                     + ":synchronized in g()");
              try {
                  TimeUnit.SECONDS.sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       } finally {
           lock.unlock();
       }
    }

    public void h() {
       // other operations should not be locked...
       System.out.println(Thread.currentThread().getName()
              + ":not synchronized in h()");
       lock.lock();
       try {
           for (int i = 0; i < 5; i++) {
              System.out.println(Thread.currentThread().getName()
                     + ":synchronized in h()");
              try {
                  TimeUnit.SECONDS.sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       } finally {
           lock.unlock();
       }
    }

    public static void main(String[] args) {
       final Resource3 rs = new Resource3();

       new Thread() {
           public void run() {
              rs.f();
           }
       }.start();

       new Thread() {
           public void run() {
              rs.g();
           }
       }.start();

       rs.h();
        }
    }

    结果:
    Thread-0:not synchronized in f()
    Thread-0:synchronized in f()
    main:not synchronized in h()
    Thread-1:not synchronized in g()
    Thread-0:synchronized in f()
    Thread-0:synchronized in f()
    Thread-0:synchronized in f()
    Thread-0:synchronized in f()
    main:synchronized in h()
    main:synchronized in h()
    main:synchronized in h()
    main:synchronized in h()
    main:synchronized in h()
    Thread-1:synchronized in g()
    Thread-1:synchronized in g()
    Thread-1:synchronized in g()
    Thread-1:synchronized in g()
    Thread-1:synchronized in g()
    Resource4.java
    package com.zj.lock;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;

    public class Resource4 {
        private Lock lock1 = new ReentrantLock();
        private Lock lock2 = new ReentrantLock();
        private Lock lock3 = new ReentrantLock();

        public void f() {
           // other operations should not be locked...
           System.out.println(Thread.currentThread().getName()
                  + ":not synchronized in f()");
           lock1.lock();
           try {
           for (int i = 0; i < 5; i++) {
              System.out.println(Thread.currentThread().getName()
                     + ":synchronized in f()");
              try {
                  TimeUnit.SECONDS.sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       } finally {
           lock1.unlock();
       }
    }

    public void g() {
       // other operations should not be locked...
       System.out.println(Thread.currentThread().getName()
              + ":not synchronized in g()");
       lock2.lock();
       try {
           for (int i = 0; i < 5; i++) {
              System.out.println(Thread.currentThread().getName()
                     + ":synchronized in g()");
              try {
                  TimeUnit.SECONDS.sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       } finally {
           lock2.unlock();
       }
    }

    public void h() {
       // other operations should not be locked...
       System.out.println(Thread.currentThread().getName()
              + ":not synchronized in h()");
       lock3.lock();
       try {
           for (int i = 0; i < 5; i++) {
              System.out.println(Thread.currentThread().getName()
                     + ":synchronized in h()");
              try {
                  TimeUnit.SECONDS.sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       } finally {
           lock3.unlock();
        }
     }

     public static void main(String[] args) {
       final Resource4 rs = new Resource4();

       new Thread() {
           public void run() {
              rs.f();
           }
       }.start();

       new Thread() {
           public void run() {
              rs.g();
           }
       }.start();

       rs.h();
       }
    }

    结果:

     Thread-0:not synchronized in f()
    Thread-0:synchronized in f()
    main:not synchronized in h()
    main:synchronized in h()
    Thread-1:not synchronized in g()
    Thread-1:synchronized in g()
    Thread-0:synchronized in f()
    main:synchronized in h()
    Thread-1:synchronized in g()
    Thread-0:synchronized in f()
    main:synchronized in h()
    Thread-1:synchronized in g()
    Thread-0:synchronized in f()
    main:synchronized in h()
    Thread-1:synchronized in g()
    Thread-0:synchronized in f()
    main:synchronized in h()
    Thread-1:synchronized in g()



进入讨论组讨论。
上一页 1 2 3 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章