最近看很多文章在谈ArrayList和Vector的线性安全问题,特别拿来与大家一起研究下:
1.如果不考虑多线程,讨论线性安全有意义吗?
答:没有
2.java.util.Vector的方法都被Synchronize修饰过,那么它就是线性安全的?
答:对,因为set的时候不会被get,get的是没改过之前的数据。Synchronize永远作为一个整体执行,要么没有执行,要么都被执行。
上例子:
import java.util.Date; /** * Created by lizhiqiang on 2016/12/23. */ public class testSynchronize { public static void main(String[] args){ final Test test = new Test(); new Thread(new Runnable() { @Override public void run() { while(true){ test.set(); } } }).start(); new Thread(new Runnable() { @Override public void run() { while(true){ System.out.println(test.get()); } } }).start(); } public static class Test{ private String value; public synchronized void set(){ value = new Date().toString(); try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } value = new Date().toString(); } public synchronized String get(){ return value; } } }执行结果:
Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016 Fri Dec 23 16:43:24 CST 2016