多线程—02—锁

来源:互联网 发布:淘宝卖家开通蚂蚁花呗 编辑:程序博客网 时间:2024/06/01 12:44

 

/*同步函数用的是哪一个锁呢?函数需要被对象调用,那么函数都有一个所属对象引用,就是this。所以同步函数使用的锁是this。通过该程序进行验证。使用两个线程来卖票一个线程在同步代码块中。一个线程在同步函数中。都在实习卖票动作。*/class Ticket implements Runnable{private int tick = 1000;Object obj = new Object();boolean flag = true;public void run(){if(flag){while(true){synchronized(this)//同步代码块{if(tick>0){try{Thread.sleep(15);}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"...code: "+tick--);}}}}elsewhile(true)show();}public synchronized void show()//同步函数,锁为this{if(tick>0){try{Thread.sleep(15);}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"...show: "+tick--);}}}class ThisLockDemo{public static void main(String[] args){Ticket t = new Ticket();Thread t1 = new Thread(t);//创建了一个线程Thread t2 = new Thread(t);//创建了一个线程t1.start();try{Thread.sleep(10);}catch(Exception e){}t.flag = false;t2.start();}}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*如果同步函数被静态修饰后,使用的锁是什么呢?通过验证,发现不再是this。因为静态方法中不可以定义this静态进内存时,内存中没有本类对象,但是一定有该类对应的字节码文件对象。类名.class 该对象的类型是Class静态的同步方法,使用的锁是该方法所在类的字节码文件对象。类名.class*/class Ticket implements Runnable{private static int tick = 1000;Object obj = new Object();boolean flag = true;public void run(){if(flag){while(true){synchronized(Ticket.class)//同步代码块{if(tick>0){try{Thread.sleep(15);}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"...code: "+tick--);}}}}elsewhile(true)show();}public static synchronized void show()//同步函数{if(tick>0){try{Thread.sleep(15);}catch(Exception e){}System.out.println(Thread.currentThread().getName()+"...show: "+tick--);}}}class StaticMethodDemo{public static void main(String[] args){Ticket t = new Ticket();Thread t1 = new Thread(t);//创建了一个线程Thread t2 = new Thread(t);//创建了一个线程t1.start();try{Thread.sleep(10);}catch(Exception e){}t.flag = false;t2.start();}}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


 

/*单例设计模式饿汉式:class Single{private static final Single s = new Single();private Single(){}public static Single getInstance(){retrun s;}}*/ //懒汉式:class Single{private static Single s = null;private Single(){}public static Single getInstance(){if(s==null){synchronized(Single.class)//静态方法锁是Class对象{if(s==null)s = new Single();return s;}}}}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


 

//死锁示例,要求掌握class Test implements Runnable{private boolean flag;Test(boolean flag){this.flag = flag;}public void run(){if(flag){while(true){synchronized(MyLock.locka){System.out.println("if locka");synchronized(MyLock.lockb){System.out.println("if loakb");}}}}else{while(true){synchronized(MyLock.lockb){System.out.println("else lockb");synchronized(MyLock.locka){System.out.println("else locka");}}}}}}class MyLock{static Object locka = new Object();static Object lockb = new Object();}class DeadLockTest{public static void main(String[] args){Thread t1 = new Thread(new Test(true));Thread t2 = new Thread(new Test(false));t1.start();t2.start();}}


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

个人总结: 

原创粉丝点击