线程:synchronized
对象锁
关键字synchronized取得的锁都是对象锁,而不是把一段代码或方法(函数)当做锁。
锁重入
synchronized拥有锁重入功能,也就是在使用synchronized时, 当一个线程得到一个对象锁后,再次请求此对象锁时是可以再次得到该对象的锁的。
可重入锁: 自己可以再次获取自己的内部锁。
public class Service {synchronized public void service1(){System.out.println("service1...");service2();}synchronized public void service2(){System.out.println("service2...");service3();}synchronized public void service3(){System.out.println("service3...");} }
当存在父子类继承关系时,子类是完全可以通过"可重入锁"调用父类的同步方法的。
public class Main {public int i = 10;synchronized public void operateMainMethod(){try{i--;System.out.println("main print i="+i);Thread.sleep(1000);}catch(InterruptedException e){e.printStackTrace();}} }public class Sub extends Main {synchronized public void operateISubMethod(){try{while(i>0){i--;System.out.println("sub print i="+i);Thread.sleep(100);this.operateMainMethod();}}catch(InterruptedException e){e.printStackTrace();}} }出现异常,锁自动释放
当一个线程执行的代码出现异常时,其所持有的锁会自动释放。
同步不具有继承性
同步是无法继承的
public class Main {synchronized public void serviceMethod(){try{System.out.println("int main 下一步 sleep begin threadName="+Thread.currentThread().getName() +" time="+System.currentTimeMillis());Thread.sleep(5000);System.out.println("int main 下一步 sleep end threadName="+Thread.currentThread().getName() +" time="+System.currentTimeMillis());}catch(InterruptedException e){e.printStackTrace();}} }public class Sub extends Main{@Overridepublic void serviceMethod(){try{System.out.println("int sub 下一步 sleep begin threadName="+Thread.currentThread().getName() +" time="+System.currentTimeMillis());Thread.sleep(5000);System.out.println("int sub 下一步 sleep end threadName="+Thread.currentThread().getName() +" time="+System.currentTimeMillis());}catch(InterruptedException e){e.printStackTrace();}} }synchronized同步语句块
1. synchronized方法是对当前对象进行加锁, 而synchronized代码块是对某一个对象进行加锁。
2. synchronized代码块间也具有同步性, 当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对同一个object中所有其他synchronized(this)同步代码块的访问将被阻塞,这说明synchronized使用的"对象监视器"是一个。
3. synchronized(this)代码块是锁定当前对象的。
4. 多对象时候,锁住代码
只要锁住同一个对象就行了。例如:synchronized后的括号中锁同一个固定对象,这样就行了。 这样是没问题,但是,比较多的做法是让synchronized锁这个类对应的Class对象。
public class Sync2 {public void test() {synchronized (Sync2.class) {System.out.println("test start");try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("test end");}} }public class MyThread3 extends Thread{public void run() {Sync2 sync = new Sync2();sync.test();}public static void main(String[] args) {for (int i = 0; i < 3; ++i) {Thread thread = new MyThread3();thread.start();}} }执行结果:
test start
test end
test start
test end
test start
test end
静态同步synchronized方法与synchronized(class)代码块
关键字synchronized还可以应用在static静态方法上,这是对当前的*.java文件对应的Class类进行持锁。
synchronized加到static静态方法上是给Class类上锁, 而synchronized关键字加到非static静态方法上是给对象上锁。
Class锁可以对类的所有对象实例起作用。
数据类型String的常量池特性
jvm中具有String常量池缓存的功能,因此大多数情况下,同步synchronized代码块都不使用String作为锁对象,而改用其他。
方法帧: 第一块:局部变量表 第二块:操作数栈 第三块:指向常量池的指针。
总结
以上是生活随笔为你收集整理的线程:synchronized的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 数据结构:堆排序一(heap sort)
- 下一篇: 线程:volatile关键字