鱼掉鳞怎么治疗:同一个类,不同方法块synchronized同一实例
来源:百度文库 编辑:九乡新闻网 时间:2024/05/15 09:56:38
package harry.concurrent;
import java.util.concurrent.TimeUnit;
/** * User: Xu.hai * Date: 11-10-12 * Time: 上午9:54 * To change this template use File | Settings | File Templates. */public class Resource1 {
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 (this) { 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 (this) { 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 Resource1 rs = new Resource1();
new Thread() { public void run() { rs.f(); } }.start();
new Thread() { public void run() { rs.g(); } }.start();
rs.h(); }}
main:not synchronized in h()main:synchronized in h()Thread-1:not synchronized in g()Thread-0:not synchronized in f()main:synchronized in h()main:synchronized in h()main:synchronized in h()main:synchronized in h()Thread-0:synchronized in f()Thread-0:synchronized in f()Thread-0:synchronized in f()Thread-0:synchronized in f()Thread-0:synchronized in f()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()
由于3个方法块都synchronized this(自身)同一个对象.当这个类的一个方法块main线程的h()获得锁,其他Thread-1,Thread-0,都只能在他们自己的方法同步块等待,尽管不是main线程的方法块(同一个类),也需要等待。
import java.util.concurrent.TimeUnit;
/** * User: Xu.hai * Date: 11-10-12 * Time: 上午9:54 * To change this template use File | Settings | File Templates. */public class Resource1 {
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 (this) { 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 (this) { 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 Resource1 rs = new Resource1();
new Thread() { public void run() { rs.f(); } }.start();
new Thread() { public void run() { rs.g(); } }.start();
rs.h(); }}
main:not synchronized in h()main:synchronized in h()Thread-1:not synchronized in g()Thread-0:not synchronized in f()main:synchronized in h()main:synchronized in h()main:synchronized in h()main:synchronized in h()Thread-0:synchronized in f()Thread-0:synchronized in f()Thread-0:synchronized in f()Thread-0:synchronized in f()Thread-0:synchronized in f()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()
1.对象的锁所有对象都自动含有单一的锁。
JVM负责跟踪对象被加锁的次数。如果一个对象被解锁,其计数变为0.在任务(线程)第一次给对象加锁的时候,计数变为1.每当这个相同的任务(线程)在此对象上获得锁时,计数会递增。
只有首先获得锁的任务(线程)才能继续获取该对象上的多个锁。
每当任务离开一个synchronized方法,计数递减,当计数为0的时候,锁被完全释放,此时别的任务就可以使用此资源。
2.synchronized同步块2.1同步到单一对象锁当使用同步块时,如果方法下的同步块都同步到一个对象上的锁,则所有的任务(线程)只能互斥的进入这些同步块。
Resource1.java演示了三个线程(包括main线程)试图进入某个类的三个不同的方法的同步块中,虽然这些同步块处在不同的方法中,但由于是同步到同一个对象(当前对象 synchronized (this)),所以对它们的方法依然是互斥的。
由于3个方法块都synchronized this(自身)同一个对象.当这个类的一个方法块main线程的h()获得锁,其他Thread-1,Thread-0,都只能在他们自己的方法同步块等待,尽管不是main线程的方法块(同一个类),也需要等待。
同一个类,不同方法块synchronized同一实例
同一个类,不同方法块synchronized不同实例
同一张课桌才能同一个梦想
同一个故事,不同的结局
同一个世界,不同的空间
同一个厕所不同的事情
不同诗歌中同一“炼字”的比较
不同菇类的不同烹饪方法
不同菇类的不同烹饪方法
di块编辑方法
同一学习区 简易换装三种不同风格-家居装饰
雷锋同一照片在不同的时期
对同一表单的数据做不同处理的方式
[转贴]同一星座不同度数的守护星
婚姻实例预测方法
升学实例预测方法
疾病实例预测方法
词讼实例预测方法
失物实例预测方法
EXT4两种方法实加载(实例化)类
十三类女人安全避孕方法不同
盘点世界12国高考大观:同一个世界 不同的高考
升学实例预测方法1
失物实例预测方法1