8锁现象
-
-
- 1.synchronized锁的对象是方法的调用者,两个方法用的是同一个锁,谁先拿到谁先执行
- 2.这先后是锁的问题,那个线程先拿到
- 3.当增加了一个普通方法,不受锁的影响,先执行
- 4.多个对象跑不同线程,他们的线程不存在抢夺资源,同时进行(2把锁)
- 5.增加个静态同步方法 static 静态方法(锁的是Class,全局唯一)
- 6.两个对象! 两个静态同步方法,两个对象的Class模板只有一个,static锁的是Class
- 7. 一个静态同步方法(锁的是Class模板),一个普通同步方法(锁的是Phone对象),一个对象。所以同步执行
- 8.一个静态同步方法(锁的是Class模板),一个普通同步方法(锁的是Phone对象),二个对象。
-
- 总结一下
1.synchronized锁的对象是方法的调用者,两个方法用的是同一个锁,谁先拿到谁先执行
2.这先后是锁的问题,那个线程先拿到
public class Test1 {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendSms();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
}).start();
}
}
class Phone{
public synchronized void sendSms() {
System.out.println("sendSms");
}
public synchronized void call() {
System.out.println("call");
}
}
执行结果为
sendSms
call
public class Test1 {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendSms();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
}).start();
}
}
class Phone{
public synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendSms");
}
public synchronized void call() {
System.out.println("call");
}
}
执行结果为
sendSms
call
3.当增加了一个普通方法,不受锁的影响,先执行
public class Test2 {
public static void main(String[] args) {
Phone2 phone = new Phone2();
new Thread(()->{
phone.sendSms();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.hello();
}).start();
}
}
class Phone2{
public synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendSms");
}
public synchronized void call() {
System.out.println("call");
}
//这里没有锁 不是同步方法,不受锁的影响
public void hello() {
System.out.println("hello");
}
}
4.多个对象跑不同线程,他们的线程不存在抢夺资源,同时进行(2把锁)
public class Test2 {
public static void main(String[] args) {
Phone2 phone = new Phone2();
Phone2 phone2 = new Phone2();
new Thread(()->{
phone.sendSms();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
}).start();
}
}
class Phone2{
public synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendSms");
}
public synchronized void call() {
System.out.println("call");
}
//这里没有锁 不是同步方法,不受锁的影响
public void hello() {
System.out.println("hello");
}
}
5.增加个静态同步方法 static 静态方法(锁的是Class,全局唯一)
public class Test3 {
public static void main(String[] args) {
Phone3 phone = new Phone3();
new Thread(()->{
phone.sendSms();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
}).start();
}
}
class Phone3{
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendSms");
}
public static synchronized void call() {
System.out.println("call");
}
}
6.两个对象! 两个静态同步方法,两个对象的Class模板只有一个,static锁的是Class
public class Test3 {
public static void main(String[] args) {
Phone3 phone = new Phone3();
Phone3 phone2 = new Phone3();
new Thread(()->{
phone.sendSms();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
}).start();
}
}
class Phone3{
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendSms");
}
public static synchronized void call() {
System.out.println("call");
}
}
7. 一个静态同步方法(锁的是Class模板),一个普通同步方法(锁的是Phone对象),一个对象。所以同步执行
package JUC;
import java.util.concurrent.TimeUnit;
/**
* Copyright © 2021 eSunny Info. Tech Ltd. All rights reserved.
*
* 功能描述:
* @Package: JUC
* @author: 79283
* @date: 2021年1月21日 下午1:58:47
*/
public class Test4 {
public static void main(String[] args) {
Phone4 phone = new Phone4();
new Thread(()->{
phone.sendSms();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
}).start();
}
}
class Phone4{
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendSms");
}
public synchronized void call() {
System.out.println("call");
}
}
8.一个静态同步方法(锁的是Class模板),一个普通同步方法(锁的是Phone对象),二个对象。
package JUC;
import java.util.concurrent.TimeUnit;
/**
* Copyright © 2021 eSunny Info. Tech Ltd. All rights reserved.
*
* 功能描述:
* @Package: JUC
* @author: 79283
* @date: 2021年1月21日 下午1:58:47
*/
public class Test4 {
public static void main(String[] args) {
Phone4 phone = new Phone4();
Phone4 phone2 = new Phone4();
new Thread(()->{
phone.sendSms();
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
}).start();
}
}
class Phone4{
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sendSms");
}
public synchronized void call() {
System.out.println("call");
}
}
总结一下
new (this具体的一个对象)
static (Class唯一的类模板)