Java基础 线程8锁

Java基础 线程8锁

package com.robot;

import java.util.concurrent.TimeUnit;


//我们来看第一个场景 这时候会先输出sendMes还是call呢?
//1:  答案是先sendMes 停顿2秒后 在输出call
public class Demo {
	public static void main(String[] args) {
		Phone phone=new Phone();
		new Thread(()->{
			phone.sendMes();
		}) .start();
		
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		new Thread(()->{
			phone.call();
		}) .start();
	}
}
class Phone{
    //synchronized锁的对象是方法的调用者 也就是phone对象
    //两个方法用的是同一个锁 谁先拿到谁先执行
	public synchronized void sendMes() {
		System.out.println("sendMes");
	}
	public synchronized void call() {
		System.out.println("call");
	}
	
}

package com.robot;

import java.util.concurrent.TimeUnit;
//sendMes里做一个sleep
//2:  先等待5秒 然后打印sendMes 然后打印call
//这里锁的是对象
//一个对象里有多个synchronized方法 某一时刻 只要一个线程去调用其中的
//一个synchronized方法 其他线程只能等待
//也就是说 该对象被锁定后 其他线程不能进入其他synchronized方法
public class Demo {
    public static void main(String[] args)  {
        Phone phone=new Phone();
        new Thread(()->{
                phone.sendMes();
        }).start();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
                    phone.call();
        }).start();
    }
}
class Phone{
    public synchronized void sendMes(){
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendMes");
    }
    public synchronized void call(){
        System.out.println("call");
    }
}

package com.robot;

import java.util.concurrent.TimeUnit;
//两个对象 
//3:   先等待1秒 call执行 等待6秒 sendMes执行
public class Demo {
    public static void main(String[] args)  {
        Phone phone1=new Phone();
        Phone phone2=new Phone();

        new Thread(()->{
                phone1.sendMes();
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
                    phone2.call();
        }).start();
    }
}
class Phone{
    public synchronized void sendMes(){
        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendMes");
    }
    public synchronized void call(){
        System.out.println("call");
    }
}

package com.robot;

import java.util.concurrent.TimeUnit;
//加一个普通方法
//4:   等待1秒 hello打印 等待6秒 sendMes打印
public class Demo {
    public static void main(String[] args)  {
        Phone phone=new Phone();

        new Thread(()->{
                phone.sendMes();
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
                    phone.hello();
        }).start();
    }
}
class Phone{
    public synchronized void sendMes(){
        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendMes");
    }
    public synchronized void call(){
        System.out.println("call");
    }
    public void hello(){
        System.out.println("hello ");
    }
}

package com.robot;

import java.util.concurrent.TimeUnit;
//增加两个静态方法
//5:    等待6秒 打印sendMes 然后打印call
//锁的是class文件
public class Demo {
    public static void main(String[] args)  {
        Phone phone=new Phone();

        new Thread(()->{
                phone.sendMes();
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
                    phone.call();
        }).start();
    }
}
//锁的是class文件
class Phone{
    public static  synchronized void sendMes(){
        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendMes");
    }
    public static synchronized void call(){
        System.out.println("call");
    }

}

package com.robot;

import java.util.concurrent.TimeUnit;
//增加两个静态方法 增加成为两个phone对象
//锁的是class文件
//6:   等待6秒 打印sendMes 然后紧跟着打印call

public class Demo {
    public static void main(String[] args)  {
        Phone phone1=new Phone();
        Phone phone2=new Phone();

        new Thread(()->{
                phone1.sendMes();
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
                    phone2.call();
        }).start();
    }
}
class Phone{
    public static  synchronized void sendMes(){
        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendMes");
    }
    public static synchronized void call(){
        System.out.println("call");
    }

}

package com.robot;

import java.util.concurrent.TimeUnit;
//一个静态同步方法 一个普通同步方法 一个phone对象
//7:   等待2秒 打印call 在等待6秒 打印sendMes
//一个锁的是class文件 一个锁的是phone对象
//锁的对象不冲突
public class Demo {
    public static void main(String[] args)  {
        Phone phone=new Phone();

        new Thread(()->{
                phone.sendMes();
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
                    phone.call();
        }).start();
    }
}
class Phone{
    public static  synchronized void sendMes(){
        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendMes");
    }
    public synchronized void call(){
        System.out.println("call");
    }

}

package com.robot;

import java.util.concurrent.TimeUnit;
//两个对象  一个静态同步方法 一个同步方法
//8:  等待2秒 打印call 等待6秒 打印sendMes
public class Demo {
    public static void main(String[] args)  {
        Phone phone1=new Phone();
        Phone phone2=new Phone();
        new Thread(()->{
                phone1.sendMes();
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(()->{
                    phone2.call();
        }).start();
    }
}
class Phone{
    public static  synchronized void sendMes(){
        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendMes");
    }
    public synchronized void call(){
        System.out.println("call");
    }

}

发布了39 篇原创文章 · 获赞 19 · 访问量 1484

猜你喜欢

转载自blog.csdn.net/weixin_44222272/article/details/105052490