关于锁的对象
作者:互联网
锁的对象共有两种:类对象和Class模板对象
1.标准情况下,两个线程、一个对象,先打印发短信还是打电话?发短信
2.发短信方法设置延迟4秒后,两个线程、一个对象,先打印发短信还是打电话?发短信
Synchronized锁的对象是方法的调用者,故两个方法用的是同一把锁,谁先拿到谁执行
package com.itheima.lock8;
import java.util.concurrent.TimeUnit;
public class Test01 {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendMsg();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
},"B").start();
}
}
//资源类
class Phone{
public synchronized void sendMsg(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"发短信");
}
public synchronized void call(){
System.out.println(Thread.currentThread().getName()+"打电话");
}
}
3.两个线程、一个对象、一个同步方法、一个普通方法,先打印发短信还是hello?hello
因为发短信方法会延迟4秒,hello是普通方法,无锁,不是同步方法,不受锁的影响,不存在抢占资源情况。
package com.itheima.lock8;
import java.util.concurrent.TimeUnit;
public class Test02 {
public static void main(String[] args) {
Phone2 phone = new Phone2();
new Thread(()->{
phone.sendMsg();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.hello();
},"B").start();
}
}
//资源类
class Phone2{
public synchronized void sendMsg(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"发短信");
}
public synchronized void call(){
System.out.println(Thread.currentThread().getName()+"打电话");
}
//普通方法
public void hello(){
System.out.println(Thread.currentThread().getName()+"=>hello");
}
}
4.两个线程、两个对象、两个同步方法,先打印发短信还是打电话?打电话
两个对象会存在两把锁,锁不一样,故按照时间顺序来执行
package com.itheima.lock8;
import java.util.concurrent.TimeUnit;
public class Test03 {
public static void main(String[] args) {
Phone3 phone1 = new Phone3();
Phone3 phone2 = new Phone3();
new Thread(()->{
phone1.sendMsg();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
//资源类
class Phone3{
public synchronized void sendMsg(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"发短信");
}
public synchronized void call(){
System.out.println(Thread.currentThread().getName()+"打电话");
}
//普通方法
public void hello(){
System.out.println(Thread.currentThread().getName()+"=>hello");
}
}
5.两个线程、一个对象、两个静态同步方法,先打印发短信还是打电话?发短信
当同步方法为静态时,锁的是其Class模板,一个类只有一个Class对象
package com.itheima.lock8;
import java.util.concurrent.TimeUnit;
public class Test04 {
public static void main(String[] args) {
Phone4 phone4 = new Phone4();
new Thread(()->{
phone4.msg();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone4.call();
},"B").start();
}
}
class Phone4{
public static synchronized void msg(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public static synchronized void call(){
System.out.println("打电话");
}
}
6.两个线程、两个对象、两个静态同步方法,先打印发短信还是打电话?发短信
当同步方法为静态时,锁的是其Class模板,一个类只有唯一的一个Class对象
package com.itheima.lock8;
import java.util.concurrent.TimeUnit;
public class Test04 {
public static void main(String[] args) {
Phone4 phone1 = new Phone4();
Phone4 phone2 = new Phone4();
new Thread(()->{
phone1.msg();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
class Phone4{
public static synchronized void msg(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public static synchronized void call(){
System.out.println("打电话");
}
}
7.两个线程、一个对象、一个静态同步方法、一个普通同步方法,先打印发短信还是打电话?打电话
静态同步方法锁的是Class类对象,普通同步方法锁的是类对象,因此拥有两把锁,互不冲突
package com.itheima.lock8;
import java.util.concurrent.TimeUnit;
public class Test05 {
public static void main(String[] args) {
Phone5 phone = new Phone5();
new Thread(()->{
phone.msg();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone.call();
},"B").start();
}
}
class Phone5{
public static synchronized void msg(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
}
8.两个线程、两个对象、一个静态同步方法、一个普通同步方法,先打印发短信还是打电话?打电话
拥有两个锁,故先输出没有延时的
package com.itheima.lock8;
import java.util.concurrent.TimeUnit;
public class Test05 {
public static void main(String[] args) {
Phone5 phone1 = new Phone5();
Phone5 phone2 = new Phone5();
new Thread(()->{
phone1.msg();
},"A").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
phone2.call();
},"B").start();
}
}
class Phone5{
public static synchronized void msg(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
}
标签:Thread,对象,void,关于,TimeUnit,发短信,new,public 来源: https://blog.csdn.net/Puyefeng/article/details/122800522