鱼痣用什么药可以去除:同一个类,不同方法块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()
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()
同一个类,不同方法块synchronized不同实例
同一个类,不同方法块synchronized同一实例
同一个故事,不同的结局
同一个世界,不同的空间
同一个厕所不同的事情
不同菇类的不同烹饪方法
不同菇类的不同烹饪方法
十三类女人安全避孕方法不同
读书的不同方法
教育孩子的不同方法
不同帐户结帐方法
月经不调不同症状调理方法不同
不同斑点种类 不同祛斑方法
不同的肤质有不同的保湿方法
因材施教新论:血型不同方法不同
不同的血型,不同的养生方法
脸色不同养生方法大不同
失眠类型不同,治疗方法不同
因材施教新论:血型不同方法不同
不同方法记忆三百六十穴
不同方法记忆三百六十穴--
西装多种不同搭配方法
不同品牌机进入BIOS方法
不同茶的饮用方法