线程系列(七)synchronized使用方式

Java中的每一个对象都可以作为锁。具体表现为以下3种形式。

  1. 对于普通同步方法,锁是当前实例对象。
  2. 对于静态同步方法,锁是当前类的Class对象。
  3. 对于同步方法块,锁是Synchonized括号里配置的对象。

synchronized使用方式

分类 具体分类 锁对象 示例
修饰方法 实例方法 类的实例对象

//修饰普通方法

public synchronized void method(){

}

静态方法 类对象

//修饰静态方法

public static synchronized void method(){

}

修饰代码块 实例对象 类的实例对象

//同步代码块,锁是实例对象

synchronized (this){

}

class对象

类对象

//同步代码块,锁是实例对象

synchronized (CLASS){

}

不同实例对象Object 实例对象Object

//同步代码块,锁是配置的实例对象

Object object = new Object();

synchronized (object ){

}

synchronized 修饰实例方法

/**
 * 两个线程同时对一个对象的一个方法进行操作,只有一个线程能够抢到锁。
 * 因为一个对象只有一把锁,一个线程获取了该对象的锁之后,其他线程无法获取该对象的锁,
 * 就不能访问该对象的其他synchronized实例方法,但是可以访问非synchronized修饰的方法
 */
public class SynchronizedDemo1 implements Runnable{

    public static void main(String[] args) throws InterruptedException {
    	SynchronizedDemo1 test = new SynchronizedDemo1();
        Thread t1 = new Thread(test, "Thread1");
        Thread t2 = new Thread(test, "Thread2");
        t1.start();
        t2.start();
        System.out.println(counter);
    }
    /**
     * 共享资源
     */
    static int counter = 0;
    
	@Override
	public void run() {
		increase();
	}
    /**
     * synchronized 修饰实例方法
     */
    public synchronized void increase() {
        for (int j = 0; j < 10; j++) {
            System.out.println(Thread.currentThread().getName() + "执行累加操作。。。");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            counter++;
        }
    }
}

synchronized 修饰静态方法

/**
 * @Description: synchronized作用于静态方法
 *
 * 两个线程实例化两个不同的对象,但是访问的方法是静态的,两个线程发生了互斥(即一个线程访问,另一个线程只能等着),
 * 因为静态方法是依附于类而不是对象的,当synchronized修饰静态方法时,锁是class对象。
 */
public class SynchronizedTest05 implements Runnable {
    /**
     * 共享资源
     */
    static int counter = 0;

    /**
     * synchronized 修饰静态方法方法
     */
    public static synchronized void increase() {
        for (int j = 0; j < 10; j++) {
            System.out.println(Thread.currentThread().getName() + "执行累加操作。。。");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            counter++;
        }
    }

    @Override
    public void run() {
        increase();
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new SynchronizedTest05(), "线程1");
        Thread t2 = new Thread(new SynchronizedTest05(), "线程2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter);
    }
}

synchronized 修饰class对象

/**
 * 修饰类 class
 */
public class SynchronizedTest07 implements Runnable {

    static int counter = 0;

    @Override
    public void run() {
        //省略其他耗时操作....
        //使用同步代码块对变量i进行同步操作,锁对象为instance
        synchronized (SynchronizedTest07.class) {
            for (int j = 0; j < 10; j++) {
                System.out.println(Thread.currentThread().getName() + "执行累加操作。。。");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                counter++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        final SynchronizedTest07 test = new SynchronizedTest07();
        Thread t1 = new Thread(test, "线程1");
        Thread t2 = new Thread(test,"线程2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter);
    }
}
发布了55 篇原创文章 · 获赞 3 · 访问量 5254

猜你喜欢

转载自blog.csdn.net/qq_38130094/article/details/103537663