線程安全性的定義:
當多個線程訪問某個類的時候,不管運行時環境採用何種調度方式或者這些進程將如何交替執行,並且在主調代碼中不需要任何額外的同步或者協同,這個類都能表現出正確的行爲,那麼我們就稱則這個類是線程安全的
原子性的鎖有兩種:
synchronized:是Java中的關鍵字,是一種同步鎖,依賴於JVM
Lock:依賴特殊的CPU指令,代碼實現,ReentrantLock
這裏我們先來了解synchronized
1、修飾代碼塊:大括號括起來的代碼,作用於調用的對象
2、修飾方法:整個方法,作用於調用的對象
3、修飾靜態方法:整個靜態方法,作用於所有對象
4、修飾類:括號括起來的部分,作用於所有對象
修飾代碼塊
package com.lyy.concurrency.sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SynchronizedExample1 {
// 修飾一個代碼塊
public void test1(int j){
synchronized (this){
for (int i = 0; i < 10; i++) {
System.out.println("test1 j:"+j+" — i:"+i);
}
}
}
public static void main(String[] args) {
SynchronizedExample1 example1 = new SynchronizedExample1();
SynchronizedExample1 example2 = new SynchronizedExample1();
ExecutorService executorService = Executors.newCachedThreadPool();//聲明一個線程池
//加上線程池相當於我們調用了兩個線程
//兩個線程調用了同一個對象
executorService.execute(() ->{
example1.test1(1);
});
executorService.execute(() ->{
example2.test1(2);
});
}
}
返回結果:
test1 j:1 — i:0
test1 j:1 — i:1
test1 j:1 — i:2
test1 j:1 — i:3
test1 j:1 — i:4
test1 j:1 — i:5
test1 j:1 — i:6
test1 j:1 — i:7
test1 j:1 — i:8
test1 j:1 — i:9
test1 j:2 — i:0
test1 j:2 — i:1
test1 j:2 — i:2
test1 j:2 — i:3
test1 j:2 — i:4
test1 j:2 — i:5
test1 j:2 — i:6
test1 j:2 — i:7
test1 j:2 — i:8
test1 j:2 — i:9
線程1和線程2按照各自順序執行,線程一和線程二都能夠按照自己的同步代碼走下去,但是不一定能保證線程一執行完之後纔到線程二執行,這就要看哪一個線程能夠率先搶到資源。
修飾方法
package com.lyy.concurrency.sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SynchronizedExample1 {
//修飾一個方法
public synchronized void test2(int j){
for (int i = 0; i < 10; i++) {
System.out.println("test2 j:"+j+" — i:"+i);
}
}
public static void main(String[] args) {
SynchronizedExample1 example1 = new SynchronizedExample1();
SynchronizedExample1 example2 = new SynchronizedExample1();
ExecutorService executorService = Executors.newCachedThreadPool();//聲明一個線程池
//加上線程池相當於我們調用了兩個線程
//兩個線程調用了同一個對象
executorService.execute(() ->{
example1.test2(1);
});
executorService.execute(() ->{
example2.test2(2);
});
}
}
返回結果:
test2 j:1 — i:0
test2 j:1 — i:1
test2 j:1 — i:2
test2 j:1 — i:3
test2 j:1 — i:4
test2 j:1 — i:5
test2 j:1 — i:6
test2 j:1 — i:7
test2 j:2 — i:0
test2 j:2 — i:1
test2 j:2 — i:2
test2 j:2 — i:3
test2 j:2 — i:4
test2 j:2 — i:5
test2 j:2 — i:6
test2 j:2 — i:7
test2 j:2 — i:8
test2 j:1 — i:8
test2 j:2 — i:9
test2 j:1 — i:9
我們可以看到1 和2 是交替運行的,但是各自都是按照順序在執行,這裏是因爲修飾代碼塊只能作用於當前調用的對象,我們這裏是調用了兩個方法所以,兩個線程則互不干擾,都是各自執行各自的代碼,同步是整個方法
注意:如果SynchronizedExample1 是個子類 那麼實現test2的時候是不會攜帶synchronized關鍵字 ,因爲synchronized是不屬於方法聲明的一部分,因此,synchronized關鍵字不能被繼承,如果想要去實現這個子類繼承synchronized,需要我們手動是實現這個功能
修飾類
package com.lyy.concurrency.sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SynchronizedExample2 {
// 修飾一個類
public static void test1(int j){
synchronized (SynchronizedExample2.class){
for (int i = 0; i < 10; i++) {
System.out.println("test1 j:"+j+" — i:"+i);
}
}
}
public static void main(String[] args) {
SynchronizedExample2 example1 = new SynchronizedExample2();
SynchronizedExample2 example2 = new SynchronizedExample2();
ExecutorService executorService = Executors.newCachedThreadPool();//聲明一個線程池
//加上線程池相當於我們調用了兩個線程 l
//兩個線程調用了同一個對象
executorService.execute(() ->{
example1.test1(1);
});
executorService.execute(() ->{
example2.test1(2);
});
}
}
返回結果:
test1 j:1 — i:0
test1 j:1 — i:1
test1 j:1 — i:2
test1 j:1 — i:3
test1 j:1 — i:4
test1 j:1 — i:5
test1 j:1 — i:6
test1 j:1 — i:7
test1 j:1 — i:8
test1 j:1 — i:9
test1 j:2 — i:0
test1 j:2 — i:1
test1 j:2 — i:2
test1 j:2 — i:3
test1 j:2 — i:4
test1 j:2 — i:5
test1 j:2 — i:6
test1 j:2 — i:7
test1 j:2 — i:8
test1 j:2 — i:9
修飾靜態方法
package com.lyy.concurrency.sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SynchronizedExample2 {
//修飾一個靜態方法
public static synchronized void test2(int j){
for (int i = 0; i < 10; i++) {
System.out.println("test2 j:"+j+" — i:"+i);
}
}
public static void main(String[] args) {
SynchronizedExample2 example1 = new SynchronizedExample2();
SynchronizedExample2 example2 = new SynchronizedExample2();
ExecutorService executorService = Executors.newCachedThreadPool();//聲明一個線程池
//加上線程池相當於我們調用了兩個線程 l
//兩個線程調用了同一個對象
executorService.execute(() ->{
example1.test2(1);
});
executorService.execute(() ->{
example2.test2(2);
});
}
}
返回結果:
test2 j:1 — i:0
test2 j:1 — i:1
test2 j:1 — i:2
test2 j:1 — i:3
test2 j:1 — i:4
test2 j:1 — i:5
test2 j:1 — i:6
test2 j:1 — i:7
test2 j:1 — i:8
test2 j:1 — i:9
test2 j:2 — i:0
test2 j:2 — i:1
test2 j:2 — i:2
test2 j:2 — i:3
test2 j:2 — i:4
test2 j:2 — i:5
test2 j:2 — i:6
test2 j:2 — i:7
test2 j:2 — i:8
test2 j:2 — i:9
案例:
線程不安全案例:
package com.lyy.concurrency.example.count;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class CountExample1 {
//請求總數
public static int clientTotal = 5000;
//同時併發執行的線程數
public static int threadTotal = 200;
//
public static int count = 0;
public static void main(String[] args) throws Exception{
ExecutorService executorService = Executors.newCachedThreadPool();//線程池
final Semaphore semaphore = new Semaphore(threadTotal);//允許併發的數量
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
executorService.execute(() ->{
try {
semaphore.acquire();//判斷線程是否允許被執行
add();//當acquire()返回出來值之後纔會被執行
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown();
System.out.println("count:"+count);
}
private static void add(){
count++;
}
}
執行結果:
count:4973
我們看到執行結果是4973,而正確的執行結果應該是5000,那麼我們怎麼才能讓結果顯示爲5000呢,就看接下來我們使用synchronized實現一個線程安全的類
線程安全的類:
package com.lyy.concurrency.example.count;
import com.lyy.concurrency.annoatioons.NotThreadSafe;
import com.lyy.concurrency.annoatioons.ThreadSafe;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
@Slf4j
@ThreadSafe //線程安全的類
public class CountExample3 {
//請求總數
public static int clientTotal = 5000;
//同時併發執行的線程數
public static int threadTotal = 200;
//
public static int count = 0;
public static void main(String[] args) throws Exception{
ExecutorService executorService = Executors.newCachedThreadPool();//線程池
final Semaphore semaphore = new Semaphore(threadTotal);//允許併發的數量
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
for (int i = 0; i < clientTotal; i++) {
executorService.execute(() ->{
try {
semaphore.acquire();//判斷線程是否允許被執行
add();//當acquire()返回出來值之後纔會被執行
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown();
System.out.println("count:"+count);
}
private synchronized static void add(){
count++;
}
}
在這裏我們加了一個synchronized,就可以使我們的結果顯示爲正確的5000
返回結果:
count:5000
總結:
1、synchronized:是不可中斷鎖,適合競爭不激烈,可讀性比較好
2、無論synchronized關鍵字加在方法上還是對象上,如果它作用的對象是非靜態的,則它取得的鎖是對象;
3、如果synchronized作用的對象是一個靜態方法或一個類,則它取得的鎖是對類,該類所有的對象同一把鎖。
4、每個對象只有一個鎖(lock)與之相關聯,誰拿到這個鎖誰就可以運行它所控制的那段代碼。
5、實現同步是要很大的系統開銷作爲代價的,甚至可能造成死鎖,所以儘量避免無謂的同步控制。