- 浏览: 335951 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
alafqq:
很好的一篇启蒙hashmap的文章;HASHTABLE的93行 ...
使用数组和链表实现hash表存储信息 -
小帅1127:
我擦,我还以为有什么大坑呢,这也写出来。。。
if..else if和if..if的区别 -
fncj:
转下http://www.dodoer.com
hadoop单机版搭建图文详解 -
yueshang520:
Spring注解原理的详细剖析与实现 -
fncj:
转下,谢谢http://www.whohelpme.com/b ...
Spring注解原理的详细剖析与实现
1、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。
2、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问object中的非synchronized(this)同步代码块。
3、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其他synchronized(this)同步代码块得访问将被阻塞。
4、第三个例子同样适用其他同步代码块。也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其他线程对该object对象所有同步代码部分的访问都将被暂时阻塞。
5、以上规则对其他对象锁同样适用
一、
public class Thread1 implements Runnable{ /** * 一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。 * 另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块 * @param args */ public static void main(String[] args) { Thread1 t1=new Thread1(); Thread ta=new Thread(t1,"A"); Thread tb=new Thread(t1,"B"); ta.start(); tb.start(); } @Override public void run() { synchronized(this){ for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName()+" synchronized loop "+i); } } } }
执行结果:
A synchronized loop 0
A synchronized loop 1
A synchronized loop 2
A synchronized loop 3
A synchronized loop 4
B synchronized loop 0
B synchronized loop 1
B synchronized loop 2
B synchronized loop 3
B synchronized loop 4
二、
package review.testSynchronized; public class Thread2 implements Runnable { /** * 然而,当一个线程访问object的一个synchronized(this)同步代码块时, * 另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。 * @param args */ public static void main(String[] args) { final Thread2 myt2=new Thread2(); Thread t1=new Thread( new Runnable(){ public void run(){ myt2.m4t1(); } },"t1" ); Thread t2=new Thread( new Runnable(){ public void run(){ myt2.m4t2(); } },"t2" ); t1.start(); t2.start(); } public void m4t1(){ synchronized(this){ int i=5; while(i-->0){ System.out.println(Thread.currentThread().getName()+" : "+i); } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } public void m4t2(){ int i=5; while(i-->0){ System.out.println(Thread.currentThread().getName()+" : "+i); } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public void run() { } }
执行结果:
t1 : 4
t1 : 3
t1 : 2
t1 : 1
t2 : 4
t2 : 3
t2 : 2
t2 : 1
t2 : 0
t1 : 0
三、
执行结果:package review.testSynchronized;
public class Thread3 {
/**
* 三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,
* 其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。
* @param args
*/
public static void main(String[] args) {
final Thread3 myt2=new Thread3();
Thread t1=new Thread(
new Runnable(){
public void run(){
myt2.m4t1();
}
},"Thread3_t1"
);
Thread t2=new Thread(
new Runnable(){
public void run(){
myt2.m4t2();
}
},"Thread3_t2"
);
t1.start();
t2.start();
}
public void m4t1(){
synchronized(this){
int i=5;
while(i-->0){
System.out.println(Thread.currentThread().getName()+" : "+i);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void m4t2(){
synchronized(this){
int i=5;
while(i-->0){
System.out.println(Thread.currentThread().getName()+" : "+i);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Thread3_t1 : 4
Thread3_t1 : 3
Thread3_t1 : 2
Thread3_t1 : 1
Thread3_t1 : 0
Thread3_t2 : 4
Thread3_t2 : 3
Thread3_t2 : 2
Thread3_t2 : 1
Thread3_t2 : 0
四、
package review.testSynchronized; public class Thread4 { /** * 四、第三个例子同样适用其它同步代码块。 * 也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。 * 结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。 * @param args */ public static void main(String[] args) { final Thread4 myt4=new Thread4(); Thread t1=new Thread( new Runnable(){ public void run(){ myt4.m4t1(); } },"Thread4_t1" ); Thread t2=new Thread( new Runnable(){ public void run(){ myt4.m4t2(); } },"Thread4_t2" ); t1.start(); t2.start(); } public void m4t1(){ synchronized(this){ int i=5; while(i-->0){ System.out.println(Thread.currentThread().getName()+" : "+i); } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void m4t2(){ int i=5; while(i-->0){ System.out.println(Thread.currentThread().getName()+" : "+i); } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }
执行结果:
Thread4_t1 : 4
Thread4_t1 : 3
Thread4_t1 : 2
Thread4_t1 : 1
Thread4_t1 : 0
Thread4_t2 : 4
Thread4_t2 : 3
Thread4_t2 : 2
Thread4_t2 : 1
Thread4_t2 : 0
五、
package review.testSynchronized;
public class Thread5 {
/** * @param args */ public static void main(String[] args) { final Thread5 myt5=new Thread5(); final Inner inner=myt5.new Inner(); Thread t1=new Thread(new Runnable(){ public void run(){ myt5.m4t1(inner); } },"Thread5_t1" ); Thread t2=new Thread(new Runnable(){ public void run(){ myt5.m4t2(inner); } },"Thread5_t2" ); t1.start(); t2.start(); } class Inner{ private void m4t1(){ int i=5; while(i-->0){ System.out.println(Thread.currentThread().getName()+":Inner.m4t1()="+i); try{ Thread.sleep(500); }catch(Exception e){ } } } private void m4t2(){ int i=5; while(i-->0){ System.out.println(Thread.currentThread().getName()+":Inner.m4t2()="+i); try{ Thread.sleep(500); }catch(Exception e){ } } } } private void m4t1(Inner inner){ synchronized(inner){//使用对象锁 inner.m4t1(); } } private void m4t2(Inner inner){ inner.m4t2(); } }
执行结果:
(尽管t1获得了对Inner的对象锁,但由于线程t2 访问的是同一个inner的非同步部分。所所以两个线程互不干扰)
Thread5_t1:Inner.m4t1()=4
Thread5_t2:Inner.m4t2()=4
Thread5_t2:Inner.m4t2()=3
Thread5_t1:Inner.m4t1()=3
Thread5_t2:Inner.m4t2()=2
Thread5_t1:Inner.m4t1()=2
Thread5_t2:Inner.m4t2()=1
Thread5_t1:Inner.m4t1()=1
Thread5_t1:Inner.m4t1()=0
Thread5_t2:Inner.m4t2()=0
现在在Inner.m4t2()前面加上synchronized:
(尽管t1与t2访问了同一个对象中两个毫不相干的部分,但因为t1先获得了对对象Inner的对象锁,所以t2对Inner.m4t2()的访问也被阻塞,因为m4t2()是Inner中的一个同步方法)
Thread5_t1:Inner.m4t1()=4
Thread5_t1:Inner.m4t1()=3
Thread5_t1:Inner.m4t1()=2
Thread5_t1:Inner.m4t1()=1
Thread5_t1:Inner.m4t1()=0
Thread5_t2:Inner.m4t2()=4
Thread5_t2:Inner.m4t2()=3
Thread5_t2:Inner.m4t2()=2
Thread5_t2:Inner.m4t2()=1
Thread5_t2:Inner.m4t2()=0
发表评论
-
apache日志信息详解
2011-11-06 21:19 6265一、访问日志的格式 Apache内建了记录服务器 ... -
浏览器如何工作
2011-08-19 08:57 0http://taligarsiel.com/Projects ... -
编码实现用JDK中的Proxy实现springAOP功能
2011-08-18 15:04 736http://blog.csdn.net/iamtheevil ... -
Spring注解原理的详细剖析与实现
2011-08-14 23:09 84208本文主要分为三部分: ... -
Spring装配基本属性的原理分析与代码实现
2011-08-11 15:37 1409首先,做一个配置属性的基本测试。修改beans.xml,使引用 ... -
编码剖析Spring依赖注入的原理
2011-08-10 20:01 1805一、注入依赖对象 基本类型对象注入: <b ... -
Spring的三种实例化Bean的方法
2011-08-10 14:03 1Spring的三种实例化Bean的方法 1、 使用 ... -
Spring管理bean的原理自定义实现
2011-08-10 10:44 61761、Spring通过BeanDefinition管理基于S ... -
spring环境搭建与测试
2011-08-10 08:40 3400Chapter1、搭建与测试spring的环境 1、 ... -
java回调机制实现
2011-08-08 09:06 2026Java的接口支持提供了一种获得回调的等价功能的 ... -
log4j的使用与详细分析
2011-08-05 13:32 2617一、什么是log4j? http://logging.a ... -
log4j使用详解
2011-08-04 23:05 2http://logging.apache.org/log4j ... -
java解析XML的四种方法的学习与比较
2011-03-30 20:55 7222四种XML解析方法: ... -
自定义日志模块实现
2011-03-30 09:58 1093package wxy.XXXX.Utils; impo ... -
详细解析Java中抽象类和接口的区别(转)
2011-03-24 23:48 914在Java语言中, abstract cl ... -
NIO学习笔记(三)---通道
2011-03-09 23:06 15461、通道基础 ... -
NIO学习笔记(2)--缓冲区
2011-03-09 18:20 9311、一个Buffer对象是固定数量的数据的容器。其作用是 ... -
封锁管理子系统模拟实现java版
2011-03-09 18:01 1166封锁管理子系统模拟实现 文件锁定 ... -
NIO学习笔记(一)I/O缓冲区操作
2011-03-07 20:04 1220上图简单描述了数据从外部磁盘向运行中的进程的内存区域移动的 ... -
四种参数传递机制的比较与分析
2011-02-16 23:21 1843一、所有程序设计语言都有关于过程的概念,但是在这些 ...
相关推荐
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
synchronized的几种示例研究,方法加锁,代码块加锁(this和对象)以及静态方法加锁的示例和效果。基本上包含了synchronized的几种常用的方式。
This document specifies the third version of the Synchronized Multimedia Integration Language (SMIL, pronounced "smile"). SMIL 3.0 has the following design goals: ● Define an XML-based language...
This document specifies the second version of the Synchronized Multimedia Integration Language (SMIL, pronounced "smile"). SMIL 2.1 has the following design goals: ● Define an XML-based language that...
Abstract-- In this letter, a power quality measurement network for power system is proposed to integrate electric measurement units with the Internet, global positioning system (GPS) and LAMP ...
目录一、synchronized基础1.1synchronized的使用1.1示例1.2验证1.2.1 普通方法和代码块中使用this是同一个监视器(锁),即某个具体调用该代码的对象1.2.2 静态方法和代码块中使用该类的class对象是同一个监视器,...
本文实例讲述了PHP pthreads v3下同步处理synchronized用法。分享给大家供大家参考,具体如下: 之所以会用到同步,是因为如果多个线程中对同一个资源进行操作时,会发生混乱。... $this->add(); } public funct
if (StringType.StrCmp(this.dropBranch.Items[this.i].Text, this.cookie.Values["branch"], false) == 0) { this.dropBranch.Items[this.i].Selected = true; } this.i++; } this.txtpwd.Text = this....
synchronized(this)与synchronized(class) 简单实例 分类: Android Java
(15分) 主要相同点: Lock能完成synchronized所实现的所有功能.(其它不重要) 主要不同点: Lock有比synchronized更精确的线程语义和更好的性能(在相同点中回答此点也行) synchronized会自动释放锁....
public synchronized void setAge(int age) throws RemoteException { this.age = age; } @Override // ����name public void setName(String name) throws RemoteException { this.name = name; } }
synchronized (this){ if(isplay) chosenClip.play(); else { try{this.wait();} catch(InterruptedException e){e.printStackTrace();} }//else try { this.wait(); } catch (InterruptedException e) ...
clientthread.out.writeUTF((String)this.serverthread.name.elementAt(j)); } } catch(IOException e) { e.printStackTrace(); } } } synchronized (serverthread.messages) { if(serverthread....
This is a standard for synchronized phasor measurement systems in substations. It addresses synchronization of data sampling, data-to-phasor conversions, and formats for timing input and phasor data ...
This includes the maintenance of synchronized time during normal operation and following addition, removal, or failure of network components and network reconfiguration. It specifies the use of IEEE ...
public synchronized long nextId() { long timestamp = this.timeGen(); if (this.lastTimestamp == timestamp) { this.sequence = (this.sequence + 1) & this.sequenceMask; if (this.sequence == 0) { System....
* synchronized(同步监视器){ * //需要被同步的代码块(即为操作共享数据的代码) * } * 1.共享数据:多个线程共同操作的同一个数据(变量) * 2.同步监视器:由一个类(任意类)的对象来充当.哪个线程获取此监视器,谁...
/** * PathView is an View that animate ... * or resided it must be synchronized with this object. */ private final Object mSvgLock = new Object(); /** * Thread for working with the object above.
this:this.getCause()).getStackTrace()){ errorMessage.append("\n\t at "); errorMessage.append(st.toString()); } alerter.showMessageDialog(parent, errorMessage, title ,JOptionPane.ERROR_...
We upgraded to the new versions to provide the most recent information and keep this book synchronized with the official documentation. Several reviewers have checked the book for technical accuracy, ...