多线程静态同步函数的锁是class 对象
2015-01-09 13:50
232 查看
/* 通过验证发现 同步函数如果被静态修饰后,不再以this 为锁 静态进内存时,内存中没有本类对象,但是一定有该类的字节码文件对象 类名.class 该对象的类型是class 静态的同步方法,使用的锁是该方法所在类的字节码文件对象。类名.class */ class Ticket implements Runnable { private static int tick = 100; boolean flag = true; public void run() { if(flag) { while(true) { // synchronized(this) synchronized(Ticket.class) { if(tick>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.println(Thread.currentThread().getName() +" ..code..."+ tick--); } } } } else while(true) show();//this.show(); } public static synchronized void show()//静态同步函数 该类对应的字节码文件对象为锁 { if(tick>0) { try{Thread.sleep(10);}catch(Exception e){} System.out.println(Thread.currentThread().getName() +" .....show..."+ tick--); } } } class StaticLockDemo { public static void main(String []args) { Ticket t = new Ticket(); Thread t1 = new Thread(t); Thread t2 = new Thread(t); t1.start();//开启第一个线程 但不一定马上执行 t.flag = false;//改变标志 try{Thread.sleep(40);}catch(Exception e){}//让主线程睡眠40毫秒 保证第一个线程先开始运行 且标志位改变 t2.start(); } }
相关文章推荐
- 多线程静态同步函数的锁是class 对象
- 面向对象-多线程(同步函数的锁是this与静态同步函数的锁是class)
- (49)静态同步函数的锁是Class对象,例子:懒汉式--在多线程中存在问题--同步解决
- JAVA 多线程静态同步函数的锁是class 对象
- JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制
- JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制
- 面向对象-多线程(同步函数的锁this与静态同步函数的锁class)
- (多线程-静态同步函数的锁是Class对象)
- JAVA 多线程静态同步函数的锁是class 对象
- java多线程-静态同步函数的锁是Class对象
- 多线程---静态同步函数的锁是class(转载)
- java-静态同步函数的锁是Class对象
- 多线程——静态同步函数的锁的相关问题
- 同步代码块,同步函数,静态同步函数的锁对象
- Java多线程知识点总结——进阶篇(四)之静态同步函数用的是哪一个锁问题
- 多线程三,同步函数、静态同步函数和单例设计模式中懒汉式即延迟加载模式的多线程问题(毕向东老师)
- Java笔记3 多线程<1>线程概述、多线程的创建、多线程的安全问题、静态同步函数的锁、死锁
- 验证多线程中同步函数锁是this对象
- java多线程之-----静态同步synchronized方法与synchronized(class) 代码块
- Java 多线程 静态同步函数