Java synchronized同步方法和同步块总结

今天做了一些实验,把Java synchronized同步方法和同步块总结一下,欢迎拍砖! :D

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();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值