synchronized关键字-线程同步总结+测试用例

package com.xch.synchronized_test;

import java.util.concurrent.TimeUnit;

/**
 * synchronized关键字使用
 * 解释:实现线程同步,让多个线程排队依次获取某个资源
 * -修饰方法
 * -静态方法:锁定的是类
 * -非静态方法:锁定的是方法的调用者
 * -修饰代码块:锁定的是传入的对象
 *
 * @author XuChenghe
 * @date 2023/3/14 16:59
 */
public class Test01 {
    /**
     * 非静态方法,锁定的是方法的调用者
     * 由于data只有一个,所以先后有序执行
     */
    public static void main(String[] args) throws InterruptedException {
        Data data = new Data();
        // 子线程A
        new Thread(data::func1).start();
        Thread.sleep(1000);
        // 子线程B
        new Thread(data::func2).start();
    }
}

class Test01_1 {
    /**
     * 非静态方法,锁定的是方法的调用者
     * 由于分别new Data(),所以并发执行
     */
    public static void main(String[] args) throws InterruptedException {
        // 子线程A
        new Thread(new Data()::func1).start();
        Thread.sleep(1000);
        // 子线程B
        new Thread(new Data()::func2).start();
    }
}

/**
 * 修饰非静态方法
 */
class Data {
    public synchronized void func1() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("1...");
    }
    
    public synchronized void func2() {
        System.out.println("2...");
    }
}

class Test02 {
    /**
     * 静态方法,锁定的是类
     * 所以先后有序执行
     */
    public static void main(String[] args) throws InterruptedException {
        // 子线程A
        new Thread(Data1::func1).start();
        Thread.sleep(1000);
        // 子线程B
        new Thread(Data1::func2).start();
    }
}

/**
 * 修饰静态方法
 */
class Data1 {
    public synchronized static void func1() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("1...");
    }
    
    public synchronized static void func2() {
        System.out.println("2...");
    }
}

class Test03 {
    /**
     * 代码块,锁定的是传入的对象
     * 由于data只有一个,所以先后有序执行
     */
    public static void main(String[] args) {
        Data2 data2 = new Data2();
        for (int threadId = 0; threadId < 5; threadId++) {
            // 子线程n
            new Thread(data2::func1).start();
        }
    }
}

class Test03_1 {
    /**
     * 代码块,锁定的是传入的对象this
     * 由于分别new Data(),所以并发执行
     */
    public static void main(String[] args) {
        for (int threadId = 0; threadId < 5; threadId++) {
            // 子线程n
            new Thread(new Data2()::func1).start();
        }
    }
}

/**
 * 修饰代码块
 */
class Data2 {
    public void func1() {
        synchronized (this) {
            System.out.println("start...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("end...");
        }
    }
}

class Test04 {
    /**
     * 代码块,锁定的是传入的对象Data3.class
     * 所以先后有序执行
     */
    public static void main(String[] args) {
        for (int threadId = 0; threadId < 5; threadId++) {
            // 子线程n
            new Thread(new Data3()::func1).start();
        }
    }
}

/**
 * 修饰代码块
 */
class Data3 {
    public void func1() {
        synchronized (Data3.class) {
            System.out.println("start...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("end...");
        }
    }
}

class Test05 {
    /**
     * 代码块,锁定的是传入的对象num
     * 所以先后有序执行
     */
    public static void main(String[] args) {
        for (int threadId = 0; threadId < 5; threadId++) {
            // 子线程n
            new Thread(new Data4()::func1).start();
        }
    }
}

/**
 * 修饰代码块
 */
class Data4 {
    public void func1() {
        Integer num = 1;
        System.out.println(Thread.currentThread().getName() + "|" + num);
        // 所有线程均阻塞在此处,所以num均为同一个1
        synchronized (num) {
            ++num;
            System.out.println("start...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("end...");
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BB-X

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值