Java多線程及線程安全實(shí)現(xiàn)方法解析
一、java多線程實(shí)現(xiàn)的兩種方式
1、繼承Thread
/** * * @version: 1.1.0 * @Description: 多線程 * @author: wsq * @date: 2020年6月8日下午2:25:33 */ public class MyThread extends Thread{ @Override public void run() { System.out.println("This is the first thread!"); } public static void main(String[] args) { MyThread mt = new MyThread(); mt.start(); } }
2、實(shí)現(xiàn) Runnable 接口
public class MultithreadingTest { public static void main(String[] args) { new Thread(() -> System.out.println("This is the first thread!")).start(); } }
或者
public class MyThreadImpl implements Runnable{ private int count = 5; @Override public void run() { // TODO Auto-generated method stub count--; System.out.println("Thread"+Thread.currentThread().getName()+"count:"+count); } }
二、解決線程不安全問(wèn)題
/** * * @version: 1.1.0 * @Description: 測(cè)試類 * @author: wsq * @date: 2020年6月8日下午9:27:02 */ public class Test { public static void main(String[] args) { MyThreadImpl myThreadImpl = new MyThreadImpl(); Thread A = new Thread(myThreadImpl,"A"); Thread B = new Thread(myThreadImpl,"B"); Thread C = new Thread(myThreadImpl,"C"); Thread D = new Thread(myThreadImpl,"D"); Thread E = new Thread(myThreadImpl,"E"); A.start(); B.start(); C.start(); D.start(); E.start(); } }
打印結(jié)果為:
ThreadBcount:3
ThreadCcount:2
ThreadAcount:3
ThreadDcount:1
ThreadEcount:0
B和A共用一個(gè)線程,存在線程安全問(wèn)題
改成:
public class MyThreadImpl implements Runnable{ private int count = 5; @Override // 使用同步解決線程安全問(wèn)題 synchronized public void run() { // TODO Auto-generated method stub count--; System.out.println("Thread"+Thread.currentThread().getName()+"count:"+count); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot2如何集成ElasticSearch6.4.3
這篇文章主要介紹了springboot2如何集成ElasticSearch6.4.3問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07SpringBoot/Spring?AOP默認(rèn)動(dòng)態(tài)代理方式實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于SpringBoot/Spring?AOP默認(rèn)動(dòng)態(tài)代理方式的相關(guān)資料,Spring AOP是一款基于Java的AOP框架,其中默認(rèn)采用動(dòng)態(tài)代理方式實(shí)現(xiàn)AOP功能,本文將詳細(xì)介紹動(dòng)態(tài)代理的實(shí)現(xiàn)原理和使用方法,需要的朋友可以參考下2023-03-03Mybatis中注入執(zhí)行sql查詢、更新、新增及建表語(yǔ)句案例代碼
這篇文章主要介紹了Mybatis中注入執(zhí)行sql查詢、更新、新增以及建表語(yǔ)句,主要說(shuō)明一個(gè)另類的操作,注入sql,并使用mybatis執(zhí)行,結(jié)合案例代碼詳解講解,需要的朋友可以參考下2023-02-02Java編程實(shí)現(xiàn)的二維數(shù)組轉(zhuǎn)置功能示例
這篇文章主要介紹了Java編程實(shí)現(xiàn)的二維數(shù)組轉(zhuǎn)置功能,結(jié)合實(shí)例形式分析了Java二維數(shù)組的遍歷、運(yùn)算、賦值等實(shí)現(xiàn)轉(zhuǎn)置的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01SpringBoot使用責(zé)任鏈模式優(yōu)化業(yè)務(wù)邏輯中的if-else代碼
在開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)遇到需要根據(jù)不同的條件執(zhí)行不同的邏輯的情況,我們可以考慮使用責(zé)任鏈模式來(lái)優(yōu)化代碼結(jié)構(gòu),使得代碼更加清晰、可擴(kuò)展和易于維護(hù)2023-06-06解決@Validated注解無(wú)效,嵌套對(duì)象屬性的@NotBlank無(wú)效問(wèn)題
這篇文章主要介紹了解決@Validated注解無(wú)效,嵌套對(duì)象屬性的@NotBlank無(wú)效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java編程關(guān)于子類重寫父類方法問(wèn)題的理解
這篇文章主要介紹了Java編程關(guān)于子類重寫父類方法問(wèn)題的理解,分享了有關(guān)子類重寫父類的實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11解讀httpclient的validateAfterInactivity連接池狀態(tài)檢測(cè)
這篇文章主要為大家介紹了httpclient的validateAfterInactivity連接池狀態(tài)檢測(cè)解讀*,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11