java基礎知識篇

1.java序列化究竟是什麼html

    序列化是一種用來處理對象流的機制 ,所謂對象流就是將對象的內容進行流化。能夠對流化後的對象進行讀寫操做,也可將流化後的對象傳輸於網絡之間。序列化是爲了解決在對對象流進行讀寫操做時所引起的問題。java

    ps:使用場景tomcat

         分佈式數據傳輸 , 網絡傳輸安全

         根本應用場景是:
           1.當對象須要被網絡傳輸時
           2.對象狀態須要被持久化時網絡

         典型應用 :session

         tomcat關閉之後會把session對象序列化到SESSIONS.ser文件中,等下次啓動的時候就把這些session再加載到內存裏面來。多線程

2.線程安全、線程不安全分佈式

    概念:ui

    線程安全就是多線程訪問的時候,採用了加鎖機制,當前線程訪問的時候其餘線程不能訪問。this

    線程不安全就是不提供數據訪問保護,可能出現髒數據。

    若是你的程序是多線程執行,而後執行結果和預期的一致,那麼就是線程安全的。

    線程安全問題都是由全局變量及靜態變量引發的。

    若每一個線程對全局變量,靜態變量只有讀,沒有寫操做,那麼也是線程安全的。若多線程同時執行寫操做,那麼就須要考慮線程同步。

3.java對象鎖應用

    鎖,java中只要加synchronized關鍵字便可,能夠加到方法上,也能夠加到代碼塊中

public synchronized String get(){
     return null;
}  

public String get(){
     synchronized{
        //代碼;
     }
     return null;
}

    

/**
 * 當前是多個線程用一個SyncV1對象,因此,synchronized執行過程當中同一時間只能有一個拿到鎖
 * 所以,執行結果爲 
 * 	test開始..
	test結束..
	test開始..
	test結束..
	test開始..
	test結束..
 * @author mrg
 *
 */
public class SyncV1 {
	public synchronized void test() {  
        System.out.println("test開始..");  
        try {  
            Thread.sleep(1000);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        System.out.println("test結束..");  
    }  
    public static void main(String[] args) {  
    	SyncV1 sync = new SyncV1();  
        for (int i = 0; i < 3; i++) { 
        	
            Thread thread = new MyThread(sync);  
            thread.start();  
        }  
    }  
}
class MyThread extends Thread {  
	SyncV1 sync ;
    public MyThread(SyncV1 sync) {
		super();
		this.sync = sync;
	}
	public void run() {  
        sync.test();  
    }  
}

         

/**
 * 這個是每執行一次都會建立一個SyncV2 , 而synchronized只是控制同一個對象同一時間只有一個實例能執行,
 * 因此結果是同時執行的。  
 * test2()方法,鎖住的也只是一個對象,因此test()方法和test2()方法結果一致
 * 所以,執行結果爲 
 * test開始..
 * test開始..
 * test開始..
 * test結束..
 * test結束..
 * test結束..
 * @author mrg
 *
 */
public class SyncV2 {
	public synchronized void test() {  
        System.out.println("test開始..");  
        try {  
            Thread.sleep(1000);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        System.out.println("test結束..");  
    }  
	public void test2() {  
		synchronized (this) {
			System.out.println("test開始..");  
	        try {  
	            Thread.sleep(1000);  
	        } catch (InterruptedException e) {  
	            e.printStackTrace();  
	        }  
	        System.out.println("test結束..");  
		}
    }  
    public static void main(String[] args) {  
        for (int i = 0; i < 3; i++) { 
            Thread thread = new MyThreadV2();  
            thread.start();  
        }  
    }  
}
class MyThreadV2 extends Thread {  
	public void run() {  
		SyncV2 sync = new SyncV2();
        sync.test();  
    }  
}

  

/**
 * 解釋一下test(),test2()方法
 * 	這裏的synchronized鎖定的是一個全局的類,和static synchronized是一致的。不論有多少個對象,同一時間只有一個對象能執行方法裏的代碼
 * 所以結果爲
* 	test開始..
	test結束..
	test開始..
	test結束..
	test開始..
	test結束..
 * @author mrg
 *
 */
public class SyncV3 {
	public  void test() {  
		synchronized (SyncV3.class) {
			System.out.println("test開始..");  
			System.out.println("當前對象:"+this);
	        try {  
	            Thread.sleep(1000);  
	        } catch (InterruptedException e) {  
	            e.printStackTrace();  
	        }  
	        System.out.println("test結束..");  
		}
    }  
	public static synchronized void test2() {  
		System.out.println("test開始..");  
        try {  
            Thread.sleep(1000);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        System.out.println("test結束..");  
    }  
    public static void main(String[] args) {  
        for (int i = 0; i < 3; i++) { 
            Thread thread = new MyThreadV3();  
            thread.start();  
        }  
    }  
}
class MyThreadV3 extends Thread {  
	public void run() {  
		SyncV3 sync = new SyncV3();
        sync.test2();  
    }  
}

  引自:http://blog.csdn.net/xiao__gui/article/details/8188833

4.對於vector的一些解釋(是否安全)

    http://mt.sohu.com/it/d20170226/127307378_494942.shtml

    http://talentluke.iteye.com/blog/1496976

    http://blog.csdn.net/ghevinn/article/details/37764791

相關文章
相關標籤/搜索