多线程访问同步方法的7种情况

本文原创作者为慕课网的悟空,仅用于我个人学习使用,整理自:https://www.imooc.com/video/18492

7种情况:
1.两个线程同时访问一个对象的同步方法

/**
 * @program: SynchronizedTest
 * @description: 对象锁示例2,方法锁形式
 * @author: Zhang Shangfeng
 * @create: 2019-07-07 14:43
 * 结果串行输出
 **/
public class test3 implements Runnable {
    static test3 instance = new test3();

    public static void main(String[] args) {
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        while ((t1.isAlive() || t2.isAlive())) {
        }
        System.out.println("finished");
    }

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

    public synchronized void method() {
        System.out.println("我是对象锁的方法修饰符形式,我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");

    }
}

2.两个线程访问的是两个对象的同步方法

/**
 * @program: SynchronizedTest
 * @description: 对象锁实例1,代码块形式
 * @author: Zhang Shangfeng
 * @create: 2019-07-07 14:06
 * 结果两个对象同时结束不受影响
 **/
public class test2 implements Runnable {
    static test2 instance1 = new test2();
    static test2 instance2 = new test2();
    public static void main(String[] args) {
        Thread t1 = new Thread(instance1);
        Thread t2 = new Thread(instance2);
        t1.start();
        t2.start();
        while ((t1.isAlive() || t2.isAlive())) {

        }
        System.out.println("finished");
    }

    @Override
    public void run() {
        synchronized (this) {
            System.out.println("我是对象锁的代码块形式。我叫" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "运行结束。");
        }
    }
}

3.两个线程访问的是synchronized的静态方法

/**
 * @program: SynchronizedTest
 * @description: 类锁的第一种形式,静态方法锁,static
 * @author: Zhang Shangfeng
 * @create: 2019-07-07 14:57
 * 结果串行输出,锁生效
 **/
public class test4 implements Runnable {
    static test4 instance1 = new test4();
    static test4 instance2 = new test4();

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

    public static synchronized void method() {
        System.out.println("我是类锁的第一种形式,我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");

    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(instance1);
        Thread t2 = new Thread(instance2);
        t1.start();
        t2.start();
        while ((t1.isAlive() || t2.isAlive())) {
        }
        System.out.println();
    }
}

4.同时访问同步方法和非同步方法

/**
 * @program: SynchronizedTest
 * @description: 同时访问同步和非同步方法
 * @author: Zhang Shangfeng
 * @create: 2019-07-07 15:58
 * 结果非同步方法不受影响,同时结束
 **/
public class test6 implements Runnable {
    static test6 instance=new test6();
    @Override
    public void run() {
        if(Thread.currentThread().getName().equals("Thread-0")){
            method1();
        }else {
            method2();
        }
    }
    public synchronized void method1(){
        System.out.println("我是加锁的方法,我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");

    }

    public  void method2(){
        System.out.println("我是不加锁的方法,我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");

    }
    public static void main(String[] args){
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        while ((t1.isAlive() || t2.isAlive())) {
        }
        System.out.println("finished");
    }
}

5.访问同一个对象的不同的同步方法

/**
 * @program: SynchronizedTest
 * @description: 同时访问一个类的不同的普通同步方法
 * @author: Zhang Shangfeng
 * @create: 2019-07-07 16:03
 * 结果串行输出
 **/
public class test7 implements Runnable {
    static test7 instance=new test7();
    @Override
    public void run() {
        if(Thread.currentThread().getName().equals("Thread-0")){
            method1();
        }else {
            method2();
        }
    }
    public synchronized void method1(){
        System.out.println("我是加锁的方法1,我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");

    }

    public synchronized void method2(){
        System.out.println("我是加锁的方法2,我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");

    }
    public static void main(String[] args){
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        while ((t1.isAlive() || t2.isAlive())) {
        }
        System.out.println("finished");
    }
}

6.同时访问静态synchronized和非静态synchronized方法

/**
 * @program: SynchronizedTest
 * @description:
 * @author: Zhang Shangfeng
 * @create: 2019-07-07 16:08
 * 结果同时结束
 **/
public class test8 implements Runnable{
    static test8 instance=new test8();
    @Override
    public void run() {
        if(Thread.currentThread().getName().equals("Thread-0")){
            method1();
        }else {
            method2();
        }
    }
    public synchronized static void method1(){
        System.out.println("我是静态的加锁方法1,我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");

    }

    public synchronized void method2(){
        System.out.println("我是非静态的加锁方法2,我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");

    }
    public static void main(String[] args){
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        while ((t1.isAlive() || t2.isAlive())) {
        }
        System.out.println("finished");
    }
}

7.方法抛出异常后,会释放锁

/**
 * @program: SynchronizedTest
 * @description: 方法抛出异常后,会释放锁。展示不抛出异常前和抛出异常后的对比。
 * 一旦抛出异常,第二个线程会立刻进入同步方法,意味着锁已经释放
 * @author: Zhang Shangfeng
 * @create: 2019-07-07 16:12
 **/
public class test9 implements Runnable{
    static test9 instance=new test9();
    @Override
    public void run() {
        if(Thread.currentThread().getName().equals("Thread-0")){
            method1();
        }else {
            method2();
        }
    }
    public synchronized  void method1(){
        System.out.println("我是抛出异常的方法1,我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
          
        } catch (InterruptedException e) {
            e.printStackTrace();
       
             throw new RuntimeException();
             //System.out.println(Thread.currentThread().getName() + "运行结束");

    }

    public synchronized void method2(){
        System.out.println("我是不抛出异常的方法2,我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");

    }
    public static void main(String[] args){
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
        while ((t1.isAlive() || t2.isAlive())) {
        }
        System.out.println("finished");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值