Java學(xué)習(xí)隨記之多線程編程
Process和Thread
程序是指令和數(shù)據(jù)的有序集合, 本身沒(méi)有運(yùn)行的含義,是一個(gè)靜態(tài)的概念。
進(jìn)程是執(zhí)行程序的一次執(zhí)行過(guò)程,他是一個(gè)動(dòng)態(tài)的概念,是系統(tǒng)資源分配的單位
一個(gè)進(jìn)程中可以包含若干個(gè)線程,線程是CPU調(diào)度和執(zhí)行的單位
線程創(chuàng)建
三種創(chuàng)建方法
繼承Thread類(lèi)
//創(chuàng)建線程方法一:繼承Thread類(lèi),重寫(xiě)run() 方法,調(diào)用start開(kāi)啟主線程 public class TestThread01 extends Thread{ @Override public void run() { //run方法線程體 for (int i = 0; i < 2000; i++) { System.out.println("我在看代碼-----" + i); } } public static void main(String[] args) { //main線程,主線程 //創(chuàng)建一個(gè)線程對(duì)象 TestThread01 testThread01 = new TestThread01(); //調(diào)用start方法開(kāi)啟多線程,子線程調(diào)用run方法,主線程和子線程并行交替執(zhí)行 testThread01.start(); //testThread01.run(); //主線程調(diào)用run方法,只有主線程一條執(zhí)行路徑 for (int i = 0; i < 2000; i++) { System.out.println("Im" + i); } } }
總結(jié):注意,線程開(kāi)啟不一定立即執(zhí)行,由CPU調(diào)度處理
- 子類(lèi)繼承Thread類(lèi),具備多線程能力、
- 啟動(dòng)線程:子類(lèi)對(duì)象.start()
- 不推薦使用:避免OOP單繼承局限性
小練習(xí)
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; //練習(xí)Thread,實(shí)現(xiàn)多線程同步下載圖片 public class TestThread02 extends Thread{ private String url; private String name; public TestThread02(String url, String name) { this.url = url; this.name = name; } @Override public void run() { WebDownloader webDownloader = new WebDownloader(); webDownloader.downloadr(url, name); System.out.println("下載了:" + name); } public static void main(String[] args) { TestThread02 t1 = new TestThread02("https://img-blog.csdnimg.cn/20201130170256201.png?…3dlaXhpbl80NDE3NjM5Mw==,size_16,color_FFFFFF,t_70", "1.png"); TestThread02 t2 = new TestThread02("https://img-blog.csdnimg.cn/20201130170221843.png", "2.png"); TestThread02 t3 = new TestThread02("https://img-blog.csdnimg.cn/20201130170256201.png?…3dlaXhpbl80NDE3NjM5Mw==,size_16,color_FFFFFF,t_70", "3.png"); t1.start(); t2.start(); t3.start(); } } //下載器 class WebDownloader{ public void downloadr(String url, String name){ try { FileUtils.copyURLToFile(new URL(url), new File(name)); } catch (IOException e) { e.printStackTrace(); System.out.println("IO異常, downloader出現(xiàn)問(wèn)題"); } } }
實(shí)現(xiàn)Runnable
//創(chuàng)建線程方式二:實(shí)現(xiàn)Runnable接口,重寫(xiě)run方法,執(zhí)行線程需要丟入runnable接口實(shí)現(xiàn)類(lèi),調(diào)用start方法 public class TestThread03 implements Runnable{ @Override public void run() { //run方法線程體 for (int i = 0; i < 2000; i++) { System.out.println("我在看代碼-----" + i); } } public static void main(String[] args) { //創(chuàng)建runnable 接口的實(shí)現(xiàn)類(lèi)對(duì)象 TestThread03 testThread03 = new TestThread03(); //創(chuàng)建線程對(duì)象,通過(guò)線程對(duì)象來(lái)開(kāi)啟線程,代理 // Thread thread = new Thread(testThread03); // thread.start(); new Thread(testThread03).start(); for (int i = 0; i < 1000; i++) { System.out.println("Im" + i); } } }
總結(jié):
- 實(shí)現(xiàn)接口Runnable具備多線程能力
- 啟動(dòng)線程:傳入目標(biāo)對(duì)象+Thread對(duì)象.start()
- 推薦使用:避免單繼承局限性,靈活方便,方便同一個(gè)對(duì)象被線程使用
出現(xiàn)的問(wèn)題
多個(gè)線程操作操作同一個(gè)資源的情況下,線程不安全,數(shù)據(jù)紊亂
//多個(gè)線程同時(shí)操作同一個(gè)對(duì)象 //買(mǎi)火車(chē)票的例子 //發(fā)現(xiàn)問(wèn)題:多個(gè)線程操作操作同一個(gè)資源的情況下,線程不安全,數(shù)據(jù)紊亂 public class TestThread04 implements Runnable { //票數(shù) private int ticketNums = 10; @Override public void run() { while (true) { if (ticketNums <= 0) { break; } try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "拿到了第" + ticketNums-- + "票"); } } public static void main(String[] args) { TestThread04 testThread04 = new TestThread04(); new Thread(testThread04, "老師").start(); new Thread(testThread04, "黃牛").start(); new Thread(testThread04, "小明").start(); } }
實(shí)現(xiàn)Callable接口
1、實(shí)現(xiàn)Callable接口,需要返回值類(lèi)型
2、重寫(xiě)call方法,需要拋出異常
3、創(chuàng)建目標(biāo)對(duì)象
4、創(chuàng)建執(zhí)行服務(wù):ExecutorService ser = Executors.newFixedThreadPool(3);(使用了線程池)
5、提交執(zhí)行:Future r1 = ser.submit(t1);
6、獲取結(jié)果:boolean res1 = r1.get();
7、關(guān)閉服務(wù):ser.shutdown();
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.concurrent.*; //線程創(chuàng)建方式三:實(shí)現(xiàn)Callable接口(了解即可) // 實(shí)現(xiàn)Callable接口 public class TestCallable implements Callable<Boolean> { private String url; private String name; public TestCallable(String url, String name) { this.url = url; this.name = name; } @Override public Boolean call() { WebDownloader1 webDownloader1 = new WebDownloader1(); webDownloader1.downloadr(url, name); System.out.println("下載了:" + name); return true; } public static void main(String[] args) throws ExecutionException, InterruptedException { TestCallable t1 = new TestCallable("https://img-blog.csdnimg.cn/20201130170256201.png?…3dlaXhpbl80NDE3NjM5Mw==,size_16,color_FFFFFF,t_70", "1.png"); TestCallable t2 = new TestCallable("https://img-blog.csdnimg.cn/20201130170221843.png", "2.png"); TestCallable t3 = new TestCallable("https://img-blog.csdnimg.cn/20201130170256201.png?…3dlaXhpbl80NDE3NjM5Mw==,size_16,color_FFFFFF,t_70", "3.png"); //1、創(chuàng)建執(zhí)行服務(wù)() 線程池 ExecutorService ser = Executors.newFixedThreadPool(3); //2、提交執(zhí)行 Future<Boolean> r1 = ser.submit(t1); Future<Boolean> r2 = ser.submit(t2); Future<Boolean> r3 = ser.submit(t3); //3、獲取結(jié)果 boolean res1 = r1.get(); boolean res2 = r2.get(); boolean res3 = r3.get(); //4、關(guān)閉服務(wù) ser.shutdown(); } } //下載器 class WebDownloader1{ public void downloadr(String url, String name){ try { FileUtils.copyURLToFile(new URL(url), new File(name)); } catch (IOException e) { e.printStackTrace(); System.out.println("IO異常, downloader出現(xiàn)問(wèn)題"); } } }
Lambda表達(dá)式
為什么要使用lamba表達(dá)式
避免匿名內(nèi)部類(lèi)定義過(guò)多
可以讓代碼看起來(lái)更整潔
去掉了一堆沒(méi)有意義的代碼,只留下核心邏輯
示例
/** * 推導(dǎo)lambda表達(dá)式 */ public class TestLambda01 { //3、靜態(tài)內(nèi)部類(lèi) static class Like2 implements ILike { @Override public void lambda() { System.out.println("I like lambda2"); } } public static void main(String[] args) { ILike like = new Like(); like.lambda(); like = new Like2(); like.lambda(); //4、局部?jī)?nèi)部類(lèi) class Like3 implements ILike { @Override public void lambda() { System.out.println("I like lambda3"); } } like = new Like3(); like.lambda(); //5、匿名內(nèi)部類(lèi) like = new ILike() { @Override public void lambda() { System.out.println("I like lambda4"); } }; like.lambda(); //6、用lambda簡(jiǎn)化 jdk1.8特性 like = ()->{ System.out.println("I like lambda5"); }; like.lambda(); } } //1、定義一個(gè)函數(shù)式接口----必須有 interface ILike { void lambda(); } //2、實(shí)現(xiàn)類(lèi) class Like implements ILike { @Override public void lambda() { System.out.println("I like lambda"); } }
對(duì)于lambda表達(dá)式的簡(jiǎn)化
public class TestLambda02 { public static void main(String[] args) { //標(biāo)準(zhǔn)格式 // ILove love = (int a)->{ // System.out.println("I Love you " + a); // }; //簡(jiǎn)化1 去掉參數(shù)類(lèi)型,多個(gè)不同類(lèi)型的參數(shù)也可以直接去掉 ILove love = (a) -> { System.out.println("I love you " + a); }; //簡(jiǎn)化2 去掉括號(hào) -->僅單參數(shù) love = a->{ System.out.println("I love you " + a); }; //簡(jiǎn)化3 去掉花括號(hào) --> 僅lambda表達(dá)式有一行時(shí)才可 love = a -> System.out.println("I love you " + a); love.love(520); //使用lambda表達(dá)式僅適用于函數(shù)式接口(接口里只有一個(gè)函數(shù)接口) } } interface ILove { void love(int a); }
靜態(tài)代理
//靜態(tài)代理模式: //真實(shí)對(duì)象和代理對(duì)象都要實(shí)現(xiàn)同一個(gè)接口 //代理對(duì)象要代理真實(shí)角色 //好處: //代理對(duì)象可以做很多真實(shí)對(duì)象做不了的事 //真實(shí)對(duì)象專(zhuān)注做自己的事 public class StaticProxy { public static void main(String[] args) { You you = new You(); new Thread( () -> System.out.println("I Love You")).start(); new WeddingCompany(new You()). HappyMarry(); } } interface Marry { void HappyMarry(); } // 真實(shí)對(duì)象 class You implements Marry { @Override public void HappyMarry() { System.out.println("Happy"); } } // 代理 class WeddingCompany implements Marry { //代理誰(shuí)->真實(shí)目標(biāo)角色 private Marry target; public WeddingCompany(Marry target) { this.target = target; } @Override public void HappyMarry() { before(); this.target.HappyMarry(); after(); } private void after() { System.out.println("后"); } private void before() { System.out.println("前"); } }
線程狀態(tài)
方法 | 說(shuō)明 |
---|---|
setPriority(int newPriority) | 更改線程的優(yōu)先級(jí) |
static void sleep(long millis) | 在指定的毫秒數(shù)內(nèi)讓當(dāng)前正在執(zhí)行的線程休眠 |
void join() | 等待該線程終止 |
static void yield() | 暫停當(dāng)前正在執(zhí)行的線程對(duì)象,并執(zhí)行其他線程 |
void interrupt() | 中斷線程,別用這個(gè)方式 |
boolean isAlive() | 測(cè)試線程是否處于某個(gè)活動(dòng)狀態(tài) |
線程終止
- 不推薦使用JDK提供的stop()、destroy()方法
- 推薦線程自己停止下來(lái)
- 建議使用一個(gè)標(biāo)志位進(jìn)行終止變量,當(dāng)flag = false,則終止線程運(yùn)行
//測(cè)試stop //1、建議線程正常停止---->利用次數(shù),不建議死循環(huán) //2、建議使用標(biāo)志位----->設(shè)置一個(gè)標(biāo)志位 //3、不要使用stop或者destroy等過(guò)時(shí)或者jdk不推薦的方法 public class TestStop implements Runnable { //1、設(shè)置一個(gè)標(biāo)志位 private boolean flag = true; @Override public void run() { int i = 0; while (flag) { System.out.println("run-----thread" + i++); } } //2、設(shè)置一個(gè)公開(kāi)的方法停止線程,轉(zhuǎn)換標(biāo)志位 public void stop() { this.flag = false; } public static void main(String[] args) throws InterruptedException { TestStop testStop = new TestStop(); new Thread(testStop).start(); for (int i = 0; i < 1000; i++) { Thread.sleep(1); if (i == 900) { testStop.stop(); System.out.println("Stop"); break; } } } }
線程休眠
- sleep(時(shí)間) 指定當(dāng)前線程阻塞的毫秒數(shù)
- sleep存在異常InterruptedException
- sleep時(shí)間達(dá)到后進(jìn)入就緒狀態(tài)
- sleep可以模擬網(wǎng)絡(luò)延時(shí)(放大問(wèn)題的發(fā)生性),倒計(jì)時(shí)等
- 每一個(gè)對(duì)象都有一個(gè)鎖,sleep不會(huì)釋放鎖
import java.text.SimpleDateFormat; import java.util.Date; public class TestSleep2 { //模擬倒計(jì)時(shí) public static void tenDown(){ int num = 10; while (num > 0) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(num--); } } public static void main(String[] args) { //打印當(dāng)前時(shí)間 Date startTime = new Date(System.currentTimeMillis()); //獲取時(shí)間 while (true) { try { Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:MM:SS").format(startTime)); startTime = new Date(System.currentTimeMillis());//更新時(shí)間 } catch (InterruptedException e) { e.printStackTrace(); } } } }
線程禮讓
- 禮讓線程,讓當(dāng)前正在執(zhí)行的線程停止,但不阻塞
- 讓線程從運(yùn)行態(tài)轉(zhuǎn)換為就緒態(tài)
- 讓CPU重新進(jìn)行調(diào)度,禮讓不一定成功,看CPU心情
public class TestYield { public static void main(String[] args) { Myyield myyield = new Myyield(); new Thread(myyield, "A").start(); new Thread(myyield, "B").start(); } } class Myyield implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + "線程開(kāi)始"); Thread.yield(); System.out.println(Thread.currentThread().getName() + "線程停止"); } }
JOIN
JOIN合并線程,待此線程執(zhí)行完后,再執(zhí)行其他線程,其他線程阻塞
可以想象成插隊(duì)
//插隊(duì) public class TestJoin implements Runnable{ @Override public void run() { try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } for (int i = 0; i < 100; i++) { System.out.println("VIP" + i); } } public static void main(String[] args) { TestJoin testJoin = new TestJoin(); Thread thread = new Thread(testJoin); thread.start(); for (int i = 0; i < 1000; i++) { if (i == 200) { try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("main" + i); } } }
線程狀態(tài)觀測(cè)
線程狀態(tài)。線程可以處于以下?tīng)顟B(tài)之一:
NEW
尚未啟動(dòng)的線程處于此狀態(tài)。
RUNNABLE
在Java虛擬機(jī)中執(zhí)行的線程處于此狀態(tài)。
BLOCKED
被阻塞等待監(jiān)視器鎖定的線程處于此狀態(tài)。
WAITING
無(wú)限期等待另一個(gè)線程執(zhí)行特定操作的線程處于此狀態(tài)。
TIMED_WAITING
正在等待另一個(gè)線程執(zhí)行最多指定等待時(shí)間的操作的線程處于此狀態(tài)。
TERMINATED
已退出的線程處于此狀態(tài)。
//觀測(cè)測(cè)試線程的狀態(tài) public class TestStatus { public static void main(String[] args) { Thread thread = new Thread(() -> { for (int i = 0; i < 5; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(i); } System.out.println("http://///////////"); }); //觀察狀態(tài) Thread.State state = thread.getState(); System.out.println(state); //觀察啟動(dòng)后 thread.start(); state = thread.getState(); System.out.println(state); while (state != Thread.State.TERMINATED) { //只要線程不終止,就一直輸出狀態(tài) try { Thread.sleep(100); state = thread.getState(); System.out.println(state); } catch (InterruptedException e) { e.printStackTrace(); } } } }
線程的優(yōu)先級(jí)
Java提供一個(gè)線程調(diào)度器來(lái)監(jiān)控程序中啟動(dòng)后進(jìn)入就緒態(tài)的所有線程,線程調(diào)度器按照優(yōu)先級(jí)決定應(yīng)該調(diào)度哪個(gè)線程來(lái)執(zhí)行
線程的優(yōu)先級(jí)用數(shù)字表示,范圍從1~10
- Thread.MIN_PRIORITY = 1;
- Thread.MAX_PRIORITY = 10;
- Thread.NORM_PRIORITY = 5;
使用以下方法改變或者獲取優(yōu)先級(jí)
- getPriority
- setPriority
優(yōu)先級(jí)低只是意味著獲得調(diào)度的概率低,并不是優(yōu)先級(jí)低的就不會(huì)調(diào)用了,這都是看CPU的調(diào)度
public class TestPriority extends Thread { public static void main(String[] args) { System.out.println(Thread.currentThread().getName() + "====>" + Thread.currentThread().getPriority()); MyPriority myPriority = new MyPriority(); Thread t1 = new Thread(myPriority); Thread t2 = new Thread(myPriority); Thread t3 = new Thread(myPriority); Thread t4 = new Thread(myPriority); Thread t5 = new Thread(myPriority); t1.start(); t2.setPriority(1); t2.start(); t3.setPriority(4); t3.start(); t4.setPriority(Thread.MAX_PRIORITY); t4.start(); t5.setPriority(8); t5.start(); } } class MyPriority implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + "====>" + Thread.currentThread().getPriority()); } }
守護(hù)(daemon)線程
- 線程分為用戶(hù)線程和守護(hù)線程
- JVM虛擬機(jī)必須確保用戶(hù)線程執(zhí)行完畢
- JVM虛擬機(jī)不用等待守護(hù)線程執(zhí)行完畢
public class TestDaemon { public static void main(String[] args) { God god = new God(); You you = new You(); Thread thread = new Thread(god); thread.setDaemon(true);//默認(rèn)式false表示是用戶(hù)線程,正常的線程是用戶(hù)線程 thread.start(); new Thread(you).start(); } } //上帝 class God implements Runnable { @Override public void run() { while (true) { System.out.println("god"); } } } //你 class You implements Runnable { @Override public void run() { for (int i = 0; i < 36500; i++) { System.out.println("Happy"); } System.out.println("Goodbye world"); } }
同步(synchronized)
synchronized
由于我們可以提出private關(guān)鍵字來(lái)保證數(shù)據(jù)對(duì)象只能被方法訪問(wèn),所以我們只需要針對(duì)方法提出一套機(jī)制,這套機(jī)制就是synchronized關(guān)鍵字,它包括兩種用法:synchronized方法和synchronized塊。
public synchronized void method(){}
synchronized方法控制對(duì)“對(duì)象”的訪問(wèn),每個(gè)對(duì)象對(duì)應(yīng)一把鎖,每個(gè)synchronized方法都必須獲得調(diào)用該方法的對(duì)象的鎖才能執(zhí)行,否則線程會(huì)阻塞,方法一旦執(zhí)行,就獨(dú)占該鎖,直到該方法返回才釋放鎖,后面被阻塞的線程才能獲得這個(gè)鎖,繼續(xù)執(zhí)行
缺陷:若將一個(gè)大的方法聲明為synchronized,將會(huì)影響效率
同步方法
synchronized方法控制對(duì)成員變量或者類(lèi)屬性對(duì)象的訪問(wèn),每個(gè)對(duì)象對(duì)應(yīng)一把鎖。寫(xiě)法如下:
public synchronized void test(){ //。。。。 }
- 如果修飾的是具體對(duì)象:鎖的是對(duì)象
- 如果修飾的是成員方法:鎖的是this
- 如果修飾的是靜態(tài)方法:鎖的就是這個(gè)對(duì)象.class
每個(gè)synchronized方法都必須獲得該方法的對(duì)象的鎖才能執(zhí)行,否則所屬的這個(gè)線程阻塞,方法一旦執(zhí)行,就獨(dú)占該鎖,直到該方法返回時(shí),鎖釋放。
原程序:
public class Checkout { public static void main(String[] args) { Account account = new Account(200000, "禮金"); Drawing you = new Drawing(account, 80000, "你"); Drawing wife = new Drawing(account, 140000, "your wife"); you.start(); wife.start(); } } class Account { int money; String name; public Account(int money, String name) { this.money = money; this.name = name; } } class Drawing extends Thread { Account account; int outMoney; int outTotal; public Drawing(Account account, int outMoney, String name) { super(name); this.account = account; this.outMoney = outMoney; } @Override public void run() { test(); } public void test() { if (account.money < outMoney) { System.out.println("余額不足"); return; } try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } account.money -= outMoney; outTotal += outMoney; System.out.println(this.getName() + "-----賬戶(hù)余額為:" + account.money); System.out.println(this.getName() + "-----總共取到了:" + outTotal); } }
顯然,上面的代碼會(huì)出現(xiàn)負(fù)數(shù),但是我們不希望它出現(xiàn)負(fù)數(shù)。
同步方法的寫(xiě)法代碼,以上面代碼為例,直接在提款機(jī)的操作,把run方法或者里面的內(nèi)容提出來(lái)變成test,加上synchronized修飾:
@Override public void run(){ } public synchronized void test(){ }
但是這樣仍會(huì)發(fā)現(xiàn)出現(xiàn)負(fù)數(shù),鎖定失敗。
分析:
我們認(rèn)為在test方法里進(jìn)行的對(duì)象修改,所以把他鎖上就好了,但是,對(duì)于這個(gè)類(lèi),這個(gè)提款機(jī)來(lái)說(shuō),test時(shí)成員方法,因此鎖的對(duì)象實(shí)際上是this,也就是提款機(jī)(Drawing)。
但是我們的初衷,希望線程鎖定的資源是Account對(duì)象,而不是提款機(jī)對(duì)象。
同步塊
除了方法,synchronized還可以修飾塊,叫做同步塊。
synchronized修飾同步塊的方式是:
synchonized (obj){ }
其中的obj可以是任何對(duì)象,但是用到它,肯定是設(shè)置為那個(gè)共享資源,這個(gè)obj被稱(chēng)為同步監(jiān)視器。同步監(jiān)視器的作用就是,判斷這個(gè)監(jiān)視器是否被鎖定(是否能訪問(wèn)),從而決定其是否能執(zhí)行其中的代碼。
Java的花括號(hào)中內(nèi)容有一下幾種:
- 方法里面的塊:局部快。解決變量作用域的問(wèn)題,快速釋放內(nèi)存(比如方法里面再有個(gè)for循環(huán),里面的變量)
- 類(lèi)層的塊:構(gòu)造塊。初始化信息,和構(gòu)造方法是一樣的
- 類(lèi)層的靜態(tài)塊:靜態(tài)構(gòu)造塊。最早加載,不是對(duì)象的信息,而是類(lèi)的信息;
- 方法里面的同步塊:監(jiān)視對(duì)象。
第四種就是我們目前學(xué)習(xí)的同步塊。
注意:如果是同步方法里,沒(méi)必要指定同步監(jiān)視器,因?yàn)橥椒椒ǖ谋O(jiān)視器已經(jīng)是this或者.class
使用同步塊對(duì)提款機(jī)問(wèn)題進(jìn)行修改:
public void test(){ synchronized(account){ } }
也就是加上對(duì)account的監(jiān)視器,鎖住了這個(gè)對(duì)象 ,這樣運(yùn)行結(jié)果就正確了。
這種做法的效率不高,因?yàn)殡m然對(duì)account上了鎖,但是每一次都要把整個(gè)流程走一遍,方法體內(nèi)的內(nèi)容是很多的,另外,每次加鎖與否,都是對(duì)性能的消耗,進(jìn)入之后再出來(lái),哪怕什么都不做,也是消耗。
其實(shí),我們可以在加鎖的外面再加一重判斷,那么之后就沒(méi)有必要再進(jìn)行鎖的過(guò)程了。
public void test(){ if (account.money == 0){ return; } synchronized(account){ } }
就是這樣的代碼,在并發(fā)量很高的時(shí)候,往往可以大大提高效率。
問(wèn)題
synchronized塊太小,可能鎖不住,安全性又太低了,鎖的方法太大,又會(huì)降低效率,所以要很注意控制范圍?
以上就是Java學(xué)習(xí)隨記之多線程編程的詳細(xì)內(nèi)容,更多關(guān)于Java多線程的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Intellij?IDEA根據(jù)maven依賴(lài)名查找它是哪個(gè)pom.xml引入的(圖文詳解)
這篇文章主要介紹了Intellij?IDEA根據(jù)maven依賴(lài)名查找它是哪個(gè)pom.xml引入的,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08springboot集成spark并使用spark-sql的示例詳解
這篇文章主要介紹了spring-boot集成spark并使用spark-sql的方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02mybatis注解之@Mapper和@MapperScan的使用
這篇文章主要介紹了mybatis注解之@Mapper和@MapperScan的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10利用Java和c語(yǔ)言寫(xiě)一個(gè)計(jì)算器
這篇文章我們就來(lái)分享如何利用Java和c語(yǔ)言來(lái)寫(xiě)一個(gè)計(jì)算器,文章附有代碼詳細(xì)說(shuō)明,感興趣得小伙伴可以參考下面文章得具體內(nèi)容2021-10-10IntelliJ?IDEA運(yùn)行SpringBoot項(xiàng)目的詳細(xì)步驟
這篇文章主要介紹了IntelliJ?IDEA如何運(yùn)行SpringBoot項(xiàng)目,步驟一配置maven,步驟二配置JDK環(huán)境,緊接著通過(guò)步驟三檢查數(shù)據(jù)庫(kù)的配置,最后一步數(shù)據(jù)庫(kù)連接,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08SpringBoot利用注解來(lái)實(shí)現(xiàn)Redis分布式鎖
有些業(yè)務(wù)請(qǐng)求,屬于耗時(shí)操作,需要加鎖,防止后續(xù)的并發(fā)操作,同時(shí)對(duì)數(shù)據(jù)庫(kù)的數(shù)據(jù)進(jìn)行操作,需要避免對(duì)之前的業(yè)務(wù)造成影響。本文將利用注解來(lái)實(shí)現(xiàn)Redis分布式鎖,需要的可以參考一下2022-09-09