- 浏览: 7894 次
- 性别:
- 来自: 上海
最新评论
今天做了一些实验,把Java synchronized同步方法和同步块总结一下,欢迎拍砖!
Java synchronized同步方法和同步块总结:
1、非静态 同步方法:
synchronized method() {...} 锁对象的所有同步方法
一个进程进入某对象同步方法后,其它线程不能同时访问这个对象中任何一个同步方法
2、非静态 同步块:
2.1
method(){
synchronized (this){..} 锁对象的所有同步方法
}
一个进程进入某对象同步方法后,其它线程不能同时访问这个对象中任何一个同步方法
2.2
Object obj = new Object();
method(){
synchronized (obj){..} 锁住该代码块
}
锁定非自身(this)的其他对象(如this的成员),则只锁住该代码块,本类其他同步方法不受影响
3、静态 同步方法
static synchronized method() {...}
一个进程进入类静态同步方法后,其它线程不能同时访问这个类中静态同步方法和静态同步块,其他非静态同步方法和非静态同步块不受影响
4、静态 同步块
static method{
synchronized (Xxx.class){..}
}
一个进程进入类静态同步块后,其它线程不能同时访问这个类中所有静态同步方法和静态同步块,其他非静态同步方法和非静态同步块不受影响
Java synchronized同步方法和同步块总结:
1、非静态 同步方法:
synchronized method() {...} 锁对象的所有同步方法
一个进程进入某对象同步方法后,其它线程不能同时访问这个对象中任何一个同步方法
/**************************************************************************************** Copyright © 2014 Your Company/Org. All rights reserved.<br> Reproduction or transmission in whole or in part, in any form or<br> by any means, electronic, mechanical or otherwise, is prohibited<br> without the prior written consent of the copyright owner. <br> ****************************************************************************************/ package com.beston.concurrency.synchronization; /** * @ClassName: SyncMethod * @Description: 同步方法 synchronized syncMethod(){...} * 作用域:某个对象实例内,可以防止多个线程同时访问这个对象的synchronized方法 * 如果一个对象有多个synchronized方法,只要一个线 程访问了其中的一个synchronized方法, * 其它线程不能同时访问这个对象中任何一个synchronized方法.这时,不同的对象实例的 synchronized方法是不相干扰的。 * 也就是说,其它线程照样可以同时访问相同类的另一个对象实例中的synchronized方法; * @author beston * @date 2014年3月17日 下午4:19:32 * @version v1.0 * */ public class SyncMethod { public static void main(String[] args) { MethodObj obj = new MethodObj(); //线程1执行 hello()和syncmethod1()方法 new Thread(new Runnable1(obj)).start(); //线程2执行 hello()和syncmethod2()方法 new Thread(new Runnable2(obj)).start(); } } /** * @ClassName: Runnable1 * @Description: 执行 hello()和syncmethod1()方法 * @author beston * @date 2014年3月17日 下午4:29:49 * @version v1.0 * */ class Runnable1 implements Runnable{ private MethodObj obj; public Runnable1(MethodObj obj){ this.obj= obj; } public void run(){ obj.hello(); obj.syncmethod1(); } } /** * @ClassName: Runnable2 * @Description: 执行 hello()和syncmethod2()方法 * @author beston * @date 2014年3月17日 下午4:29:07 * @version v1.0 * */ class Runnable2 implements Runnable{ private MethodObj obj; public Runnable2(MethodObj obj){ this.obj= obj; } public void run(){ obj.hello(); obj.syncmethod2(); } } class MethodObj{ /** * @Title: hello * @Description: 非同步,不会被锁 */ public void hello(){ System.out.println(Thread.currentThread().getName()+" hello!"); } /** * @Title: syncmethod1 * @Description: 同步方法1,被第一个持有该对象的线程占用5秒 */ public synchronized void syncmethod1(){ System.out.println(Thread.currentThread().getName()+" 占用 syncmethod1() 5秒"); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @Title: syncmethod2 * @Description: 同步方法2 */ public synchronized void syncmethod2(){ System.out.println(Thread.currentThread().getName()+" syncmethod2()"); } }
2、非静态 同步块:
2.1
method(){
synchronized (this){..} 锁对象的所有同步方法
}
一个进程进入某对象同步方法后,其它线程不能同时访问这个对象中任何一个同步方法
/**************************************************************************************** Copyright © 2014 Your Company/Org. All rights reserved.<br> Reproduction or transmission in whole or in part, in any form or<br> by any means, electronic, mechanical or otherwise, is prohibited<br> without the prior written consent of the copyright owner. <br> ****************************************************************************************/ package com.beston.concurrency.synchronization; /** * @ClassName: SyncMethod * @Description: 同步块 synchronized(this){...} * 作用域: 锁定 当前对象this * @author beston * @date 2014年3月17日 下午4:19:32 * @version v1.0 * */ public class SyncThis { public static void main(String[] args) { ThisObj obj = new ThisObj(); //线程1执行 hello()和syncthis1()方法 new Thread(new Runnable3(obj)).start(); //线程2执行 hello()和syncthis2()方法 new Thread(new Runnable4(obj)).start(); } } /** * @ClassName: Runnable3 * @Description: 执行 hello()和syncthis1()方法 * @author beston * @date 2014年3月17日 下午4:29:49 * @version v1.0 * */ class Runnable3 implements Runnable{ private ThisObj obj; public Runnable3(ThisObj obj){ this.obj= obj; } public void run(){ obj.hello(); obj.syncthis1(); } } /** * @ClassName: Runnable4 * @Description: 执行 hello()和syncthis2()方法 * @author beston * @date 2014年3月17日 下午4:29:07 * @version v1.0 * */ class Runnable4 implements Runnable{ private ThisObj obj; public Runnable4(ThisObj obj){ this.obj= obj; } public void run(){ obj.hello(); obj.syncthis2(); } } class ThisObj{ /** * @Title: hello * @Description: 非同步,不会被锁 */ public void hello(){ System.out.println(Thread.currentThread().getName()+" hello!"); } /** * @Title: syncthis1 * @Description: 同步块1,被第一个持有该对象的线程占用5秒 */ public void syncthis1(){ synchronized(this){ System.out.println(Thread.currentThread().getName()+" 占用 syncthis1() 5秒"); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * @Title: syncthis2 * @Description: 同步块2 */ public void syncthis2(){ synchronized(this){ System.out.println(Thread.currentThread().getName()+" syncthis2()"); } } }
2.2
Object obj = new Object();
method(){
synchronized (obj){..} 锁住该代码块
}
锁定非自身(this)的其他对象(如this的成员),则只锁住该代码块,本类其他同步方法不受影响
/**************************************************************************************** Copyright © 2014 Your Company/Org. All rights reserved.<br> Reproduction or transmission in whole or in part, in any form or<br> by any means, electronic, mechanical or otherwise, is prohibited<br> without the prior written consent of the copyright owner. <br> ****************************************************************************************/ package com.beston.concurrency.synchronization; /** * @ClassName: SyncBlock * @Description: 同步块 synchronized(obj){...} * 锁定非自身(this)的其他对象,则只锁住该代码块,其他同步方法不受影响 * @author beston * @date 2014年3月17日 下午5:02:16 * @version v1.0 * */ public class SyncBlock { public static void main(String[] args) { BlockObj obj = new BlockObj(); //线程1执行 hello()和syncblock1()方法 new Thread(new Runnable5(obj)).start(); //线程2执行 hello()和syncblock1(),syncblock2()方法 new Thread(new Runnable6(obj)).start(); } } /** * @ClassName: Runnable5 * @Description: 执行 hello()和syncblock1()方法 * @author beston * @date 2014年3月17日 下午4:29:49 * @version v1.0 * */ class Runnable5 implements Runnable{ private BlockObj obj; public Runnable5(BlockObj obj){ this.obj= obj; } public void run(){ obj.hello(); obj.syncblock1(); } } /** * @ClassName: Runnable6 * @Description: 执行 hello()和syncblock1(),syncblock2()方法 * @author beston * @date 2014年3月17日 下午4:29:07 * @version v1.0 * */ class Runnable6 implements Runnable{ private BlockObj obj; public Runnable6(BlockObj obj){ this.obj= obj; } public void run(){ obj.hello(); obj.syncblock2(); obj.syncblock1(); } } class BlockObj{ Byte[] b = new Byte[0]; Object a = new Object(); int i = 0;//非对象 不能放到synchronized()括号中 /** * @Title: hello * @Description: 非同步,不会被锁 */ public void hello(){ System.out.println(Thread.currentThread().getName()+" hello!"); } /** * @Title: syncblock1 * @Description: 同步块,进入该方法线程占用5秒 */ public void syncblock1(){ synchronized(a){ System.out.println(Thread.currentThread().getName()+" 占用 syncblock1() 5秒"); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * @Title: syncblock2 * @Description: 同步块 */ public void syncblock2(){ synchronized(b){ System.out.println(Thread.currentThread().getName()+" syncblock2()"); } } }
3、静态 同步方法
static synchronized method() {...}
一个进程进入类静态同步方法后,其它线程不能同时访问这个类中静态同步方法和静态同步块,其他非静态同步方法和非静态同步块不受影响
/**************************************************************************************** Copyright © 2014 Your Company/Org. All rights reserved.<br> Reproduction or transmission in whole or in part, in any form or<br> by any means, electronic, mechanical or otherwise, is prohibited<br> without the prior written consent of the copyright owner. <br> ****************************************************************************************/ package com.beston.concurrency.synchronization; import java.util.logging.Logger; /** * @ClassName: SynStaticMethod * @Description: 结论: 不同线程访问同一个类的静态同步方法时, 线程间是互斥的.其他非静态同步块和同步方法不受影响 * @author beston * @date 2014年3月17日 下午6:02:41 * @version v1.0 * */ public class SynStaticMethod { // 线程1 static class T1 implements Runnable { SynStaticMethod s; public T1(SynStaticMethod sameObj) { this.s = sameObj; } // 线程1访问静态同步方法 public void run() { SynStaticMethod.syn(); } } // 线程2 static class T2 implements Runnable { SynStaticMethod s; public T2(SynStaticMethod sameObj) { this.s = sameObj; } // 线程2访问静态同步方法 public void run() { //SynStaticMethod.static2();不会阻塞 //SynStaticMethod.syn2(); //new SynStaticMethod().synInstance3(); //new SynStaticMethod().syn(); //SynStaticMethod.syn(); } } // 对象的静态同步方法 public static synchronized void syn() { String threadStr = Thread.currentThread().getName(); try { System.out.println(threadStr + "正在静态访问同步方法syn()!!!"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } // 对象的静态同步方法 public static synchronized void syn2() { String threadStr = Thread.currentThread().getName(); try { System.out.println(threadStr + "正在静态访问同步方法syn2()!!!"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } // 对象的静态同步方法 public synchronized void synInstance3() { String threadStr = Thread.currentThread().getName(); try { System.out.println(threadStr + "正在静态访问同步方法synInstance3()!!!"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } // 对象的静态方法2 public static void static2() { String threadStr = Thread.currentThread().getName(); try { System.out.println(threadStr + "正在静态访问方法static2()!!!"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { // 这是多线程要访问的同一个对象 SynStaticMethod sameObj1 = new SynStaticMethod(); SynStaticMethod sameObj2 = new SynStaticMethod(); // 线程1,线程2访问静态同步方法 Thread t1 = new Thread(new T1(sameObj1)); Thread t2 = new Thread(new T2(sameObj2)); t1.start(); t2.start(); } }
4、静态 同步块
static method{
synchronized (Xxx.class){..}
}
一个进程进入类静态同步块后,其它线程不能同时访问这个类中所有静态同步方法和静态同步块,其他非静态同步方法和非静态同步块不受影响
/**************************************************************************************** Copyright © 2014 Your Company/Org. All rights reserved.<br> Reproduction or transmission in whole or in part, in any form or<br> by any means, electronic, mechanical or otherwise, is prohibited<br> without the prior written consent of the copyright owner. <br> ****************************************************************************************/ package com.beston.concurrency.synchronization; import java.util.logging.Logger; /** * @ClassName: SynStaticClass * @Description: 不同线程访问类的所有同步块静态方法和静态同步方法时, 线程间是互斥的.其他非静态同步块和同步方法不受影响 * @author beston * @date 2014年3月17日 下午6:02:41 * @version v1.0 * */ public class SynStaticClass { // 线程1 static class T1 implements Runnable { SynStaticClass s; public T1(SynStaticClass sameObj) { this.s = sameObj; } // 线程1访问静态同步方法 public void run() { SynStaticClass.syn(); } } // 线程2 static class T2 implements Runnable { SynStaticClass s; public T2(SynStaticClass sameObj) { this.s = sameObj; } // 线程2访问静态同步方法 public void run() { new SynStaticClass().synInstance4();//不会被阻塞 //new SynStaticClass().syn(); //会阻塞 //new SynStaticClass().syn3(); //会阻塞 //SynStaticClass.syn3(); //阻塞 //SynStaticClass.syn2(); //阻塞 //SynStaticClass.syn(); //阻塞 } } // 对象的同步块静态方法 public static void syn() { synchronized(SynStaticClass.class){ String threadStr = Thread.currentThread().getName(); try { System.out.println(threadStr + "正在静态访问同步方法syn()!!!"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } // 对象的同步块静态方法2 public static void syn2() { synchronized (SynStaticClass.class) { String threadStr = Thread.currentThread().getName(); try { System.out.println(threadStr + "正在静态访问同步方法syn2()!!!"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } // 对象的静态同步方法3 public static synchronized void syn3() { String threadStr = Thread.currentThread().getName(); try { System.out.println(threadStr + "正在静态访问同步方法syn3()!!!"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } // 对象的同步方法3 public synchronized void synInstance4() { String threadStr = Thread.currentThread().getName(); try { System.out.println(threadStr + "正在访问实例同步方法synInstance4()!!!"); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) throws InterruptedException { // 这是多线程要访问的同一个类 SynStaticClass sameObj1 = new SynStaticClass(); SynStaticClass sameObj2 = new SynStaticClass(); // 线程1,线程2访问静态同步方法 Thread t1 = new Thread(new T1(sameObj1)); Thread t2 = new Thread(new T2(sameObj2)); t1.start(); Thread.sleep(100); t2.start(); } }
发表评论
-
实现线程的stop,suspend和resume方法
2014-03-28 11:15 1508Thread的stop,suspend和resume方法不 ... -
经典生存者消费者模型,wait,notify和notifyAll实现
2014-03-28 10:48 1142经典生存者消费者模型,使用Object的wait,noti ... -
Java多线程-线程的调度sleep,yield,join, wait,notify,notifyAll
2014-03-28 09:26 1353Java多线程-线程的调度(休眠) sleep 让给 ... -
[转]Java方法参数是引用调用还是值调用?
2014-03-24 09:22 697原文:http://hxraid.iteye.com/bl ...
相关推荐
java同步synchronized关键字用法示例
主要介绍了java synchronized同步静态方法和同步非静态方法的异同的相关资料,需要的朋友可以参考下
基于Java synchronized同步锁实现线程交互.pdf
主要介绍了 java中synchronized(同步代码块和同步方法)详解及区别的相关资料,需要的朋友可以参考下
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
synchronized关键字可以作为函数的修饰符,也可作为函数内的语句,也就是平时说的同步方法和同步语句块。如果 再细的分类,synchronized可作用于instance变量、object reference(对象引用)、static函数和class ...
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
synchronized下的方法控制多线程程序中的线程同步非常方便,这里就来看一下Java使用synchronized修饰方法来同步线程的实例演示,需要的朋友可以参考下
synchronized块来同步一个对象变量,也可以使用synchronized块来同步类中的静态方法和非静态方法,下面使用Synchronized块同步方法
Java中的synchronized:同步方法与线程安全
Java 对多线程的支持与同步机制深受...是平时说的同步方法和同步语句块。如果再细的分类,synchronized 可作用于instance 变量、 object reference(对象引用)、static 函数和class literals(类名称字面常量)身上。
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
在学习Java过程中,自己收集了很多的Java的学习资料,分享给大家,有需要的欢迎下载,希望对大家有用,一起学习,一起进步。
主要介绍了Java多线程synchronized同步方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java线程及同步(synchronized)样例代码
synchronized关键字可以作为函数的修饰符,也可作为函数内的语句,也就是平时说的同步方法和同步语句块。如果 再细的分类,synchronized可作用于instance变量、object reference(对象引用)、static函数和class ...
java synchronized关键字原理、自定义一把锁来实现同步等