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...");
}
}
}
synchronized关键字-线程同步总结+测试用例
于 2023-03-14 21:10:52 首次发布