java并发编程实践学习(一)java的类锁和对象锁(二)

2014-11-23 23:56:40 · 作者: · 浏览: 1
public static void test2() throws Exception { TargetMethod shared = new TargetMethod(); Thread t1 = new DemoThread1(shared); t1.start(); Thread.sleep(100); System.out.println("main thread runnig...."); Thread t2 = new DemoThread2(shared); t2.start(); } } 2、第二组测试,验证所谓的"类锁"的确可以达到控制静态方法同步的效果

package net.aty.lock.thread.second;

import net.aty.lock.target.TargetMethod;

public class DemoThread3 extends Thread
{

	public DemoThread3()
	{

	}

	@Override
	public void run()
	{
		TargetMethod.classLock1();
	}
}

package net.aty.lock.thread.second;

import net.aty.lock.target.TargetMethod;

public class DemoThread4 extends Thread
{
	private TargetMethod target = null;

	public DemoThread4(TargetMethod target)
	{
		this.target = target;
	}

	@Override
	public void run()
	{
		target.classLock2();
	}
}

package net.aty.lock.thread.second;

import net.aty.lock.target.TargetMethod;

public class Test
{

	public static void main(String[] args) throws Exception
	{
		// 线程3运行后,睡眠500ms
		Thread t1 = new DemoThread3();
		t1.start();

		// 主线程睡眠100ms后,恢复执行,此时线程1仍然处于睡眠状态
		Thread.sleep(100);
		System.out.println("main thread runnig....");

		// 线程4开始运行
		Thread t2 = new DemoThread4(new TargetMethod());
		t2.start();
	}

}

执行结果如下:通过分析,可以知道的确实现了static方法之间的同步访问

classLock1------in
main thread runnig....
classLock1------out
classLock2------in
classLock2------out

3、最后我们来测试下对象锁和类锁的区别和联系。线程5会访问同步的实例方法,线程6访问同步的静态方法。

package net.aty.lock.thread.third;

import net.aty.lock.target.TargetMethod;

public class DemoThread5 extends Thread
{
	private TargetMethod target = null;

	public DemoThread5(TargetMethod target)
	{
		this.target = target;
	}

	@Override
	public void run()
	{
		target.objLockMethod1();
	}
}
package net.aty.lock.thread.third;

import net.aty.lock.target.TargetMethod;

public class DemoThread6 extends Thread
{
	public DemoThread6()
	{
	}

	@Override
	public void run()
	{
		TargetMethod.classLock1();
	}
}
package net.aty.lock.thread.third;

import net.aty.lock.target.TargetMethod;

public class Test
{

	public static void main(String[] args) throws Exception
	{
		test2();
	}

	public static void test1() throws Exception
	{
		// 线程5开始运行
		Thread t1 = new DemoThread5(new TargetMethod());
		t1.start();

		// 主线程睡眠100ms后,恢复执行,此时线程1仍然处于睡眠状态
		Thread.sleep(100);
		System.out.println("main thread runnig....");

		// 线程6运行后,睡眠500ms
		Thread t2 = new DemoThread6();
		t2.start();
	}

	public static void test2() throws Exception
	{
		// 线程6开始运行
		Thread t2 = new DemoThread6();
		t2.start();

		// 主线程睡眠100ms后,恢复执行,此时线程1仍然处于睡眠状态
		Thread.sleep(100);
		System.out.println("main thread runnig....");

		// 线程5
		Thread t1 = new DemoThread5(new TargetMethod());
		t1.start();
	}

}

执行结果如下:

classLock1------in
main thread runnig....
in...objLockMethod1
classLock1------out
out...objLockMethod1

可以看出,类锁和对象锁不是同1个东西,一个是类的Class对象的锁,1个是类的实例的锁。也就是说:1个线程访问静态synchronized的时候,允许另一个线程访问对象的实例synchronized方法。反过来也是成立的,因为他们需要的锁是不同的。