给静态方法加锁
public class Test {
synchronized static void fun1() throws InterruptedException {
System.out.println("加锁的方法1开始执行");
Thread.sleep(2000);
System.out.println("加锁的方法1执行结束");
}
synchronized static void fun2() throws InterruptedException {
System.out.println("加锁的方法2开始执行");
Thread.sleep(2000);
System.out.println("加锁的方法2执行结束");
}
public static void main(String[] args) {
Test obj1 = new Test();
Test obj2 = new Test();
new Thread(() -> {
try {
obj1.fun1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
try {
obj2.fun2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
效果:
image.png
给普通方法加锁
public class Test {
synchronized void fun1() throws InterruptedException {
System.out.println("加锁的方法1开始执行");
Thread.sleep(2000);
System.out.println("加锁的方法1执行结束");
}
synchronized void fun2() throws InterruptedException {
System.out.println("加锁的方法2开始执行");
Thread.sleep(2000);
System.out.println("加锁的方法2执行结束");
}
public static void main(String[] args) {
Test obj1 = new Test();
Test obj2 = new Test();
new Thread(() -> {
try {
obj1.fun1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
try {
obj2.fun2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
效果:
image.png












网友评论