synchronized :java内置关键字,被保存在对象头中,而一个对象则是由对象头、实例数据、对其填充三部分组成。
很多时候大家伙都惯性地将synchronized称为一个重量级锁,理由是synchronized性能开销较重;这在JDK1.6之间这样说是没毛病的,但是在JDK1.6及以后,还这样认为那就欠妥了,因为在JDK1.6及以后JVM层面对它作了优化,可以由轻到重分为:偏向锁->轻量级锁->重量级锁;并且可自动进行锁粒度的切换升级。所以从性能开销的程度来讲,已经变得和Lock相差无几了!
一般来说synchronized有三种用法:
一、作用于静态方法
给当前类加锁,又可称之静态方法锁:可以修饰在静态方法名前: public static synchronized void inc() :
又可以修饰在静态方法里的代码块(如下): synchronized (Method.class){}
package com.wuqx.demo;public class Method { private static int count = 0; public static void inc(){ synchronized (Method.class){ try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } count ++; } } public static void main(String[] args) { for (int i = 0; i < 100; i++) { //不要new太多的线程,避免操作系统假死,出现找不到主类的错误 new Thread(()->Method.inc()).start(); } while(Thread.activeCount()>1){ //这里也可以使用Thread.sleep()方法进行阻塞,避免得到错误的结果 Thread.yield(); } System.out.println("result:"+Method.count); }}
总结:可以保证多个实例对象调用静态方法时保证其安全性
二、作用于实例方法
即对象锁,对同一个对象加锁,保证多个线程访问同一个对象时线程的安全,进入同步代码前要获得当前对象的锁,
(1)多个线程对同一对象进行访问,代码清单如下:
package t;public class Method implements Runnable{ private static int count = 0; public synchronized void run(){ for(int i=0; i<5; i++){ System.out.println(Thread.currentThread().getName()+":"+count++); } } public static void main(String[] args) { Method md = new Method(); Thread thread1 = new Thread(md,"thread1"); Thread thread2 = new Thread(md,"thread2"); thread1.start(); thread2.start(); int i = 0; while(Thread.activeCount()>1){ System.out.println("第"+i+++"次出让系统资源"); Thread.yield(); } System.out.println("result:"+Method.count); }}
运行结果:
(2)多个线程对不同对象访问,则会造成线程的安全问题;因为两个对象之间没有关系,是独立的,内存地址,保存在对象头里的锁也不相同,所以会造成线程安全问题。代码清单如下(两个线程执行两个对象):
package t;
public class Method implements Runnable{
private static int count = 0;
public synchronized void run(){
for(int i=0; i<5; i++){
System.out.println(Thread.currentThread().getName()+":"+count++);
}
}
public static void main(String[] args) {
Method md = new Method();
Method md1 = new Method();
Thread thread1 = new Thread(md,"thread1");
Thread thread2 = new Thread(md1,"thread2");
thread1.start();
thread2.start();
}
}
运行结果如下:
可见这种情况只能通过将run方法里的方法静态化,通过类锁去解决:代码清单如下:
package t;public class Method implements Runnable{ private static int count = 0; public void run(){ //这里加与不加synchronized 效果都一样,可以思考下为什么 inc(); } public static synchronized void inc(){ for(int i=0; i<5; i++){ System.out.println(Thread.currentThread().getName()+":"+count++); } } public static void main(String[] args) { Method md = new Method(); Method md1 = new Method(); Thread thread1 = new Thread(md,"thread1"); Thread thread2 = new Thread(md1,"thread2"); thread1.start(); thread2.start(); }}
运行结果如下:
三、修饰代码快
写法为synchronized(obj){},实际工程上都用写为synchronized(this){}, 这是对于多个线程访问同一对象,代码清单如下;如果多个线程访问不同对象,那么这样写还是不能保证线程的安全,因为将对象不同,锁也就不是同一把锁了,这样就同样需要类锁去实现了。
package t;
public class Method implements Runnable{
private static int count = 0;
public void run(){
inc();
}
public void inc(){
synchronized(this){
for(int i=0; i<5; i++){
System.out.println(Thread.currentThread().getName()+":"+count++);
}
}
}
public static void main(String[] args) {
Method md = new Method();
Thread thread1 = new Thread(md,"thread1");
Thread thread2 = new Thread(md,"thread2");
thread1.start();
thread2.start();
}
}
运行结果:
如果觉得对自己有帮助的话,不妨关注点赞转发支持下哦!
相关导读:
synchronized与Lock的区别