🚀在 Loki的个人博客,阅读体验更佳
下面将通过经典的8锁问题,认清锁原理
场景一
import java.util.concurrent.TimeUnit;
public class LockDemo1 {
public static void main(String[] args) throws InterruptedException {
Phone1 phone1 = new Phone1();
new Thread(()->{
phone1.sendEmail();
},"A").start();
TimeUnit.SECONDS.sleep(3);
new Thread(()->{
phone1.callPhone();
},"B").start();
}
}
class Phone1{
public synchronized void sendEmail(){
System.out.println("senEmail");
}
public synchronized void callPhone(){
System.out.println("callPhone");
}
}
场景二
import java.util.concurrent.TimeUnit;
public class LockDemo2 {
public static void main(String[] args) throws InterruptedException {
Phone2 phone2 = new Phone2();
new Thread(()->{
try {
phone2.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
TimeUnit.SECONDS.sleep(2);
new Thread(()->{
phone2.callPhone();
},"B").start();
}
}
class Phone2{
public synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
public synchronized void callPhone(){
System.out.println("callPhone");
}
}
场景三
import java.util.concurrent.TimeUnit;
public class LockDemo3 {
public static void main(String[] args) throws InterruptedException {
Phone3 phone3 = new Phone3();
new Thread(()->{
try {
phone3.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
TimeUnit.SECONDS.sleep(2);
new Thread(()->{
phone3.callPhone();
},"B").start();
}
}
class Phone3{
public synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(4);
System.out.println("sendEmail");
}
public void callPhone(){
System.out.println("callPhone");
}
}
场景四
import java.util.concurrent.TimeUnit;
public class LockDemo4 {
public static void main(String[] args) throws InterruptedException {
Phone4 phoneA = new Phone4();
Phone4 phoneB = new Phone4();
new Thread(()->{
try {
phoneA.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phoneB.callPhone();
},"B").start();
}
}
class Phone4{
public synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
public synchronized void callPhone(){
System.out.println("callPhone");
}
}
场景五
import java.util.concurrent.TimeUnit;
public class LockDemo5 {
public static void main(String[] args) throws InterruptedException {
Phone5 phone5 = new Phone5();
new Thread(()->{
try {
phone5.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone5.callPhone();
},"B").start();
}
}
class Phone5{
public static synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
public static synchronized void callPhone(){
System.out.println("callPhone");
}
}
场景六
import java.util.concurrent.TimeUnit;
public class LockDemo6 {
public static void main(String[] args) throws InterruptedException {
Phone6 phone6 = new Phone6();
new Thread(()->{
try {
phone6.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone6.callPhone();
},"B").start();
}
}
class Phone6{
public static synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
public synchronized void callPhone(){
System.out.println("callPhone");
}
}
场景七
import java.util.concurrent.TimeUnit;
public class LockDemo7 {
public static void main(String[] args) throws InterruptedException {
Phone7 phoneA = new Phone7();
Phone7 phoneB = new Phone7();
new Thread(()->{
try {
phoneA.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phoneB.callPhone();
},"B").start();
}
}
class Phone7{
public static synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
public static synchronized void callPhone(){
System.out.println("callPhone");
}
}
场景八
import java.util.concurrent.TimeUnit;
public class LockDemo8 {
public static void main(String[] args) throws InterruptedException {
Phone8 phoneA = new Phone8();
Phone8 phoneB = new Phone8();
new Thread(()->{
try {
phoneA.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phoneB.callPhone();
},"B").start();
}
}
class Phone8{
public static synchronized void sendEmail() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("sendEmail");
}
public synchronized void callPhone(){
System.out.println("callPhone");
}
}
小结:
synchronized(Demo.class){
}
synchronized(this){
}
1、new this 调用的是这个对象,是一个具体的对象! 2、static class 唯一的一个模板!
在我们编写多线程程序得时候,只需要搞明白这个到底锁的是什么就不会出错了!
|