Java的synchronized使用

1.synchronized块只能使用对象作为它的参数

2.synchronized用在普通方法和this上,都是用的实例对象锁

3.synchronized用在静态方法和类对象上用的都是类对象的锁

4.synchronized用在属性上用的是当前属性的对象锁

测试代码:

package thread;

public class TestSync {

	private Long num = 1L;

	private static Long millis = 3000L;

	public void attribute() {
		synchronized (num) {
			System.out.println("attribute");
			try {
				Thread.sleep(millis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void staticAttribute() {
		synchronized (millis) {
			System.out.println("staticAttribute");
			try {
				Thread.sleep(millis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void onLong() {
		synchronized (millis.getClass()) {
			System.out.println("onLong");
			try {
				Thread.sleep(millis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public synchronized void method() {
		System.out.println("method");
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static synchronized void staticMethod() {
		System.out.println("staticMethod");
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void staticClass() {
		synchronized (TestSync.class) {
			System.out.println("staticClass");
			try {
				Thread.sleep(millis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void onThis() {
		synchronized (this) {
			System.out.println("onThis");
			try {
				Thread.sleep(millis);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void testOne() {
		// onThis,attribute,staticMethod,staticAttribute,onLong使用的不是同一个锁
		// method,staticClass使用的不是同一个锁
		final TestSync t1 = new TestSync();
		new Thread(new Runnable() {
			public void run() {
				t1.onThis();
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				t1.method();
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				t1.attribute();
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				TestSync.staticMethod();
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				TestSync.staticClass();
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				TestSync.staticAttribute();
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				TestSync.onLong();
			}
		}).start();
	}

	public static void testTwo() {
		// onThis和method使用的是同一个锁
		final TestSync t1 = new TestSync();
		new Thread(new Runnable() {
			public void run() {
				t1.onThis();
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				t1.method();
			}
		}).start();
	}

	public static void testThree() {
		// staticClass和staticMethod使用的是同一个锁
		new Thread(new Runnable() {
			public void run() {
				TestSync.staticClass();
			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				TestSync.staticMethod();
			}
		}).start();
	}

	public static void main(String[] args) {
		/**
		 * 通过三个test方法分别测试,可以得出:
		 * 1.synchronized用在普通方法,普通属性,静态属性,静态方法都用的不同的锁(testOne得出)
		 * 2.synchronized用在普通方法和this上是同一把锁(testTwo得出)
		 * 3.synchronized用在静态方法和Class对象上是同一把锁(testThree得出)
		 */
		testOne();
		// testTwo();
		// testThree();
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值