synchronized用於多線程設計,有了synchronized關鍵字,多線程程序的運行結果將變得能夠控制。synchronized關鍵字用於保護共享數據。多線程
synchronized實現同步的機制:synchronized依靠"鎖"機制進行多線程同步,"鎖"有2種,一種是對象鎖,一種是類鎖。ide
初始化一個對象時,自動有一個對象鎖。synchronized {普通方法}依靠對象鎖工做,多線程訪問synchronized方法,一旦某個進程搶得鎖以後,其餘的進程只有排隊對待。函數
public class TestSynchronized { public synchronized void method1() throws InterruptedException { System.out.println("method1 begin at:" + System.currentTimeMillis()); Thread.sleep(6000); System.out.println("method1 end at:" + System.currentTimeMillis()); } public synchronized void method2() throws InterruptedException { while(true) { System.out.println("method2 running"); Thread.sleep(200); } } static TestSychronized instance = new TestSychronized(); public static void main(String[] args) { Thread thread1 = new Thread(new Runnable() { @Override public void run() { try { instance.method1(); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=1; i<4; i++) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread1 still alive"); } } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { instance.method2(); } catch (InterruptedException e) { e.printStackTrace(); } } }); thread1.start(); thread2.start(); } }
運行結果:thread2一直等到thread1中的method1執行完了以後才執行method2,說明method1和method2互斥this
method1 begin at:1381584063557 method1 end at:1381584069557 method2 running method2 running Thread1 still alive method2 running Thread1 still alive method2 running Thread1 still alive method2 running method2 running method2 running method2 running method2 running method2 running method2 running method2 running
package com.free4lab.lol; public class TestSychronized { public synchronized static void method1() throws InterruptedException { System.out.println("method1 begin at:" + System.currentTimeMillis()); Thread.sleep(6000); System.out.println("method1 end at:" + System.currentTimeMillis()); } public synchronized static void method2() throws InterruptedException { while(true) { System.out.println("method2 running"); Thread.sleep(200); } } static TestSychronized instance1 = new TestSychronized(); static TestSychronized instance2 = new TestSychronized(); public static void main(String[] args) { Thread thread1 = new Thread(new Runnable() { @Override public void run() { try { instance1.method1(); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=1; i<4; i++) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread1 still alive"); } } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { instance2.method2(); } catch (InterruptedException e) { e.printStackTrace(); } } }); thread1.start(); thread2.start(); } }
輸出效果也是method1和method2互斥spa
package com.free4lab.lol; public class TestSychronized { static TestSychronized instance = new TestSychronized(); public static void main(String[] args) { Thread thread1 = new Thread(new Runnable() { @Override public synchronized void run() { for(int i=1; i<4; i++) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread1 still alive, " + i); } } }); new Thread(thread1).start(); new Thread(thread1).start(); } }
若是加了synchronized當前線程取完全部數據後,纔會釋放鎖,輸出結果是有序的:線程
Thread1 still alive, 1 Thread1 still alive, 2 Thread1 still alive, 3 Thread1 still alive, 1 Thread1 still alive, 2 Thread1 still alive, 3