8锁问题
1.synchronized 锁的对象是方法调用者,两个方法用的是同一个锁,谁先拿到谁执行!结果:先发短信后打电话
package com.czp.lock;
import java.util.concurrent.TimeUnit;
/**
* 8锁就是关于锁的八个问题
* 1\. 标准情况下,两个线程先打印 发短信 还是打电话
* AsendMessage
* Bcall
* synchronized 放在方法上 锁的对象是方法调用者
*
*/
public class Test1 {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendMessage();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
},"B").start();
}
}
class Phone{
public synchronized void sendMessage(){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "sendMessage");
}
public synchronized void call(){
System.out.println(Thread.currentThread().getName() + "call");
}
}
package com.czp.lock;
import java.util.concurrent.TimeUnit;
/**
* 增加了一个普通方法
* 3\. hello后发短信
*
* 4.打电话/后发短信
*/
public class Test2 {
public static void main(String[] args) {
Phone1 phone = new Phone1();
Phone1 phone2 = new Phone1();
new Thread(() -> {
phone.sendMessage();
}, "A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
// phone.hello();
phone2.call();
}, "B").start();
}
}
class Phone1 {
public synchronized void sendMessage() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "sendMessage");
}
public synchronized void call() {
System.out.println(Thread.currentThread().getName() + "call");
}
//这里没有锁,不受锁的影响
public void hello(){
System.out.println("hello");
}
}
package com.czp.lock;
import java.util.concurrent.TimeUnit;
/**
* 5.增加两个静态的同步方法
* synchronized 锁的是方法调用者
* static 静态方法类一加载就有了 锁的是CLass
*先发短信后打电话
* 6\. 两个对象,两个静态同步方法,先发短信还是先打电话
*发短信后打电话
*/
public class Test3 {
public static void main(String[] args) {
Phone2 phone2 = new Phone2();
Phone2 phone3 = new Phone2();
new Thread(() -> {
phone2.sendMessage();
}, "A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
phone3.call();
}, "B").start();
}
}
class Phone2 {
public static synchronized void sendMessage() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "sendMessage");
}
public static synchronized void call() {
System.out.println(Thread.currentThread().getName() + "call");
}
}
package com.czp.lock;
import java.util.concurrent.TimeUnit;
/**
* 7.一个静态同步方法,一个普通同步方法 ,一个对象,先输出哪一个
*先call后发短信
* 8\. 一个静态同步方法,一个普通同步方法, 两个对象,先打印哪一个
*/先call后发信息
public class Test4 {
public static void main(String[] args) {
Phone3 phone3 = new Phone3();
Phone3 phone4 = new Phone3();
new Thread(() -> {
phone3.sendMessage();
}, "A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
phone4.call();
}, "B").start();
}
}
class Phone3 {
public static synchronized void sendMessage() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "sendMessage");
}
//普通同步方法
public synchronized void call() {
System.out.println(Thread.currentThread().getName() + "call");
}
}
小结
new this 具体的一个手机对象
static Class 唯一的模板