Java學(xué)習(xí)隨記之多線程編程
Process和Thread
程序是指令和數(shù)據(jù)的有序集合, 本身沒有運(yùn)行的含義,是一個靜態(tài)的概念。
進(jìn)程是執(zhí)行程序的一次執(zhí)行過程,他是一個動態(tài)的概念,是系統(tǒng)資源分配的單位
一個進(jìn)程中可以包含若干個線程,線程是CPU調(diào)度和執(zhí)行的單位
線程創(chuàng)建
三種創(chuàng)建方法
繼承Thread類
//創(chuàng)建線程方法一:繼承Thread類,重寫run() 方法,調(diào)用start開啟主線程
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)建一個線程對象
TestThread01 testThread01 = new TestThread01();
//調(diào)用start方法開啟多線程,子線程調(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é):注意,線程開啟不一定立即執(zhí)行,由CPU調(diào)度處理
- 子類繼承Thread類,具備多線程能力、
- 啟動線程:子類對象.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)問題");
}
}
}
實(shí)現(xiàn)Runnable
//創(chuàng)建線程方式二:實(shí)現(xiàn)Runnable接口,重寫run方法,執(zhí)行線程需要丟入runnable接口實(shí)現(xiàn)類,調(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)類對象
TestThread03 testThread03 = new TestThread03();
//創(chuàng)建線程對象,通過線程對象來開啟線程,代理
// 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具備多線程能力
- 啟動線程:傳入目標(biāo)對象+Thread對象.start()
- 推薦使用:避免單繼承局限性,靈活方便,方便同一個對象被線程使用
出現(xiàn)的問題
多個線程操作操作同一個資源的情況下,線程不安全,數(shù)據(jù)紊亂
//多個線程同時操作同一個對象
//買火車票的例子
//發(fā)現(xiàn)問題:多個線程操作操作同一個資源的情況下,線程不安全,數(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接口,需要返回值類型
2、重寫call方法,需要拋出異常
3、創(chuàng)建目標(biāo)對象
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)問題");
}
}
}
Lambda表達(dá)式
為什么要使用lamba表達(dá)式
避免匿名內(nèi)部類定義過多
可以讓代碼看起來更整潔
去掉了一堆沒有意義的代碼,只留下核心邏輯
示例
/**
* 推導(dǎo)lambda表達(dá)式
*/
public class TestLambda01 {
//3、靜態(tài)內(nè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、局部內(nèi)部類
class Like3 implements ILike {
@Override
public void lambda() {
System.out.println("I like lambda3");
}
}
like = new Like3();
like.lambda();
//5、匿名內(nèi)部類
like = new ILike() {
@Override
public void lambda() {
System.out.println("I like lambda4");
}
};
like.lambda();
//6、用lambda簡化 jdk1.8特性
like = ()->{
System.out.println("I like lambda5");
};
like.lambda();
}
}
//1、定義一個函數(shù)式接口----必須有
interface ILike {
void lambda();
}
//2、實(shí)現(xiàn)類
class Like implements ILike {
@Override
public void lambda() {
System.out.println("I like lambda");
}
}
對于lambda表達(dá)式的簡化
public class TestLambda02 {
public static void main(String[] args) {
//標(biāo)準(zhǔn)格式
// ILove love = (int a)->{
// System.out.println("I Love you " + a);
// };
//簡化1 去掉參數(shù)類型,多個不同類型的參數(shù)也可以直接去掉
ILove love = (a) -> {
System.out.println("I love you " + a);
};
//簡化2 去掉括號 -->僅單參數(shù)
love = a->{
System.out.println("I love you " + a);
};
//簡化3 去掉花括號 --> 僅lambda表達(dá)式有一行時才可
love = a -> System.out.println("I love you " + a);
love.love(520);
//使用lambda表達(dá)式僅適用于函數(shù)式接口(接口里只有一個函數(shù)接口)
}
}
interface ILove {
void love(int a);
}
靜態(tài)代理
//靜態(tài)代理模式:
//真實(shí)對象和代理對象都要實(shí)現(xiàn)同一個接口
//代理對象要代理真實(shí)角色
//好處:
//代理對象可以做很多真實(shí)對象做不了的事
//真實(shí)對象專注做自己的事
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í)對象
class You implements Marry {
@Override
public void HappyMarry() {
System.out.println("Happy");
}
}
// 代理
class WeddingCompany implements Marry {
//代理誰->真實(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)
| 方法 | 說明 |
|---|---|
| setPriority(int newPriority) | 更改線程的優(yōu)先級 |
| static void sleep(long millis) | 在指定的毫秒數(shù)內(nèi)讓當(dāng)前正在執(zhí)行的線程休眠 |
| void join() | 等待該線程終止 |
| static void yield() | 暫停當(dāng)前正在執(zhí)行的線程對象,并執(zhí)行其他線程 |
| void interrupt() | 中斷線程,別用這個方式 |
| boolean isAlive() | 測試線程是否處于某個活動狀態(tài) |
線程終止
- 不推薦使用JDK提供的stop()、destroy()方法
- 推薦線程自己停止下來
- 建議使用一個標(biāo)志位進(jìn)行終止變量,當(dāng)flag = false,則終止線程運(yùn)行
//測試stop
//1、建議線程正常停止---->利用次數(shù),不建議死循環(huán)
//2、建議使用標(biāo)志位----->設(shè)置一個標(biāo)志位
//3、不要使用stop或者destroy等過時或者jdk不推薦的方法
public class TestStop implements Runnable {
//1、設(shè)置一個標(biāo)志位
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag) {
System.out.println("run-----thread" + i++);
}
}
//2、設(shè)置一個公開的方法停止線程,轉(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(時間) 指定當(dāng)前線程阻塞的毫秒數(shù)
- sleep存在異常InterruptedException
- sleep時間達(dá)到后進(jìn)入就緒狀態(tài)
- sleep可以模擬網(wǎng)絡(luò)延時(放大問題的發(fā)生性),倒計時等
- 每一個對象都有一個鎖,sleep不會釋放鎖
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep2 {
//模擬倒計時
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)前時間
Date startTime = new Date(System.currentTimeMillis()); //獲取時間
while (true) {
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:MM:SS").format(startTime));
startTime = new Date(System.currentTimeMillis());//更新時間
} 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() + "線程開始");
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)觀測
線程狀態(tài)。線程可以處于以下狀態(tài)之一:
NEW
尚未啟動的線程處于此狀態(tài)。
RUNNABLE
在Java虛擬機(jī)中執(zhí)行的線程處于此狀態(tài)。
BLOCKED
被阻塞等待監(jiān)視器鎖定的線程處于此狀態(tài)。
WAITING
無限期等待另一個線程執(zhí)行特定操作的線程處于此狀態(tài)。
TIMED_WAITING
正在等待另一個線程執(zhí)行最多指定等待時間的操作的線程處于此狀態(tài)。
TERMINATED
已退出的線程處于此狀態(tài)。
//觀測測試線程的狀態(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);
//觀察啟動后
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)先級
Java提供一個線程調(diào)度器來監(jiān)控程序中啟動后進(jìn)入就緒態(tài)的所有線程,線程調(diào)度器按照優(yōu)先級決定應(yīng)該調(diào)度哪個線程來執(zhí)行
線程的優(yōu)先級用數(shù)字表示,范圍從1~10
- Thread.MIN_PRIORITY = 1;
- Thread.MAX_PRIORITY = 10;
- Thread.NORM_PRIORITY = 5;
使用以下方法改變或者獲取優(yōu)先級
- getPriority
- setPriority
優(yōu)先級低只是意味著獲得調(diào)度的概率低,并不是優(yōu)先級低的就不會調(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ù)線程
- JVM虛擬機(jī)必須確保用戶線程執(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表示是用戶線程,正常的線程是用戶線程
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)鍵字來保證數(shù)據(jù)對象只能被方法訪問,所以我們只需要針對方法提出一套機(jī)制,這套機(jī)制就是synchronized關(guān)鍵字,它包括兩種用法:synchronized方法和synchronized塊。
public synchronized void method(){}
synchronized方法控制對“對象”的訪問,每個對象對應(yīng)一把鎖,每個synchronized方法都必須獲得調(diào)用該方法的對象的鎖才能執(zhí)行,否則線程會阻塞,方法一旦執(zhí)行,就獨(dú)占該鎖,直到該方法返回才釋放鎖,后面被阻塞的線程才能獲得這個鎖,繼續(xù)執(zhí)行
缺陷:若將一個大的方法聲明為synchronized,將會影響效率
同步方法
synchronized方法控制對成員變量或者類屬性對象的訪問,每個對象對應(yīng)一把鎖。寫法如下:
public synchronized void test(){
//。。。。
}
- 如果修飾的是具體對象:鎖的是對象
- 如果修飾的是成員方法:鎖的是this
- 如果修飾的是靜態(tài)方法:鎖的就是這個對象.class
每個synchronized方法都必須獲得該方法的對象的鎖才能執(zhí)行,否則所屬的這個線程阻塞,方法一旦執(zhí)行,就獨(dú)占該鎖,直到該方法返回時,鎖釋放。
原程序:
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() + "-----賬戶余額為:" + account.money);
System.out.println(this.getName() + "-----總共取到了:" + outTotal);
}
}
顯然,上面的代碼會出現(xiàn)負(fù)數(shù),但是我們不希望它出現(xiàn)負(fù)數(shù)。
同步方法的寫法代碼,以上面代碼為例,直接在提款機(jī)的操作,把run方法或者里面的內(nèi)容提出來變成test,加上synchronized修飾:
@Override
public void run(){
}
public synchronized void test(){
}
但是這樣仍會發(fā)現(xiàn)出現(xiàn)負(fù)數(shù),鎖定失敗。
分析:
我們認(rèn)為在test方法里進(jìn)行的對象修改,所以把他鎖上就好了,但是,對于這個類,這個提款機(jī)來說,test時成員方法,因此鎖的對象實(shí)際上是this,也就是提款機(jī)(Drawing)。
但是我們的初衷,希望線程鎖定的資源是Account對象,而不是提款機(jī)對象。
同步塊
除了方法,synchronized還可以修飾塊,叫做同步塊。
synchronized修飾同步塊的方式是:
synchonized (obj){ }
其中的obj可以是任何對象,但是用到它,肯定是設(shè)置為那個共享資源,這個obj被稱為同步監(jiān)視器。同步監(jiān)視器的作用就是,判斷這個監(jiān)視器是否被鎖定(是否能訪問),從而決定其是否能執(zhí)行其中的代碼。
Java的花括號中內(nèi)容有一下幾種:
- 方法里面的塊:局部快。解決變量作用域的問題,快速釋放內(nèi)存(比如方法里面再有個for循環(huán),里面的變量)
- 類層的塊:構(gòu)造塊。初始化信息,和構(gòu)造方法是一樣的
- 類層的靜態(tài)塊:靜態(tài)構(gòu)造塊。最早加載,不是對象的信息,而是類的信息;
- 方法里面的同步塊:監(jiān)視對象。
第四種就是我們目前學(xué)習(xí)的同步塊。
注意:如果是同步方法里,沒必要指定同步監(jiān)視器,因?yàn)橥椒椒ǖ谋O(jiān)視器已經(jīng)是this或者.class
使用同步塊對提款機(jī)問題進(jìn)行修改:
public void test(){
synchronized(account){
}
}
也就是加上對account的監(jiān)視器,鎖住了這個對象 ,這樣運(yùn)行結(jié)果就正確了。
這種做法的效率不高,因?yàn)殡m然對account上了鎖,但是每一次都要把整個流程走一遍,方法體內(nèi)的內(nèi)容是很多的,另外,每次加鎖與否,都是對性能的消耗,進(jìn)入之后再出來,哪怕什么都不做,也是消耗。
其實(shí),我們可以在加鎖的外面再加一重判斷,那么之后就沒有必要再進(jìn)行鎖的過程了。
public void test(){
if (account.money == 0){
return;
}
synchronized(account){
}
}
就是這樣的代碼,在并發(fā)量很高的時候,往往可以大大提高效率。
問題
synchronized塊太小,可能鎖不住,安全性又太低了,鎖的方法太大,又會降低效率,所以要很注意控制范圍?
以上就是Java學(xué)習(xí)隨記之多線程編程的詳細(xì)內(nèi)容,更多關(guān)于Java多線程的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Intellij?IDEA根據(jù)maven依賴名查找它是哪個pom.xml引入的(圖文詳解)
這篇文章主要介紹了Intellij?IDEA根據(jù)maven依賴名查找它是哪個pom.xml引入的,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
springboot集成spark并使用spark-sql的示例詳解
這篇文章主要介紹了spring-boot集成spark并使用spark-sql的方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
mybatis注解之@Mapper和@MapperScan的使用
這篇文章主要介紹了mybatis注解之@Mapper和@MapperScan的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
IntelliJ?IDEA運(yùn)行SpringBoot項(xiàng)目的詳細(xì)步驟
這篇文章主要介紹了IntelliJ?IDEA如何運(yùn)行SpringBoot項(xiàng)目,步驟一配置maven,步驟二配置JDK環(huán)境,緊接著通過步驟三檢查數(shù)據(jù)庫的配置,最后一步數(shù)據(jù)庫連接,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
SpringBoot利用注解來實(shí)現(xiàn)Redis分布式鎖
有些業(yè)務(wù)請求,屬于耗時操作,需要加鎖,防止后續(xù)的并發(fā)操作,同時對數(shù)據(jù)庫的數(shù)據(jù)進(jìn)行操作,需要避免對之前的業(yè)務(wù)造成影響。本文將利用注解來實(shí)現(xiàn)Redis分布式鎖,需要的可以參考一下2022-09-09

