synchronized是对什么加锁?
来源:互联网 发布:女生打扮前 知乎 编辑:程序博客网 时间:2024/04/29 01:44
直接看代码
public class SynTest { static int count = 0; private int i; public SynTest() { i = (++count); } public synchronized void f(){ System.out.println("hello" + " " + i); while (true) { } } public static void main(String[] args) { SynTest synTest = new SynTest(); SynTest synTest1 = new SynTest(); Thread thread = new Thread() { @Override public void run() { synTest.f(); } }; Thread thread1 = new Thread() { @Override public void run() { synTest1.f(); } }; thread.start(); thread1.start(); }}
/**hello 1hello 2*/
再来看这一个
public class SynTest { static int count = 0; private int i; public SynTest() { i = (++count); } public synchronized void f(){ System.out.println("hello" + " " + i); while (true) { } } public static void main(String[] args) { SynTest synTest = new SynTest(); Thread thread = new Thread() { @Override public void run() { synTest.f(); } }; Thread thread1 = new Thread() { @Override public void run() { synTest.f(); } }; thread.start(); thread1.start(); }}
/**hello 1*/
对实例对象加锁
再来看代码
public class SynTest { public static synchronized void f(){ System.out.println("hello"); while (true) { } } public static void main(String[] args) { SynTest synTest = new SynTest(); SynTest synTest1 = new SynTest(); Thread thread = new Thread() { @Override public void run() { synTest.f(); } }; Thread thread1 = new Thread() { @Override public void run() { synTest1.f(); } }; thread.start(); thread1.start(); }}
/**hello*/
对class对象加锁
然后synchronized代码块肯定是对显式的对象加锁
0 0
- synchronized是对什么加锁?
- synchronized -- 同步加锁的是对象,而不是代码
- 多线程Thread同步synchronized(加锁)
- 重新认识 java synchronized 加锁
- synchronized关键字加锁-线程可重入
- 服务器集群对Synchronized有没有什么影响
- 如何对全局变量加锁
- 短暂transient和加锁synchronized 使用
- Android 同步代码块,synchronized加锁
- OSATOMIC与synchronized 加锁的对比
- IOS开发之关键字synchronized(线程加锁)
- 对集合在客户端加锁
- java 对文件进行加锁
- 对Lu模块进行加锁
- fcntl 对文件加锁示例
- 如果synchronized是不可重入的,为什么重载父类函数后(重载函数也加锁)会产生死锁?
- synchronized锁住了什么
- synchronized 锁住了什么
- 面向对象之寄生组合继承
- Zookeeper开源客户端Curator之事件监听详解
- bzoj 1046: [HAOI2007]上升序列 (DP)
- ·完整·单纯形算法(Simplex Algorithm),附C源码
- bzoj 1028: [JSOI2007]麻将 (贪心)
- synchronized是对什么加锁?
- Ubuntu 16.04中搜狗输入法乱码问题
- linux之信号产生
- Instant Run导致ClassNotFoundException
- Flex布局(二)
- poj 3260 双端队列优化多重背包+完全背包
- Android NDK开发入门 jni javah使用(类继承Activity)
- mvhline函数
- iceroot java基础工具类