1、锁java
线程安全安全
``` 多线程网站统计访问人数 使用锁,维护计数器的串行访问与安全性 多线程访问ArrayList ``` ``` public static List<Integer> numberList =new ArrayList<Integer>(); public static class AddToList implements Runnable{ int startnum=0; public AddToList(int startnumber){ startnum=startnumber; } @Override public void run() { int count=0; while(count<1000000){ numberList.add(startnum); startnum+=2; count++; } } } public static void main(String[] args) throws InterruptedException { Thread t1=new Thread(new AddToList(0)); Thread t2=new Thread(new AddToList(1)); t1.start(); t2.start(); while(t1.isAlive() || t2.isAlive()){ Thread.sleep(1); } System.out.println(numberList.size()); } ``` ``` Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 24552 at java.util.ArrayList.add(ArrayList.java:463) at LockRunnable$AddToList.run(JvmLockCompare.java:101) at java.lang.Thread.run(Thread.java:748) 1010143 ```
对象头Mark多线程
Mark Word,对象头的标记,32位 描述对象的hash、锁信息,垃圾回收标记,年龄 指向锁记录的指针 指向monitor的指针 GC标记 偏向锁线程ID
偏向锁并发
大部分状况是没有竞争的,因此能够经过偏向来提升性能 所谓的偏向,就是偏爱,即锁会偏向于当前已经占有锁的线程 将对象头Mark的标记设置为偏向,并将线程ID写入对象头Mark 只要没有竞争,得到偏向锁的线程,在未来进入同步块,不须要作同步 当其余线程请求相同的锁时,偏向模式结束 -XX:+UseBiasedLocking 默认启用 在竞争激烈的场合,偏向锁会增长系统负担
本例中,使用偏向锁,能够得到5%以上的性能提高app
-XX:+UseBiasedLocking -XX:BiasedLockingStartupDelay=0ide
-XX:-UseBiasedLocking性能
public static List<Integer> numberList =new Vector<Integer>(); public static void main(String[] args) throws InterruptedException { long begin=System.currentTimeMillis(); int count=0; int startnum=0; while(count<10000000){ numberList.add(startnum); startnum+=2; count++; } long end=System.currentTimeMillis(); System.out.println(end-begin); }
轻量级锁优化
若是轻量级锁失败,表示存在竞争,升级为重量级锁(常规锁) 在没有锁竞争的前提下,减小传统锁使用OS互斥量产生的性能损耗 在竞争激烈时,轻量级锁会多作不少额外操做,致使性能降低
自旋锁网站
当竞争存在时,若是线程能够很快得到锁,那么能够不在OS层挂起线程,让线程作几个空操做(自旋) JDK1.6中-XX:+UseSpinning开启 JDK1.7中,去掉此参数,改成内置实现 若是同步块很长,自旋失败,会下降系统性能 若是同步块很短,自旋成功,节省线程挂起切换时间,提高系统性能
偏向锁,轻量级锁,自旋锁总结this
不是Java语言层面的锁优化方法 内置于JVM中的获取锁的优化方法和获取锁的步骤 偏向锁可用会先尝试偏向锁 轻量级锁可用会先尝试轻量级锁 以上都失败,尝试自旋锁 再失败,尝试普通锁,使用OS互斥量在操做系统层挂起
减小锁持有时间
减少锁粒度
ConcurrentHashMap 若干个Segment :Segment<K,V>[] segments Segment中维护HashEntry<K,V> put操做时 先定位到Segment,锁定一个Segment,执行put 在减少锁粒度后, ConcurrentHashMap容许若干个线程同时进入
锁分离
锁粗化
一般状况下,为了保证多线程间的有效并发,会要求每一个线程持有锁的时间尽可能短,即在使用完公共资源后,应该当即释放锁。只有这样,等待在这个锁上的其余线程才能尽早的得到资源执行任务。可是,凡事都有一个度,若是对同一个锁不停的进行请求、同步和释放,其自己也会消耗系统宝贵的资源,反而不利于性能的优化
示例1
示例2
锁消除
在即时编译器时,若是发现不可能被共享的对象,则能够消除这些对象的锁操做
public static void main(String args[]) throws InterruptedException { long start = System.currentTimeMillis(); for (int i = 0; i < CIRCLE; i++) { craeteStringBuffer("JVM", "Diagnosis"); } long bufferCost = System.currentTimeMillis() - start; System.out.println("craeteStringBuffer: " + bufferCost + " ms"); } public static String craeteStringBuffer(String s1, String s2) { StringBuffer sb = new StringBuffer(); //同步操做 sb.append(s1); sb.append(s2); return sb.toString(); }
CIRCLE=20000000
-server -XX:+DoEscapeAnalysis -XX:+EliminateLocks
craeteStringBuffer: 1057 ms
-server -XX:+DoEscapeAnalysis -XX:-EliminateLocks
craeteStringBuffer: 1857 ms
无锁
锁是悲观的操做 无锁是乐观的操做 无锁的一种实现方式 CAS(Compare And Swap) 非阻塞的同步 CAS(V,E,N) 在应用层面判断多线程的干扰,若是有干扰,则通知线程重试
示例
锁使用错误实例
错误:
/** * description: * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/10/6 12:53 AM */ public class IntegerLock { static Integer i = 0; public static class AddThread extends Thread { public void run() { for (int k = 0; k < 100000; k++) { synchronized (i) { i++; } } } } public static void main(String[] args) throws InterruptedException { AddThread t1 = new AddThread(); AddThread t2 = new AddThread(); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(i); } }
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * description: * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/10/6 12:53 AM */ public class IntegerLock { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static class ParseDate implements Runnable { int i = 0; public ParseDate(int i) { this.i = i; } public void run() { try { Date t = sdf.parse("2015-03-29 19:29:" + i % 60); System.out.println(i + ":" + t); } catch (ParseException e) { e.printStackTrace(); } } public static void main(String[] args) { ExecutorService es = Executors.newFixedThreadPool(10); for (int i = 0; i < 1000; i++) { es.execute(new ParseDate(i)); // SimpleDateFormat被多线程访问 } } } }
若是使用共享实例,起不到效果
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * description: * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/10/6 12:53 AM */ public class IntegerLock { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); static ThreadLocal<SimpleDateFormat> tl=new ThreadLocal<SimpleDateFormat>(); public static class ParseDate implements Runnable { int i = 0; public ParseDate(int i) { this.i = i; } public void run() { try { if(tl.get()==null){ tl.set(sdf); } Date t=tl.get().parse("2015-03-29 19:29:"+i%60); System.out.println(i + ":" + t); } catch (ParseException e) { e.printStackTrace(); } } public static void main(String[] args) { ExecutorService es = Executors.newFixedThreadPool(10); for (int i = 0; i < 1000; i++) { es.execute(new ParseDate(i)); // SimpleDateFormat被多线程访问 } } } }
正确:
/** * description: * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/10/6 12:53 AM */ public class IntegerLock { static Integer i = 0; static Object f = new Object(); public static class AddThread extends Thread { public void run() { for (int k = 0; k < 100000; k++) { synchronized (f) { i++; } } } } public static void main(String[] args) throws InterruptedException { AddThread t1 = new AddThread(); AddThread t2 = new AddThread(); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(i); } }
为每个线程分配一个实例
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * description: * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/10/6 12:53 AM */ public class IntegerLock { static ThreadLocal<SimpleDateFormat> tl=new ThreadLocal<SimpleDateFormat>(); public static class ParseDate implements Runnable { int i = 0; public ParseDate(int i) { this.i = i; } public void run() { try { if(tl.get()==null){ tl.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); } Date t=tl.get().parse("2015-03-29 19:29:"+i%60); System.out.println(i + ":" + t); } catch (ParseException e) { e.printStackTrace(); } } public static void main(String[] args) { ExecutorService es = Executors.newFixedThreadPool(10); for (int i = 0; i < 1000; i++) { es.execute(new ParseDate(i)); // SimpleDateFormat被多线程访问 } } } }