java 多线程同步
一.synchronized關鍵字
同步方法
每個對象都包含一把鎖(也叫做監視器),它自動稱為對象的一部分(不必為此寫任何特殊的代碼)。調用任何synchronized方法時,對象就會被鎖定,不可再調用那個對象的其他任何synchronized方法,除非第一個方法完成了自己的工作。
示例代碼如下:
public class SimpleThread implements Runnable {private int count = 0;@Overridepublic void run() {while (true) {try {test();} catch (InterruptedException e) {e.printStackTrace();}}}private synchronized void test() throws InterruptedException {System.out.println(Thread.currentThread().getName() + ":" + count++);Thread.sleep(100);}public static void main(String[] args) {SimpleThread sd = new SimpleThread();for (int i = 0; i < 3; i++) {new Thread(sd).start();}}}同步塊
在進入同步塊之前,必須在synchObject上取得鎖。如果已有其他線程取得了這把鎖,塊便不能進入,必須等候那把鎖被釋放。一般情況下,當前對象作為鎖來使用。
示例代碼如下:
public class SimpleThread implements Runnable {private int count = 0;@Overridepublic void run() {while (true) {try {test();} catch (InterruptedException e) {e.printStackTrace();}}}private void test() throws InterruptedException {synchronized (this) {System.out.println(Thread.currentThread().getName() + ":" + count++);}Thread.sleep(100);}public static void main(String[] args) {SimpleThread sd = new SimpleThread();for (int i = 0; i < 3; i++) {new Thread(sd).start();}}}注:同步是一種高開銷的操作,因此應該盡量減少同步的內容。 通常沒有必要同步整個方法,使用synchronized代碼塊同步關鍵代碼即可。
二.使用特殊域變量(volatile)
volatile關鍵字為域變量的訪問提供了一種免鎖機制,使用volatile修飾域相當于告訴虛擬機該域可能會被其他線程更新, 因此每次使用該域就要重新計算,而不是使用寄存器中的值,volatile不會提供任何原子操作,它也不能用來修飾final類型的變量。
public class SimpleThread implements Runnable {private volatile int count = 0;@Overridepublic void run() {while (true) {try {test();} catch (InterruptedException e) {e.printStackTrace();}}}private void test() throws InterruptedException {System.out.println(Thread.currentThread().getName() + ":" + count++);Thread.sleep(100);}public static void main(String[] args) {SimpleThread sd = new SimpleThread();for (int i = 0; i < 3; i++) {new Thread(sd).start();}}}注:由于線程執行速度不一樣,所以控制臺打印的結果不全是按從大到小來的。
三.使用java.util.concurrent包
在JavaSE5.0中新增了一個java.util.concurrent包來支持同步。ReentrantLock類是可重入、互斥、實現了Lock接口的鎖,它與使用synchronized方法和快具有相同的基本行為和語義,并且擴展了其能力。
示例代碼如下:
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;public class SimpleThread implements Runnable {private int count = 0;private Lock lock = new ReentrantLock();@Overridepublic void run() {while (true) {try {test();} catch (InterruptedException e) {e.printStackTrace();}}}private void test() throws InterruptedException {lock.lock();try {System.out.println(Thread.currentThread().getName() + ":" + count++);} finally {lock.unlock();}Thread.sleep(100);}public static void main(String[] args) {SimpleThread sd = new SimpleThread();for (int i = 0; i < 3; i++) {new Thread(sd).start();}}}四.使用ThreadLocal管理變量
如果使用ThreadLocal管理變量,則每一個使用該變量的線程都獲得該變量的副本,副本之間相互獨立,這樣每一個線程都可以隨意修改自己的變量副本,而不會對其他線程產生影響。
示例代碼如下:
public class SimpleThread implements Runnable {private ThreadLocal<Integer> count = new ThreadLocal<Integer>() {@Overrideprotected Integer initialValue() {return 0;}};@Overridepublic void run() {while (true) {try {test();} catch (InterruptedException e) {e.printStackTrace();}}}private void test() throws InterruptedException {System.out.println(Thread.currentThread().getName() + ":" + count.get().toString());count.set(count.get() + 1);Thread.sleep(100);}public static void main(String[] args) {SimpleThread sd = new SimpleThread();for (int i = 0; i < 3; i++) {new Thread(sd).start();}}}轉載于:https://www.cnblogs.com/minisculestep/p/4984346.html
總結
以上是生活随笔為你收集整理的java 多线程同步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springmvc的ModelAttri
- 下一篇: 提升业务价值 APM应用与整合分享