synchronized(this)、synchronized(xx.class)、synchronized(Object)的使用及区别

这个类是用来调取方法的

package synchronizedthis;

public class SynchronizedThis {
	
	int adder = 0;
	Test test = new Test();
	
	public void useMethod() {
		synchronized(this) {
			try {
				System.out.println(Thread.currentThread().getName() + " use the method and start");
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}finally {
				System.out.println(Thread.currentThread().getName() + " use success and 3s passed");
			}
		}
	}
	
}

class Test{
	// 啥也没有,拿来做锁对象测试的
}

这是准备用来作为一个线程的类

package synchronizedthis;

public class ThreadA extends Thread{
	
	String name;
	SynchronizedThis synchronizedThis;
	
	ThreadA(SynchronizedThis synchronizedThis){
		this.synchronizedThis = synchronizedThis;
	}
	
	public void run() {
		synchronizedThis.useMethod();
	}

}

这是另一个准备用来做线程的类

package synchronizedthis;

public class ThreadB extends Thread{

	SynchronizedThis synchronizedThis;
	
	ThreadB(SynchronizedThis synchronizedThis){
		this.synchronizedThis = synchronizedThis;
	}
	
	public void run() {
		synchronizedThis.useMethod();
	}
}

这是使用这些类的代码

package synchronizedthis;
/**
 * 两个线程执行同一个方法
 * @author bamboo
 *
 */
public class UseThis {
	public static void main(String[] args) {
		SynchronizedThis sync = new SynchronizedThis();
		SynchronizedThis sync1 = new SynchronizedThis();
		SynchronizedThis sync2 = new SynchronizedThis();
		
		ThreadA a = new ThreadA(sync);       // a b 相同实例
		ThreadB b = new ThreadB(sync);
		ThreadA c = new ThreadA(sync1);      // c d 不同实例
		ThreadA d = new ThreadA(sync2);
		
		a.setName("A Thread");
		b.setName("B Thread");
		c.setName("C Thread");
		d.setName("D Thread");
		
		a.start();
		b.start();
		c.start();
		d.start();
		
	}
}

好了,现在想要看到this、xx.class、Object的区别只用更改synchronized()括号里面的内容就能够很清楚地的看到区别了。

this的情况

C Thread use the method and start
A Thread use the method and start
D Thread use the method and start
A Thread use success and 3s passed
C Thread use success and 3s passed
D Thread use success and 3s passed
B Thread use the method and start
B Thread use success and 3s passed

可以看到先运行的A、C、D。因为A、B是相同的实例,C、D是不同的实例。

xx.class的情况

B Thread use the method and start
B Thread use success and 3s passed
D Thread use the method and start
D Thread use success and 3s passed
C Thread use the method and start
C Thread use success and 3s passed
A Thread use the method and start
A Thread use success and 3s passed

可以看到每次只能有一个执行

Object的情况

这得分两种:new Test() 和 test。也就是每次都是相同对象还是不同对象的区别。
用自己 的对象还是用别人的没有区别。

括号内是 new Test() 也就是每次对象都不相同
这个new Test() 和new SynchronizedThis () 效果是相同的

B Thread use the method and start
A Thread use the method and start
C Thread use the method and start
D Thread use the method and start
C Thread use success and 3s passed
D Thread use success and 3s passed
A Thread use success and 3s passed
B Thread use success and 3s passed

可以看出A、B、C、D几乎同时开始,同时结束,互不影响。

括号内是 test 对象

A Thread use the method and start
C Thread use the method and start
D Thread use the method and start
D Thread use success and 3s passed
A Thread use success and 3s passed
C Thread use success and 3s passed
B Thread use the method and start
B Thread use success and 3s passed

可以看出A、C、D是同时开始。即相同对象只能有一个获得锁,不同对象都可以获得锁。

synchronized加在非静态方法体

更改后的方法如下

	public /*static*/ synchronized void  useMethod() {
		//synchronized(test) {
			try {
				System.out.println(Thread.currentThread().getName() + " use the method and start");
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}finally {
				System.out.println(Thread.currentThread().getName() + " use success and 3s passed");
			}
		//}
	}
A Thread use the method and start
C Thread use the method and start
D Thread use the method and start
D Thread use success and 3s passed
A Thread use success and 3s passed
C Thread use success and 3s passed
B Thread use the method and start
B Thread use success and 3s passed

情况和this相同效果和对方法体内所有内容synchronized(this)一样。

synchronized加在静态方法上

A Thread use the method and start
A Thread use success and 3s passed
D Thread use the method and start
D Thread use success and 3s passed
C Thread use the method and start
C Thread use success and 3s passed
B Thread use the method and start
B Thread use success and 3s passed

和xx.class情况一样,因为static修饰的内容进行编译的时候还没有产生对象,默认使用类锁,也就是自己的xx.class

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值