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");
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章