一、同步方法
public synchronized void A(){
//...
}
锁定的是调用这个同步方法的对象
测试:
a、不使用这个关键字修饰方法,两个线程调用同一个对象的这个方法。
目标类:
public class BusinessLogic{
public void execute(){
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
线程类:
public class RunnableImpl implements Runnable{
BusinessLogic thread = null;
public RunnableImpl(BusinessLogic thread){
this.thread = thread;
}
public void run(){
thread.execute();
}
}
调用:
public class Main {
public static void main(String[] args) throws Exception{
BusinessLogic test = new BusinessLogic();
Runnable runnable = new RunnableImpl(test);
Thread a = new Thread(runnable, "A");
a.start();
Thread b = new Thread(runnable, "B");
b.start();
}
}
结果:
[color=green]输出的数字交错在一起。说明不是同步的,两个方法在不同的线程中是异步调用的。[/color]
b、修改目标类,增加synchronized修饰:
public class BusinessLogic {
public synchronized void execute(){
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
结果:
[color=green]输出的数字是有序的,首先输出A的数字,然后是B,说明是同步的,虽然是不同的线程,但两个方法是同步调用的。
注意:上面虽然是两个不同的线程,但是是同一个实例对象。下面使用不同的实例对象进行测试。[/color]
c、每个线程都有独立的TestThread对象。
目标类:
public class BusinessLogic {
public synchronized void execute(){
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
调用:
public class Main {
public static void main(String[] args) throws Exception{
BusinessLogic test = new BusinessLogic();
Runnable runnable = new RunnableImpl(test);
Thread a = new Thread(runnable, "A");
a.start();
BusinessLogic test2 = new BusinessLogic();
Runnable runnable2 = new RunnableImpl(test2);
Thread b = new Thread(runnable2, "B");
b.start();
}
}
结果:
[color=green]输出的数字交错在一起。说明虽然增加了synchronized 关键字来修饰方法,但是不同的线程调用各自的对象实例,两个方法仍然是异步的。[/color]
引申:
对于这种多个实例,要想实现同步即输出的数字是有序并且按线程先后顺序输出,我们可以增加一个静态变量,对它进行加锁(后面将说明锁定的对象)。
修改目标类:
public class BusinessLogic {
private static Object lock = new Object(); //It must be a static Object
public void execute(){
synchronized(lock){
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
}
二、同步代码块
锁定一个对象,其实锁定的是该对象的引用(object reference)
谁拿到这个锁谁就可以运行它所控制的那段代码。当有一个明确的对象作为锁时,就可以按上面的代码写程序,但当没有明确的对象作为锁,只是想让一段代码同步时,可以创建一个特殊的instance变量(它必须是一个对象)来充当锁(上面的解决方法就是增加了一个状态锁)。
a、锁定一个对象,它不是静态的
public class BusinessLogic {
private Object lock=new Object(); // It is not a static Object
public void execute(){
synchronized(lock){ //增加了个锁,锁定了对象lock,在同一个类实例中,是线程安全的,但不同的实例还是不安全的。因为不同的实例有不同对象锁lock
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
}
其实上面锁定一个方法,等同于下面的:
public void execute(){
synchronized(this){
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
b、锁定一个对象或方法,它是静态的
这样锁定,它锁定的是对象所属的类
public class BusinessLogic {
public synchronized static void execute(){
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
等同于:
public class BusinessLogic {
private static Object lock = new Object(); //It must be a static Object
public void execute(){
synchronized(lock){
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
}
c、注意:
[color=green]1、用synchronized 来锁定一个对象的时候,如果这个对象在锁定代码段中被修改了,则这个锁也就消失了。[/color]看下面的实例:
目标类:
public class BusinessLogic {
private static Object lock = new Object();
public void execute(){
synchronized(lock){
lock = "abc";
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
}
调用:
public class Main {
public static void main(String[] args) throws Exception{
BusinessLogic test = new BusinessLogic();
Runnable runnable = new RunnableImpl(test);
Thread a = new Thread(runnable, "A");
a.start();
BusinessLogic test2 = new BusinessLogic();
Runnable runnable2 = new RunnableImpl(test2);
Thread b = new Thread(runnable2, "B");
b.start();
}
}
不注释lock = "abc";输出是无序得。
同理:
目标类:
public class BusinessLogic {
private Object lock = new Object();
public void execute(){
synchronized(lock){
lock = "abc";
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName() +":" + i);
}
}
}
}
调用:
public class Main {
public static void main(String[] args) throws Exception{
BusinessLogic test = new BusinessLogic();
Runnable runnable = new RunnableImpl(test);
Thread a = new Thread(runnable, "A");
a.start();
Thread b = new Thread(runnable, "B");
b.start();
}
}
除非lock = "abc"被注释,否则输出为无序。