验证同步函数的锁是this
/**
* Created by Perk on 2016/7/17.
*/
class Ticket implements Runnable {
private int num = 200;
Object object = new Object();
boolean flag = true;
@Override
public void run() {
if (flag) {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (obj) {
if (num > 0) {
System.out.println(num + ":" + Thread.currentThread().getName() + "synblock");
num--;
}
}
}
} else {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
show();
}
}
}
public synchronized void show() {
if (num > 0) {
System.out.println(num + ":" + Thread.currentThread().getName() + "function");
num--;
}
}
}
public class TicketDemo {
public static void main(String[] args) {
Ticket ticket1=new Ticket();
Thread thread1=new Thread(ticket1);
Thread thread2=new Thread(ticket1);
thread1.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
ticket1.flag=false;
thread2.start();
}
}
通过设置flag 使两个线程分别执行同步代码块和同步函数的代码。
synchronized (this) {
if (num > 0) {
System.out.println(num + ":" + Thread.currentThread().getName() + "synblock");
num--;
}
}
将同步代码块的锁换成this.发现同步安全问题解决了,所有同步函数使用的同步锁是this.
如果在同步函数上加上static关键字则又会出现同步安全问题。
public static synchronized void show() {
if (num > 0) {
System.out.println(num + ":" + Thread.currentThread().getName() + "function");
num--;
}
}
将同步代码块的锁设为this.getClass()或 Ticket.class可以解决同步安全问题
synchronized (this.getClass()) { //Ticket.class
if (num > 0) {
System.out.println(num + ":" + Thread.currentThread().getName() + "synblock");
num--;
}
}