java 多线程同步
一.synchronized關(guān)鍵字
同步方法
每個(gè)對(duì)象都包含一把鎖(也叫做監(jiān)視器),它自動(dòng)稱為對(duì)象的一部分(不必為此寫任何特殊的代碼)。調(diào)用任何synchronized方法時(shí),對(duì)象就會(huì)被鎖定,不可再調(diào)用那個(gè)對(duì)象的其他任何synchronized方法,除非第一個(gè)方法完成了自己的工作。
示例代碼如下:
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();}}}同步塊
在進(jìn)入同步塊之前,必須在synchObject上取得鎖。如果已有其他線程取得了這把鎖,塊便不能進(jìn)入,必須等候那把鎖被釋放。一般情況下,當(dāng)前對(duì)象作為鎖來使用。
示例代碼如下:
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();}}}注:同步是一種高開銷的操作,因此應(yīng)該盡量減少同步的內(nèi)容。 通常沒有必要同步整個(gè)方法,使用synchronized代碼塊同步關(guān)鍵代碼即可。
二.使用特殊域變量(volatile)
volatile關(guān)鍵字為域變量的訪問提供了一種免鎖機(jī)制,使用volatile修飾域相當(dāng)于告訴虛擬機(jī)該域可能會(huì)被其他線程更新, 因此每次使用該域就要重新計(jì)算,而不是使用寄存器中的值,volatile不會(huì)提供任何原子操作,它也不能用來修飾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();}}}注:由于線程執(zhí)行速度不一樣,所以控制臺(tái)打印的結(jié)果不全是按從大到小來的。
三.使用java.util.concurrent包
在JavaSE5.0中新增了一個(gè)java.util.concurrent包來支持同步。ReentrantLock類是可重入、互斥、實(shí)現(xiàn)了Lock接口的鎖,它與使用synchronized方法和快具有相同的基本行為和語(yǔ)義,并且擴(kuò)展了其能力。
示例代碼如下:
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管理變量,則每一個(gè)使用該變量的線程都獲得該變量的副本,副本之間相互獨(dú)立,這樣每一個(gè)線程都可以隨意修改自己的變量副本,而不會(huì)對(duì)其他線程產(chǎn)生影響。
示例代碼如下:
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();}}}轉(zhuǎn)載于:https://www.cnblogs.com/minisculestep/p/4984346.html
總結(jié)
以上是生活随笔為你收集整理的java 多线程同步的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springmvc的ModelAttri
- 下一篇: 提升业务价值 APM应用与整合分享