- 关于锁的8个问题
范例1:
package pers.vincent.matrix.subject.lock8;
import java.util.concurrent.TimeUnit;
public class Test1 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(()->{
try {
phone.sendSms();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone.phone();
}).start();
}
}
/**
* 1. synchronized 锁的是方法的调用者
* 此时 两个方法的调用者都是phone, 谁先获得锁谁先执行
*/
class Phone{
public synchronized void sendSms() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("发短信");
}
public synchronized void phone(){
System.out.println("打电话");
}
}
范例2:
package pers.vincent.matrix.subject.lock8;
import java.util.concurrent.TimeUnit;
public class Test2 {
public static void main(String[] args) throws InterruptedException {
Phone2 phone1 = new Phone2();
Phone2 phone2 = new Phone2();
new Thread(()->{
try {
phone1.sendSms();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone2.phone();
}).start();
}
}
/**
* 1. Synchronized 锁的是方法的调用者
* 2. 此时两个方法由不同的调用者,归为不同的锁。打电话 没有时间等待
*/
class Phone2{
public synchronized void sendSms() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("发短信");
}
public synchronized void phone(){
System.out.println("打电话");
}
}
范例3:
package pers.vincent.matrix.subject.lock8;
import java.util.concurrent.TimeUnit;
public class Test3 {
public static void main(String[] args) throws InterruptedException {
Phone3 phone1 = new Phone3();
new Thread(()->{
try {
phone1.sendSms();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone1.call();
}).start();
}
}
/**
* 1. Synchronized 锁的是方法的调用者
* call() 没有 synchronized 修饰 没有锁的概念,先执行
*/
class Phone3{
public synchronized void sendSms() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("发短信");
}
public synchronized void phone(){
System.out.println("打电话");
}
public void call(){
System.out.println("我要打电话给");
}
}
范例4:
package pers.vincent.matrix.subject.lock8;
import java.util.concurrent.TimeUnit;
public class Test4 {
public static void main(String[] args) throws InterruptedException {
Phone4 phone1 = new Phone4();
new Thread(()->{
try {
phone1.sendSms();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone1.phone();
}).start();
}
}
/**
* 1. Synchronized 锁的是方法的调用者
* static 修饰符,说明方法为静态方法 类加载的时候就初始化了。它锁的是 类的 Class
* 两个方法的调用者一致,且都有 static 修饰符;则 谁先拿到谁执行: 发短信
*/
class Phone4{
public static synchronized void sendSms() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("发短信");
}
public static synchronized void phone(){
System.out.println("打电话");
}
}
范例5:
package pers.vincent.matrix.subject.lock8;
import java.util.concurrent.TimeUnit;
public class Test5 {
public static void main(String[] args) throws InterruptedException {
Phone5 phone1 = new Phone5();
new Thread(()->{
try {
phone1.sendSms();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone1.phone();
}).start();
}
}
/**
* 1. Synchronized 锁的是方法的调用者
* static 修饰符,说明方法为静态方法 类加载的时候就初始化了。它锁的是 类的 Class
*
* 此时 sendSms 锁的是 Class, 而phone 锁的方法的调用者 phone1;
*
* 他们属于不同的锁,没有先后和等待:打电话
*/
class Phone5{
public static synchronized void sendSms() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("发短信");
}
public synchronized void phone(){
System.out.println("打电话");
}
}
范例6:
package pers.vincent.matrix.subject.lock8;
import java.util.concurrent.TimeUnit;
public class Test6 {
public static void main(String[] args) throws InterruptedException {
Phone6 phone1 = new Phone6();
Phone6 phone2 = new Phone6();
new Thread(()->{
try {
phone1.sendSms();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone2.phone();
}).start();
}
}
/**
* 1. Synchronized 锁的是方法的调用者
* static 修饰符,说明方法为静态方法 类加载的时候就初始化了。它锁的是 类的 Class
*
* 此时两个方法的锁的是同一个 Class : 发短信
*/
class Phone6{
public static synchronized void sendSms() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("发短信");
}
public static synchronized void phone(){
System.out.println("打电话");
}
}
范例7:
package pers.vincent.matrix.subject.lock8;
import java.util.concurrent.TimeUnit;
public class Test7 {
public static void main(String[] args) throws InterruptedException {
Phone7 phone1 = new Phone7();
new Thread(()->{
try {
phone1.sendSms();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone1.phone();
}).start();
}
}
/**
* 1. Synchronized 锁的是方法的调用者
* static 修饰符,说明方法为静态方法 类加载的时候就初始化了。它锁的是 类的 Class
*
* 此时sendSms 锁的是Class phone 锁的方法的调用者
* 打电话
*/
class Phone7{
public static synchronized void sendSms() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("发短信");
}
public synchronized void phone(){
System.out.println("打电话");
}
}
范例8:
package pers.vincent.matrix.subject.lock8;
import java.util.concurrent.TimeUnit;
public class Test8 {
public static void main(String[] args) throws InterruptedException {
Phone8 phone1 = new Phone8();
Phone8 phone2 = new Phone8();
new Thread(()->{
try {
phone1.sendSms();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
phone2.phone();
}).start();
}
}
/**
* 1. Synchronized 锁的是方法的调用者
*
* 还是两把不同的锁,
*
* 打电话
*/
class Phone8{
public static synchronized void sendSms() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
System.out.println("发短信");
}
public synchronized void phone(){
System.out.println("打电话");
}
}