鱼痣用什么药可以去除:同一个类,不同方法块synchronized不同实例

来源:百度文库 编辑:九乡新闻网 时间:2024/05/05 18:47:38
 2.2 同步到多个对象锁
    Resource2.java演示了三个线程(包括main线程)试图进入某个类的三个不同的方法的同步块中,这些同步块处在不同的方法中,并且是同步到三个不同的对象(synchronized (this),synchronized (syncObject1),synchronized (syncObject2)),所以对它们的方法中的临界资源访问是独立的。
    Resource2.java
package harry.concurrent;
import java.util.concurrent.TimeUnit;
/** * User: Xu.hai * Date: 11-10-12 * Time: 上午10:05 * To change this template use File | Settings | File Templates. */public class Resource2 {
private Object syncObject1 = new Object();    private Object syncObject2 = new Object();
    public void f() {       // other operations should not be locked...       System.out.println(Thread.currentThread().getName()              + ":not synchronized in f()");       synchronized (this) {           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();              }           }       }    }
    public void g() {       // other operations should not be locked...       System.out.println(Thread.currentThread().getName()              + ":not synchronized in g()");       synchronized (syncObject1) {           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();              }           }       }    }
    public void h() {       // other operations should not be locked...       System.out.println(Thread.currentThread().getName()              + ":not synchronized in h()");       synchronized (syncObject2) {           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();              }           }       }    }
    public static void main(String[] args) {       final Resource2 rs = new Resource2();
       new Thread() {           public void run() {              rs.f();           }       }.start();
       new Thread() {           public void run() {              rs.g();           }       }.start();
       rs.h();    }}


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