Android学习:线程同步之synchronized
synchronized:Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码。
一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。
二、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。
三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。
四、第三个例子同样适用其它同步代码块。也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。
五、以上规则对其它对象锁同样适用.
举例说明:
一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。
package com.tongseng.threadlock;import android.util.Log;/*** Created by tongseng on 2017/8/31.* e-mail:tongseng.wu@qq.com* Function:*/public class ThreadLock_Parallel implements Runnable{@Overridepublic void run() {synchronized (this){for (int i = 0; i < 3; i++) {try {Thread.sleep(500);}catch (InterruptedException e){e.printStackTrace();}Log.d("ThreadLock_Parallel:", Thread.currentThread().getName() + " synchronized loop " + i);}}}
}
在Activity中同时创建两个线程,并同时去执行该线程:
private void ThreadLockParallelShow(){ThreadLock_Parallel lock_parallel = new ThreadLock_Parallel();Thread thread01 = new Thread(lock_parallel,"A");Thread thread02 = new Thread(lock_parallel,"B");thread01.start();thread02.start();}
查看执行结果:
01-03 19:55:05.692 11476-11476/com.tongseng.threadlock D/ThreadLock: Welcome to tongseng's Blog!
synchronized key word
----------------------
01-03 19:55:06.194 11476-11520/com.tongseng.threadlock D/ThreadLock_Parallel:: A synchronized loop 0
01-03 19:55:06.695 11476-11520/com.tongseng.threadlock D/ThreadLock_Parallel:: A synchronized loop 1
01-03 19:55:07.195 11476-11520/com.tongseng.threadlock D/ThreadLock_Parallel:: A synchronized loop 2
01-03 19:55:07.696 11476-11521/com.tongseng.threadlock D/ThreadLock_Parallel:: B synchronized loop 0
01-03 19:55:08.196 11476-11521/com.tongseng.threadlock D/ThreadLock_Parallel:: B synchronized loop 1
01-03 19:55:08.697 11476-11521/com.tongseng.threadlock D/ThreadLock_Parallel:: B synchronized loop 2
看到,只有A线程执行完成之后才回去执行B线程。
二、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。
package com.tongseng.threadlock;import android.util.Log;/*** Created by tongseng on 2017/8/31.* e-mail:tongseng.wu@qq.com* Function:*/public class ThreadLock_UnParallel {public void method01() {synchronized(this) {int i = 3;while( i-- > 0) {Log.d("ThreadLock_UnParallel", Thread.currentThread().getName() + " : " + i);try {Thread.sleep(500);} catch (InterruptedException ie) {}}}}/*** 当一个线程访问object的一个synchronized(this)同步代码块时,* 另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。*/public void method02() {int i = 3;while( i-- > 0) {Log.d("ThreadLock_UnParallel", Thread.currentThread().getName() + " : " + i);try {Thread.sleep(300);} catch (InterruptedException ie) {}}}
}
在Activity中同时创建两个线程,并同时去执行该线程:
private void ThreadLockUnParallelShow(){final ThreadLock_UnParallel lock_unParallel = new ThreadLock_UnParallel();Thread thread01 = new Thread(new Runnable() {@Overridepublic void run() {lock_unParallel.method01();}}, "A");Thread thread02 = new Thread(new Runnable() {@Overridepublic void run() {lock_unParallel.method02();}},"B");thread01.start();thread02.start();}
查看执行结果:
01-03 20:01:00.059 16589-16589/? D/ThreadLock: Welcome to tongseng's Blog!
synchronized key word
----------------------
01-03 20:01:00.061 16589-16606/? D/ThreadLock_UnParallel: B : 2
01-03 20:01:00.061 16589-16605/? D/ThreadLock_UnParallel: A : 2
01-03 20:01:00.362 16589-16606/? D/ThreadLock_UnParallel: B : 1
01-03 20:01:00.561 16589-16605/? D/ThreadLock_UnParallel: A : 1
01-03 20:01:00.662 16589-16606/? D/ThreadLock_UnParallel: B : 0
01-03 20:01:01.062 16589-16605/? D/ThreadLock_UnParallel: A : 0
可以看到,A线程在synchronize之后B线程也是同时执行的,并没有因为A线程被加锁就导致阻塞。
三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。
public class ThreadLock {public void method01() {synchronized (this) {int i = 0;while (i++ < 3) {Log.d("ThreadLock", Thread.currentThread().getName() + ":" + i);try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}}}/*** 当一个线程访问object的一个synchronized(this)同步代码块时,* 其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。*/public void method02() {synchronized (this) {int j = 0;while (j++ < 3) {Log.d("ThreadLock", Thread.currentThread().getName() + ":" + j);try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}}}
在Activity中同时创建两个线程,并同时去执行该线程:
private void ThreadLockShow(){final ThreadLock lock = new ThreadLock();Thread thread01 = new Thread(new Runnable() {@Overridepublic void run() {lock.method01();}},"A");Thread thread02 = new Thread(new Runnable() {@Overridepublic void run() {lock.method02();}},"B");thread01.start();thread02.start();}
执行结果如下:
01-03 20:10:12.895 24398-24398/? D/ThreadLock: Welcome to tongseng's Blog!
synchronized key word
----------------------
01-03 20:10:12.898 24398-24414/com.tongseng.threadlock D/ThreadLock: A:1
01-03 20:10:13.398 24398-24414/com.tongseng.threadlock D/ThreadLock: A:2
01-03 20:10:13.899 24398-24414/com.tongseng.threadlock D/ThreadLock: A:3
01-03 20:10:14.399 24398-24415/com.tongseng.threadlock D/ThreadLock: B:1
01-03 20:10:14.900 24398-24415/com.tongseng.threadlock D/ThreadLock: B:2
01-03 20:10:15.400 24398-24415/com.tongseng.threadlock D/ThreadLock: B:3
四、第三个例子同样适用其它同步代码块。也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。
/*** 当一个线程访问object的一个synchronized(this)同步代码块时,* 它就获得了这个object的对象锁。* 结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。*/public synchronized void method03(){int k=0;while(k++ < 3){Log.d("ThreadLock", Thread.currentThread().getName() + ":" + k);try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}}
执行结果如下:
01-03 20:10:12.898 24398-24414/com.tongseng.threadlock D/ThreadLock: A:1
01-03 20:10:13.398 24398-24414/com.tongseng.threadlock D/ThreadLock: A:2
01-03 20:10:13.899 24398-24414/com.tongseng.threadlock D/ThreadLock: A:3
01-03 20:10:14.399 24398-24415/com.tongseng.threadlock D/ThreadLock: C:1
01-03 20:10:14.900 24398-24415/com.tongseng.threadlock D/ThreadLock: C:2
01-03 20:10:15.400 24398-24415/com.tongseng.threadlock D/ThreadLock: C:3
也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,他就获得了这个object的对象锁,那么,其它的线程对该object对象所有同步代码部分的访问都被暂时阻塞。
五、以上规则对其它对象锁同样适用:
package com.tongseng.threadlock;import android.util.Log;/*** Created by tongseng on 2017/8/31.* e-mail:tongseng.wu@qq.com* Function:*/public class ThreadLock_InnerClass {class Inner{public void method01() {int i = 3;while(i-- > 0) {Log.d("Inner", Thread.currentThread().getName() + " : Inner.method01()=" + i);try {Thread.sleep(500);} catch(InterruptedException ie) {}}}public void method02() {/*synchronized (this)*/{int i = 3;while(i-- > 0) {Log.d("Inner", Thread.currentThread().getName() + " : Inner.method02()=" + i);try {Thread.sleep(300);} catch(InterruptedException ie) {}}}}}public Inner getInnerInstance(){return new Inner();}
}
调用的地方如下:
private void ThreadLock_InnerClassShow(){ThreadLock_InnerClass lock_innerClass = new ThreadLock_InnerClass();final ThreadLock_InnerClass.Inner inner = lock_innerClass.getInnerInstance();Thread thread01 = new Thread(new Runnable() {@Overridepublic void run() {if (inner != null) {synchronized (inner){inner.method01();}}}},"A");Thread thread02 = new Thread(new Runnable() {@Overridepublic void run() {if (inner != null) {inner.method02();}}},"B");thread01.start();thread02.start();}
执行结果:
尽管线程thread01获得了对Inner的对象锁,但由于线程thread02访问的是同一个Inner中的非同步部分。所以两个线程互不干扰。
thread01 :Inner.method01()=2
thread02 :Inner.method02()=2
thread01 :Inner.method01()=1
thread02 :Inner.method02()=1
thread01 :Inner.method01()=0
thread02 : Inner.method02()=0
如果在method02也做了synchronized的话,那么就是A线程先执行完,然后再去执行B线程了。
Android学习:线程同步之synchronized相关推荐
- JAVA并发编程3_线程同步之synchronized关键字
在上一篇博客里讲解了JAVA的线程的内存模型,见:JAVA并发编程2_线程安全&内存模型,接着上一篇提到的问题解决多线程共享资源的情况下的线程安全问题. 不安全线程分析 public clas ...
- Java线程同步:synchronized锁住的是代码还是对象
在Java中,synchronized关键字是用来控制线程同步的,就是在多线程的环境下,控制synchronized代码段不被多个线程同时执行.synchronized既可以加在一段代码上,也可以加在 ...
- 多线程,线程同步,synchronized关键字的用法
一.什么是多线程 Java多线程实现方式主要有四种:继承Thread类.实现Runnable接口.实现Callable接口通过FutureTask包装器来创建Thread线程.使用ExecutorSe ...
- 并发编程--线程同步之 synchronized关键字(一)
线程同步主要就是两种实现: ReentrantLock 显示锁 Synchronized 隐式锁 一.对synchronized的一个整体认知 1.JDK1.6之前 整体了解那肯定就得从synchro ...
- 线程同步机制synchronized中锁的判断以及锁的作用范围
当我们使用多个线程访问同一资源(可以是同一个变量.同一个文件.同一条记录等)的时候,若多个线程只有读操作,那么不会发生线程安全问题,但是如果多个线程中对资源有读和写的操作,就容易出现线程安全问题. 要 ...
- UNIX再学习 -- 线程同步
一.为什么要线程同步 当多个控制线程共享相同的内存时,需要确保每个线程看到一致的数据视图.如果每个线程使用的变量都是其他线程不会读取和修改的,那么就不存在一致性问题.同样,如果变量时只读的,每个线程同 ...
- Java线程同步机制synchronized关键字的理解
由于同一进程的多个线程共享同一片存储空间,在带来方便的同时,也带来了访问冲突这个严重的问题.Java语言提供了专门机制以解决这种冲突,有效避免了同一个数据对象被多个线程同时访问. 需要明确的几个问题: ...
- Android中线程同步之Mutex与Condtion的用法
简单的线程与锁关系 基本概念 http://blog.csdn.net/thl789/article/details/9879151 数据不一致现象 http://blog.sina.com.cn/s ...
- Java多线程详解(线程同步)
嗨喽-小伙伴们我来了, 上一章,我们通过几个例子(点击跳转)介绍了线程安全问题,而说到线程安全就不得不提到线程同步,它是解决线程安全的一种重要方法.本章就来简单地介绍一下线程同步. 从上一章的学习我们 ...
最新文章
- php js混淆加密工具,求混淆js加密算法解密
- Maven学习总结(七)——eclipse中使用Maven创建Web项目
- WinForm界面开发之“HTML内容编辑控件”
- 四层交换机是什么?有什么用?与二层/三层交换机有何区别?
- CodeForces - 246E Blood Cousins Return(树上启发式合并)
- 华为新系统鸿蒙有哪些手机_如今鸿蒙手机之后,华为启用新系统,谷歌微软措手不及...
- Socket编程入门
- *args, **kwargs
- Django中QuerySet的结果是否为空的判断
- 李嘉诚:孤独是他的能量
- 金蝶专业版过账提示运行时错误5_金蝶KIS专业版常见故障及解决办法
- Opencascade 帮助手册学习1 Overview
- Parallels Desktop 16在Big Sur下网络初始化失败解决办法
- 美国软件供应链安全行动中的科技巨头们
- 自旋锁(spin lock)学习
- oracle12c启动apply,Oracle 12c 新特性 -- DG 默认使用 Real-Time Apply
- halcon坐标转换(机器人坐标转换用)
- kuangbin 专题一 简单搜索
- 浅析TMS320F28035的GPIO模块
- 小程序连接 域名不合法