细说Java同步锁(synchronized)
来源:互联网 发布:baocms7.6源码下载 编辑:程序博客网 时间:2024/06/02 02:04
1.如果锁的是对象,同一对象的多个线程并发访问的时候,拿到同步锁的线程会执行临界区的代码,其他线程被阻塞;如果是多个对象的多线程并发就都可以访问;
/** * @author znland * @date 2017年7月27日 12:28:46 * @describe */public class CSDNSynchronized {public void testPrint() {synchronized (this) {int i = 6;while (i-- > 0) {System.out.println(Thread.currentThread().getName() + " : " + i);try {Thread.sleep(500);} catch (InterruptedException ie) {}}}}public static void main(String[] args) {final CSDNSynchronized csdn = new CSDNSynchronized();Thread test1 = new Thread(new Runnable() {public void run() {csdn.testPrint();}}, "线程1");Thread test2 = new Thread(new Runnable() {public void run() {csdn.testPrint();}}, "线程2");test1.start();test2.start();}}
结果可以看出:一个线程运行时,另一个被阻塞了
2.如果是类锁,这个类的多个实例也是互斥的
/** * @author znland * @date 2017年7月27日 12:28:46 * @describe */public class CSDNSynchronized {/** * 类锁有两种形式的写法 在方法前加上 synchronized static 或 synchronized (*.class),两者功能一致 */public void testPrint() {synchronized (CSDNSynchronized.class) {int i = 6;while (i-- > 0) {System.out.println(Thread.currentThread().getName() + " : " + i);try {Thread.sleep(500);} catch (InterruptedException ie) {}}}}public synchronized static void testClassPrint() {int i = 6;while (i-- > 0) {System.out.println(Thread.currentThread().getName() + " : " + i);try {Thread.sleep(500);} catch (InterruptedException ie) {}}}public static void main(String[] args) {final CSDNSynchronized csdn = new CSDNSynchronized();final CSDNSynchronized csdn2 = new CSDNSynchronized();Thread test1 = new Thread(new Runnable() {public void run() {csdn.testPrint();}}, "线程1");Thread test2 = new Thread(new Runnable() {public void run() {csdn2.testPrint();}}, "线程2");test1.start();test2.start();}}
3.对象锁和类锁相互不会干扰,也就是同一实例的多个线程访问使用对象锁的方法时,不影响其他线程访问类锁的方法;同理反过来也是一样的。
/** * @author znland * @date 2017年7月27日 12:28:46 * @describe */public class CSDNSynchronized {/** * 类锁有两种形式的写法 在方法前加上 synchronized static 或 synchronized (*.class),两者功能一致 */public void testPrint() {synchronized (this) {int i = 6;while (i-- > 0) {System.out.println(Thread.currentThread().getName() + " : " + i);try {Thread.sleep(500);} catch (InterruptedException ie) {}}}}public synchronized static void testClassPrint() {int i = 6;while (i-- > 0) {System.out.println(Thread.currentThread().getName() + " : " + i);try {Thread.sleep(500);} catch (InterruptedException ie) {}}}public static void main(String[] args) {final CSDNSynchronized csdn = new CSDNSynchronized();Thread test1 = new Thread(new Runnable() {public void run() {csdn.testPrint();}}, "线程1");Thread test2 = new Thread(new Runnable() {public void run() {CSDNSynchronized.testClassPrint();}}, "线程2");test1.start();test2.start();}}
阅读全文
1 0
- 细说Java同步锁(synchronized)
- java 同步锁(synchronized)
- java 同步锁(synchronized)
- java 同步锁(synchronized)
- java 同步锁(synchronized)
- java 同步锁(synchronized)
- java 同步锁(synchronized)
- java 同步锁(synchronized)
- java 同步锁(synchronized)
- java同步锁(synchronized)
- java 同步锁(synchronized)
- Java synchronized同步锁
- java 同步锁(synchronized)
- java-同步锁synchronized
- java 同步锁(synchronized)
- JAVA同步(synchronized)
- JAVA synchronized 同步锁 简析
- java 同步锁问题(synchronized)
- MQTT协议-MQTT协议简介及协议原理
- ASP.NET MVC 之表格分页
- 容器的打印
- CSS折行
- MYSQL 字符串处理函数
- 细说Java同步锁(synchronized)
- Android-贪吃蛇(五)- 配置文件
- 那些年ant自定义构建dex分包所遇到的坑
- 最全常用DOC命令程序,小子亲自尝试
- 10种优化页面加载速度的方法
- 数据库事物
- File---纯小白自己声明给自己的---勿喷
- kubernetes将外部服务映射为内部服务
- 字符串