03.Java 多线程 - synchronized
2016-06-07 21:12
483 查看
基本概念
synchronized 的定义:字面的意思是同步,也称为锁、同步锁。
synchronized 的作用:
它保证了同一时间,多个线程在访问某个对象时,有且只有一个线程可以操作该对象。即不同线程对同步锁的访问是互斥的。
synchronized 的理解:
好比厕所,每次只能一个人使用。一旦被占用,而其他人必须在外面等待。而 synchronized 就是厕所的锁,一旦上锁,其他人无法访问。
作用范围
synchronized 只对对象生效,但是作用的范围可以是一段代码(代码块),也可以是整个方法。1.修饰代码块
synchronized 修饰代码块时。这时候对象可以有三种形式使用成员变量替代对象,如果是静态成员变量代表作用在该类的所有对象。
class MyRunnable implements Runnable { private byte[] lock = new byte[0]; public void run() { synchronized (lock) { //dosomething... } }
使用 this 关键字,代表作用在该类的一个对象上。
class MyRunnable implements Runnable { public void run() { synchronized (this) { //dosomething... } }
使用类,表示作用在该类的所有对象
class MyRunnable implements Runnable { public void run() { synchronized (MyRunnable.class) { //dosomething... } }
2.修饰方法
synchronized 也可以修饰方法,包括静态方法与非静态方法。作用在非静态方法上,只对该类的一个对象生效
class MyRunnable implements Runnable { public synchronized void print() { //dosomething... } public void run() { print(); } }
作用在静态方法上,对该类的所有对象生效
class MyRunnable implements Runnable { public synchronized static void print() { //dosomething... } public void run() { print(); } }
实例探究
1.synchronized 作用对象
synchronized 只能作用在对象上,到使其生效的前提是作用在同一个对象。首先来看 synchronized 失效的例子:
// 线程 public class MyThread extends Thread { public void print() { synchronized (this) { for (int i = 0; i < 3; i++) { System.out.println(this.getName() + "- print -" + i); } } } public void run() { print(); } } // 调用 MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); MyThread t3 = new MyThread(); t1.start(); t2.start(); t3.start(); // 结果: // Thread-0- print -0 // Thread-1- print -0 // Thread-0- print -1 // Thread-1- print -1 // Thread-0- print -2 // Thread-1- print -2 // Thread-2- print -0 // Thread-2- print -1 // Thread-2- print -2
观察输出结果,发现同一时间段存在多个线程访问同步代码块,说明 synchronized 失效 :
这里使用 this 关键字表示 Mythread 的对象,准确来讲是一个对象。
在调用过程中,Mythread 类创建了几个不同的对象。
synchronized 之所以生效,就是因为它锁住的是 Mythread 类各自的对象,它们之间是相互不干扰的。
下面来探究要如何修正它,既然 synchronized 作用的是不同的对象,那么让它作用同一对象,效果自然就出来了。这里有四种方式:
使用 Runnable 代替 Thread 。此时 this 代表的是 MyRunnable 类的对象,而调用时所有的线程都访问了同一个 MyRunnable 对象。
// 线程 public class MyRunnable implements Runnable { public void run() { synchronized (this) { print(); } } // 省略代码... } // 调用 Thread t1 = new Thread(mr,"t1"); Thread t2 = new Thread(mr,"t2"); Thread t3= new Thread(mr,"t3"); t1.start(); t2.start(); t3.start();
将 print 修改为静态方法。 synchronized 修饰静态方法时,是作用在该类的所有对象上。
public class MyThread extends Thread { // 省略部代码... public synchronized static void print() { // do something... } }
将 this 修改为全局变量,原理同上。
public class MyThread extends Thread { // 省略代码... private static byte [ ] lock = new byte[0]; public void print() { synchronized (lock) { // do something... } } }
将 this 修改为类,synchronized 修饰类时,代表作用在该类的所有对象上。
public class MyThread extends Thread { // 省略代码... public void print() { synchronized (MyThread.class) { // do something... } } }
2. synchronized 方法继承
synchronized 关键字不能被继承的,因为它并不属于方法的一部分。在父类中的某个方法使用了 synchronized 关键字,而在子类覆写了该方法,在子类中的这个方法默认情况下并不是同步的。
如果子类也想要实现同步效果,可以采取以下两种方式:
显示地加上 synchronized 关键字
// 父类 public class Parent { public synchronized void method() { } } // 子类:显示地加上 synchronized 关键字 public class Child extends Parent { public synchronized void method() { } }
直接调用父类的方法
public class Child extends Parent { public void method() { super.method(); } }
3. 同步方法 & 未同步方法
在一个类中,访问 synchronized 修饰的代码块或方法时:其他被 synchronized 修饰的代码块或方法也将被锁住。
其他不被 synchronized 修饰的代码块或方法则可以正常访问。
public class MyRunnable implements Runnable { public synchronized void talk() { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + "-print-" + i); } } public void say() { synchronized (this) { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + "-say-" + i); } } } public void read() { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + "-read-" + i); } } public void run() { talk(); say(); read(); } } // 调用: MyRunnable mr = new MyRunnable(); Thread t1 =new Thread(mr,"t1"); Thread t2 =new Thread(mr,"t2"); Thread t3 =new Thread(mr,"t3"); t1.start(); t2.start(); t3.start();
结果如下
// 结果: t1-print-0 t1-print-1 t1-print-2 t1-say-0 t1-say-1 t1-say-2 t1-read-0 t1-read-1 t1-read-2 t3-print-0 t3-print-1 t3-print-2 t3-say-0 t3-say-1 t3-say-2 t3-read-0 // 关键 t2-print-0 t2-print-1 t2-print-2 t2-say-0 t2-say-1 t3-read-1 t3-read-2 t2-say-2 t2-read-0 t2-read-1 t2-read-2
参考
http://wangkuiwu.github.io/2012/08/04/threads-basic/相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- Python3写爬虫(四)多线程实现数据爬取
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- 使用 Syncthing 在多个设备间同步文件
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序