6. 8锁现象
package test01;
import java.util.concurrent.TimeUnit;
/*
*8锁 ,就是关于锁的8个问题
* 1.先打印 发短信还是 打电话 //1.发短信 2.打电话 锁的存在
* 2.发短信延迟 4s 先发短信还是 打电话 //等4s后 1.发短信 2.打电话
*/
public class Test1 {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendSms();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
},"B").start();
}
}
class Phone{
public synchronized void sendSms(){
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
}
package test01;
import java.util.concurrent.TimeUnit;
/*
* 3. 加了个普通方法 先hello 还是先发短信 // 1.hello 2.发短信
* 4.两个对象 先打电话 还是 发短信 //1. 打电话 2.发短信
*/
public class Test2 {
public static void main(String[] args) {
Phone2 phone2 = new Phone2();
Phone2 phone1 = new Phone2();
new Thread(()->{
phone1.sendSms();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
class Phone2{
public synchronized void sendSms(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
public void hello(){
System.out.println("hello");
}
}
package test01;
import java.util.concurrent.TimeUnit;
/*
* 5.静态方法 先 发短信还是打电话 //1.发短信 2.打电话
* 6.两个对象 先打印 发短信还是打电话 //1. 发短信 2.打电话
*/
public class Test3 {
public static void main(String[] args) {
Phone3 phone1 = new Phone3();
Phone3 phone2 = new Phone3();
new Thread(()->{
phone1.sendSms();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
//phone 唯一的一个Class对象
class Phone3{
//synchronized 锁的对象是方法的调用者
//static 静态方法
//类一加载就有了 ! 锁的是Class
public static synchronized void sendSms(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public static synchronized void call(){
System.out.println("打电话");
}
}
package test01;
import java.util.concurrent.TimeUnit;
/*
* 7.一个静态同步方法 一个普通同步方法 一个对象 先发短信 打电话 ? //1.打电话 2.发短信
* 8.一个静态同步方法 一个普通同步方法 两个对象 先发短信 打电话 ? //1.打电话 2.发短信
*/
public class Test4 {
public static void main(String[] args) {
Phone4 phone1 = new Phone4();
Phone4 phone2 = new Phone4();
new Thread(()->{
phone1.sendSms();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
//phone 唯一的一个Class对象
class Phone4{
//静态同步方法 锁的是Class类模板
public static synchronized void sendSms(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
//普通同步方法 锁的是调用者
public synchronized void call(){
System.out.println("打电话");
}
}
总结
new this 具体的一个手机
static class 唯一的一个模板
Java并发编程:同步方法与锁的概念解析
本文通过四个示例代码探讨了Java中锁的使用,包括普通同步方法、静态同步方法以及它们在不同场景下的行为。讨论了线程间的执行顺序,展示了锁在多线程环境下如何控制并发。内容涵盖了同步方法的锁对象、静态方法的锁机制以及不同对象实例的同步效果。
2791

被折叠的 条评论
为什么被折叠?



