標籤:sync runnable view void 代碼塊 etc contract 用法 current
synchronized是Java中的關鍵字,是一種同步鎖。它修飾的對象有以下幾種:
1. 修飾一個代碼塊,被修飾的代碼塊稱為同步語句塊,其作用的範圍是大括弧{}括起來的代碼,作用的對象是調用這個代碼塊的對象;
2. 修飾一個方法,被修飾的方法稱為同步方法,其作用的範圍是整個方法,作用的對象是調用這個方法的對象;
3. 修改一個靜態方法,其作用的範圍是整個靜態方法,作用的對象是這個類的所有對象;
4. 修改一個類,其作用的範圍是synchronized後面括弧括起來的部分,作用主的對象是這個類的所有對象。
修飾代碼塊
package com.qhong;public class Main { public static void main(String[] args) { SyncThread syncThread = new SyncThread(); Thread thread1 = new Thread(syncThread, "SyncThread1"); Thread thread2 = new Thread(syncThread, "SyncThread2"); thread1.start(); thread2.start(); }}class SyncThread implements Runnable { private static int count; public SyncThread() { count = 0; } public void run() { synchronized(this) { for (int i = 0; i < 5; i++) { try { System.out.println(Thread.currentThread().getName() + ":" + (count++)); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } public int getCount() { return count; }}
結果:
SyncThread1:0SyncThread1:1SyncThread1:2SyncThread1:3SyncThread1:4SyncThread2:5SyncThread2:6SyncThread2:7SyncThread2:8SyncThread2:9
View Code
當兩個並發線程(thread1和thread2)訪問同一個對象(syncThread)中的synchronized代碼塊時,
在同一時刻只能有一個線程得到執行,另一個線程受阻塞,必須等待當前線程執行完這個代碼塊以後才能執行該代碼塊。
Thread1和thread2是互斥的,因為在執行synchronized代碼塊時會鎖定當前的對象,只有執行完該代碼塊才能釋放該對象鎖,下一個線程才能執行並鎖定該對象。
我們再把SyncThread的調用稍微改一下:
package com.qhong;public class Main { public static void main(String[] args) { Thread thread1 = new Thread(new SyncThread(), "SyncThread1"); Thread thread2 = new Thread(new SyncThread(), "SyncThread2"); thread1.start(); thread2.start(); }}class SyncThread implements Runnable { private static int count; public SyncThread() { count = 0; } public void run() { synchronized(this) { for (int i = 0; i < 5; i++) { try { System.out.println(Thread.currentThread().getName() + ":" + (count++)); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } public int getCount() { return count; }}
Result:
SyncThread1:0SyncThread2:1SyncThread2:2SyncThread1:3SyncThread1:4SyncThread2:5SyncThread1:6SyncThread2:6SyncThread2:7SyncThread1:7
View Code
======================================================
修飾對象
package com.qhong;public class Main { public static void main(String[] args) { Account account = new Account("zhang san", 10000.0f); AccountOperator accountOperator = new AccountOperator(account); final int THREAD_NUM = 5; Thread threads[] = new Thread[THREAD_NUM]; for (int i = 0; i < THREAD_NUM; i ++) { threads[i] = new Thread(accountOperator, "Thread" + i); threads[i].start(); } }}/** * 銀行賬戶類 */class Account { String name; float amount; public Account(String name, float amount) { this.name = name; this.amount = amount; } //存錢 public void deposit(float amt) { amount += amt; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } //取錢 public void withdraw(float amt) { amount -= amt; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } public float getBalance() { return amount; }}/** * 賬戶操作類 */class AccountOperator implements Runnable{ private Account account; public AccountOperator(Account account) { this.account = account; } public void run() { synchronized (account) { account.deposit(500); account.withdraw(500); System.out.println(Thread.currentThread().getName() + ":" + account.getBalance()); } }}
Result:
Thread1:10000.0Thread4:10000.0Thread3:10000.0Thread2:10000.0Thread0:10000.0
View Code
當沒有明確的對象作為鎖,只是想讓一段代碼同步時,可以建立一個特殊的對象來充當鎖:
private byte[] lock = new byte[0]; // 特殊的instance變數 public void method() { synchronized(lock) { // todo 同步代碼塊 } }
=====================================================================
修飾一個方法:
======================================================================
修飾一個靜態方法:
package com.qhong;public class Main { public static void main(String[] args) { SyncThread syncThread1 = new SyncThread(); SyncThread syncThread2 = new SyncThread(); Thread thread1 = new Thread(syncThread1, "SyncThread1"); Thread thread2 = new Thread(syncThread2, "SyncThread2"); thread1.start(); thread2.start(); }}class SyncThread implements Runnable { private static int count; public SyncThread() { count = 0; } public synchronized static void method() { for (int i = 0; i < 5; i ++) { try { System.out.println(Thread.currentThread().getName() + ":" + (count++)); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void run() { method(); }}
Result:
SyncThread2:0SyncThread2:1SyncThread2:2SyncThread2:3SyncThread2:4SyncThread1:5SyncThread1:6SyncThread1:7SyncThread1:8SyncThread1:9
View Code
============================================================================
修飾一個類:
package com.qhong;public class Main { public static void main(String[] args) { SyncThread syncThread1 = new SyncThread(); SyncThread syncThread2 = new SyncThread(); Thread thread1 = new Thread(syncThread1, "SyncThread1"); Thread thread2 = new Thread(syncThread2, "SyncThread2"); thread1.start(); thread2.start(); }}class SyncThread implements Runnable { private static int count; public SyncThread() { count = 0; } public static void method() { synchronized(SyncThread.class) { for (int i = 0; i < 5; i ++) { try { System.out.println(Thread.currentThread().getName() + ":" + (count++)); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } public synchronized void run() { method(); }}
Result:
SyncThread1:0SyncThread1:1SyncThread1:2SyncThread1:3SyncThread1:4SyncThread2:5SyncThread2:6SyncThread2:7SyncThread2:8SyncThread2:9
View Code
http://blog.csdn.net/luoweifu/article/details/46613015
Java Synchronized的用法