synchronized 笔记
[color=blue]1、synchronized实例方法[/color]
[code]synchronized void method(){
...
}[/code]
在功能上,它相当于
[code]void method() {
synchronized (this) {
...
}
}[/code]
[color=blue]2、synchronized类方法[/color]
[code]class Something {
static synchronized void method(){
...
}
}[/code]
在功能上,它相当于
[code]class Something {
static void method() {
synchronized (Something.class){
...
}
}
}[/code]
[color=blue]
3、如果我们也可以自定义方法来实现相当于synchronized类似的功能,代码如下:[/color]
[code]void method() {
lock();
try{
...
}finally{
unlock();
}
}[/code]
以上的代码就是一个Before/After Pattern的一种实现方式
举例:
下面中Mutex类这种用来进行共享互斥的机制,一般称为mutex。
[code]public class Gate {
private int counter = 0;
private String name = "Nobody";
private String address = "Nowhere";
private final Mutex mutex = new Mutex();
public void pass(String name, String address) { // 并非synchronized
mutex.lock();
try {
this.counter++;
this.name = name;
this.address = address;
check();
} finally {
mutex.unlock();
}
}
public String toString() { // 并非synchronized
String s = null;
mutex.lock();
try {
s = "No." + counter + ": " + name + ", " + address;
} finally {
mutex.unlock();
}
return s;
}
private void check() {
if (name.charAt(0) != address.charAt(0)) {
System.out.println("***** BROKEN ***** " + toString());
}
}
}[/code]
设计简单的Mutex类1:
[code]public final class Mutex {
private boolean busy = false;
public synchronized void lock() {
while (busy) {
try {
wait();
} catch (InterruptedException e) {
}
}
busy = true;
}
public synchronized void unlock() {
busy = false;
notifyAll();
}
}
[/code]
设计完善的Mutex类2:
[code]public final class Mutex {
private long locks = 0;
private Thread owner = null;
public synchronized void lock() {
Thread me = Thread.currentThread();
while (locks > 0 && owner != me) {
try {
wait();
} catch (InterruptedException e) {
}
}
// locks == 0 || owner == me
owner = me;
locks++;
}
public synchronized void unlock() {
Thread me = Thread.currentThread();
if (locks == 0 || owner != me) {
return;
}
// locks > 0 && owner == me
locks--;
if (locks == 0) {
owner = null;
notifyAll();
}
}
}[/code]
[color=blue]4、我们看到synchronized块的时候,要先思考"synchronized是在保护什么",然后进一步思考"获取谁的锁定来保护的呢"[/color]
要调用synchronized实例方法的纯种,一定会获取thid的锁定。一个实例的锁定,同一个时间内,只能有个线程可以得到。
如果实例不同,那锁定也不同了。使用synchronized块的时候,特别需要考虑"获取谁的锁定来保护的呢"这种情况。因为
synchronized需要明确地指明要获取的是哪个对象的锁定。例如:
[code] synchronized (obj) {
...
}[/code]
这样的程序代码中,obj就是我们所要获取锁定的对象。请小心这个对象不可心写错,获取错误对象的锁定,就好想要保护自己
自己的家,却把别人家的门给锁定了。
注:代码来自 Java多线程设计模式 第一章SingleThreadedExecution方面的内容
[color=blue]1、synchronized实例方法[/color]
[code]synchronized void method(){
...
}[/code]
在功能上,它相当于
[code]void method() {
synchronized (this) {
...
}
}[/code]
[color=blue]2、synchronized类方法[/color]
[code]class Something {
static synchronized void method(){
...
}
}[/code]
在功能上,它相当于
[code]class Something {
static void method() {
synchronized (Something.class){
...
}
}
}[/code]
[color=blue]
3、如果我们也可以自定义方法来实现相当于synchronized类似的功能,代码如下:[/color]
[code]void method() {
lock();
try{
...
}finally{
unlock();
}
}[/code]
以上的代码就是一个Before/After Pattern的一种实现方式
举例:
下面中Mutex类这种用来进行共享互斥的机制,一般称为mutex。
[code]public class Gate {
private int counter = 0;
private String name = "Nobody";
private String address = "Nowhere";
private final Mutex mutex = new Mutex();
public void pass(String name, String address) { // 并非synchronized
mutex.lock();
try {
this.counter++;
this.name = name;
this.address = address;
check();
} finally {
mutex.unlock();
}
}
public String toString() { // 并非synchronized
String s = null;
mutex.lock();
try {
s = "No." + counter + ": " + name + ", " + address;
} finally {
mutex.unlock();
}
return s;
}
private void check() {
if (name.charAt(0) != address.charAt(0)) {
System.out.println("***** BROKEN ***** " + toString());
}
}
}[/code]
设计简单的Mutex类1:
[code]public final class Mutex {
private boolean busy = false;
public synchronized void lock() {
while (busy) {
try {
wait();
} catch (InterruptedException e) {
}
}
busy = true;
}
public synchronized void unlock() {
busy = false;
notifyAll();
}
}
[/code]
设计完善的Mutex类2:
[code]public final class Mutex {
private long locks = 0;
private Thread owner = null;
public synchronized void lock() {
Thread me = Thread.currentThread();
while (locks > 0 && owner != me) {
try {
wait();
} catch (InterruptedException e) {
}
}
// locks == 0 || owner == me
owner = me;
locks++;
}
public synchronized void unlock() {
Thread me = Thread.currentThread();
if (locks == 0 || owner != me) {
return;
}
// locks > 0 && owner == me
locks--;
if (locks == 0) {
owner = null;
notifyAll();
}
}
}[/code]
[color=blue]4、我们看到synchronized块的时候,要先思考"synchronized是在保护什么",然后进一步思考"获取谁的锁定来保护的呢"[/color]
要调用synchronized实例方法的纯种,一定会获取thid的锁定。一个实例的锁定,同一个时间内,只能有个线程可以得到。
如果实例不同,那锁定也不同了。使用synchronized块的时候,特别需要考虑"获取谁的锁定来保护的呢"这种情况。因为
synchronized需要明确地指明要获取的是哪个对象的锁定。例如:
[code] synchronized (obj) {
...
}[/code]
这样的程序代码中,obj就是我们所要获取锁定的对象。请小心这个对象不可心写错,获取错误对象的锁定,就好想要保护自己
自己的家,却把别人家的门给锁定了。
注:代码来自 Java多线程设计模式 第一章SingleThreadedExecution方面的内容