[童虎退壳系列]方法加锁测试

package creative.fire.multithread;

import java.util.concurrent.CountDownLatch;

/**
 * @author feuyeux@gmail.com 2011-10-13
 * 
 */

/*
 * synchronized static void foo() { ... } is the same as
 * 
 * static void foo() { synchronized(SomeClass.class) { ... } }
 * 
 * synchronized void foo() { ... } is the same as
 * 
 * void foo() { synchronized(this) { ... } }
 */
public class LockMethodTest {
	private static final int timeout = 3 * 1000;

	public synchronized void a(String name) throws InterruptedException {
		System.out.println(name + " enter a");
		Thread.sleep(timeout);
		System.out.println("quit a");
	}

	public synchronized void b() throws InterruptedException {
		System.out.println("enter b");
		Thread.sleep(timeout);
		System.out.println("quit b");
	}

	public synchronized static void c() throws InterruptedException {
		System.out.println("enter c");
		Thread.sleep(timeout);
		System.out.println("quit c");
	}

	public static void main(String[] args) throws InterruptedException {
		testInSameObject();
		System.out.println("--------");
		testInTwoObjects();
		System.out.println("--------");
		testClassObject();
	}

	private static void testClassObject() throws InterruptedException {
		final CountDownLatch latch = new CountDownLatch(2);
		final LockMethodTest thiz = new LockMethodTest();

		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					LockMethodTest.c();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				latch.countDown();
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					thiz.a("object");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				latch.countDown();
			}
		}).start();
		latch.await();
	}

	private static void testInTwoObjects() throws InterruptedException {
		final CountDownLatch latch = new CountDownLatch(2);
		final LockMethodTest thiz1 = new LockMethodTest();
		final LockMethodTest thiz2 = new LockMethodTest();

		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					thiz1.a("object1");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				latch.countDown();
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					thiz2.a("object2");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				latch.countDown();
			}
		}).start();
		latch.await();
	}

	private static void testInSameObject() throws InterruptedException {
		final CountDownLatch latch = new CountDownLatch(2);

		final LockMethodTest thiz = new LockMethodTest();
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					thiz.a("object");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				latch.countDown();
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					thiz.b();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				latch.countDown();
			}
		}).start();
		latch.await();
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值