日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 多线程同步

發布時間:2025/3/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 多线程同步的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。