加了一個synchronized鎖,程序就崩了

一 故事背景

一個非常頻繁調(diào)用方法加了synchronized 導(dǎo)致程序崩潰

二 模擬事故的發(fā)生

  1. 創(chuàng)建線程池模擬多用戶訪問

    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,0L,TimeUnit.MICROSECONDS,new ArrayBlockingQueue<>(10));
    
    for (int i = 0; i <1000; i++) {
        threadPoolExecutor.execute(()->{
            testSync();
        });
    }
     
    public static synchronized void  testSync(){
        try {
            System.out.println("Hi 線程池");
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    

    會發(fā)生以下錯誤

    Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.sunisco.collect.ugqd.web.pages.Test$$Lambda$1/1642360923@6aa8ceb6 rejected from java.util.concurrent.ThreadPoolExecutor@2530c12[Running, pool size = 10, active threads = 10, queued tasks = 10, completed tasks = 0]
            at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
            at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
            at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
            at com.sunisco.collect.ugqd.web.pages.Test.main(Test.java:24)
    

    這是因?yàn)檫\(yùn)行的數(shù)量大于了隊(duì)列的數(shù)量蜗顽,修改后繼續(xù)測試

    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,0L,TimeUnit.MICROSECONDS,new ArrayBlockingQueue<>(100));

    for (int i = 0; i <100; i++) {
        threadPoolExecutor.execute(()->{
            testSync();
        });
    }
     
    public static synchronized void  testSync(){
        try {
            System.out.println("Hi 線程池");
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 輸出如下
image.png
  1. 通過jps查看 所有系統(tǒng)中java進(jìn)程pid, 找到我們程序的pid 12308

    C:\Users\Administrator>jps
    11088 RemoteMavenServer
    8352 Test
    12308 Test
    10632 Launcher
    9384 Launcher
    11100
    6572 Jps
    9180 Test
    
  2. 通過jstack 12308查看該進(jìn)程下的線程信息

     C:\Users\Administrator>jstack 12308
     2021-05-13 09:34:53
     Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.141-b15 mixed mode):
    
     "DestroyJavaVM" #21 prio=5 os_prio=0 tid=0x000000001ec4f800 nid=0x27bc waiting on condition [0x0000000000000000]
        java.lang.Thread.State: RUNNABLE
    
     "pool-1-thread-10" #20 prio=5 os_prio=0 tid=0x000000001ec4e000 nid=0x1650 waiting for monitor entry [0x000000001ff8f000]
        java.lang.Thread.State: BLOCKED (on object monitor)
             at com.sunisco.collect.ugqd.web.pages.Test.getg(Test.java:55)
             - waiting to lock <0x000000076b740d80> (a java.lang.Class for com.sunisco.collect.ugqd.web.pages.Test)
             at com.sunisco.collect.ugqd.web.pages.Test.lambda$main$0(Test.java:27)
             at com.sunisco.collect.ugqd.web.pages.Test$$Lambda$1/1642360923.run(Unknown Source)
             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
             at java.lang.Thread.run(Thread.java:748)
    
     "pool-1-thread-9" #19 prio=5 os_prio=0 tid=0x000000001ec4a000 nid=0x2a88 waiting on condition [0x000000001fe8f000]
        java.lang.Thread.State: TIMED_WAITING (sleeping)
             at java.lang.Thread.sleep(Native Method)
             at com.sunisco.collect.ugqd.web.pages.Test.getg(Test.java:56)
             - locked <0x000000076b740d80> (a java.lang.Class for com.sunisco.collect.ugqd.web.pages.Test)
             at com.sunisco.collect.ugqd.web.pages.Test.lambda$main$0(Test.java:27)
             at com.sunisco.collect.ugqd.web.pages.Test$$Lambda$1/1642360923.run(Unknown Source)
             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
             at java.lang.Thread.run(Thread.java:748)
    
    
    
    
     "pool-1-thread-2" #12 prio=5 os_prio=0 tid=0x000000001ea78000 nid=0x1250 waiting for monitor entry [0x000000001f78f000]
        java.lang.Thread.State: BLOCKED (on object monitor)
             at com.sunisco.collect.ugqd.web.pages.Test.getg(Test.java:55)
             - waiting to lock <0x000000076b740d80> (a java.lang.Class for com.sunisco.collect.ugqd.web.pages.Test)
             at com.sunisco.collect.ugqd.web.pages.Test.lambda$main$0(Test.java:27)
             at com.sunisco.collect.ugqd.web.pages.Test$$Lambda$1/1642360923.run(Unknown Source)
             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
             at java.lang.Thread.run(Thread.java:748)
    
     "pool-1-thread-1" #11 prio=5 os_prio=0 tid=0x000000001ec11800 nid=0xa38 waiting for monitor entry [0x000000001f68f000]
        java.lang.Thread.State: BLOCKED (on object monitor)
             at com.sunisco.collect.ugqd.web.pages.Test.getg(Test.java:55)
             - waiting to lock <0x000000076b740d80> (a java.lang.Class for com.sunisco.collect.ugqd.web.pages.Test)
             at com.sunisco.collect.ugqd.web.pages.Test.lambda$main$0(Test.java:27)
             at com.sunisco.collect.ugqd.web.pages.Test$$Lambda$1/1642360923.run(Unknown Source)
             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
             at java.lang.Thread.run(Thread.java:748)
    
     "Service Thread" #10 daemon prio=9 os_prio=0 tid=0x000000001e93c000 nid=0x226c runnable [0x0000000000000000]
        java.lang.Thread.State: RUNNABLE
    
     "C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001e8c9800 nid=0x21d8 waiting on condition [0x0000000000000000]
        java.lang.Thread.State: RUNNABLE
     "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001e84b000 nid=0x1c4c runnable [0x000000001f08e000]
        java.lang.Thread.State: RUNNABLE
             at java.net.SocketInputStream.socketRead0(Native Method)
             at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
             at java.net.SocketInputStream.read(SocketInputStream.java:171)
             at java.net.SocketInputStream.read(SocketInputStream.java:141)
             at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
             at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
             at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
             - locked <0x000000076b82f3e0> (a java.io.InputStreamReader)
             at java.io.InputStreamReader.read(InputStreamReader.java:184)
             at java.io.BufferedReader.fill(BufferedReader.java:161)
             at java.io.BufferedReader.readLine(BufferedReader.java:324)
             - locked <0x000000076b82f3e0> (a java.io.InputStreamReader)
             at java.io.BufferedReader.readLine(BufferedReader.java:389)
             at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
    
     "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001cdf9800 nid=0x35ec waiting on condition [0x0000000000000000]
        java.lang.Thread.State: RUNNABLE
    
     "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001cde4800 nid=0x34e8 runnable [0x0000000000000000]
        java.lang.Thread.State: RUNNABLE
    
     "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001cdbf000 nid=0x54c in Object.wait() [0x000000001e12f000]
        java.lang.Thread.State: WAITING (on object monitor)
             at java.lang.Object.wait(Native Method)
             - waiting on <0x000000076b188ec8> (a java.lang.ref.ReferenceQueue$Lock)
             at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
             - locked <0x000000076b188ec8> (a java.lang.ref.ReferenceQueue$Lock)
             at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
             at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
    
     "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000003687000 nid=0x3318 in Object.wait() [0x000000001e02e000]
        java.lang.Thread.State: WAITING (on object monitor)
             at java.lang.Object.wait(Native Method)
             - waiting on <0x000000076b186b68> (a java.lang.ref.Reference$Lock)
             at java.lang.Object.wait(Object.java:502)
             at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
             - locked <0x000000076b186b68> (a java.lang.ref.Reference$Lock)
             at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
    
     "VM Thread" os_prio=2 tid=0x000000001cd97800 nid=0x2d1c runnable
    
     "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000035ad000 nid=0x1efc runnable
    
     "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000035ae800 nid=0xa50 runnable
    
     "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000035b1000 nid=0x1bac runnable
    
     "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000035b3000 nid=0x2d50 runnable
    
     "VM Periodic Task Thread" os_prio=2 tid=0x000000001ea1a800 nid=0x27f4 waiting on condition
    
     JNI global references: 335
    

從jstack日志上我們看到許多block狀態(tài)的線程雨让,生產(chǎn)問題就是因?yàn)樘嗟腂lock狀態(tài)的線程雇盖,產(chǎn)生的阻塞,最終導(dǎo)致程序的崩潰栖忠。

三 結(jié)語

鎖這種處理并發(fā)帶來的數(shù)據(jù)不一致的確有效崔挖,但是如果使用不當(dāng),對性能會產(chǎn)生極大的影響庵寞,所以使用前一定要仔細(xì)考慮狸相。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市皇帮,隨后出現(xiàn)的幾起案子卷哩,更是在濱河造成了極大的恐慌蛋辈,老刑警劉巖属拾,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異冷溶,居然都是意外死亡渐白,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進(jìn)店門逞频,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纯衍,“玉大人,你說我怎么就攤上這事苗胀〗笾睿” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵基协,是天一觀的道長歌亲。 經(jīng)常有香客問我,道長澜驮,這世上最難降的妖魔是什么陷揪? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上悍缠,老公的妹妹穿的比我還像新娘卦绣。我一直安慰自己,他們只是感情好飞蚓,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布滤港。 她就那樣靜靜地躺著,像睡著了一般趴拧。 火紅的嫁衣襯著肌膚如雪蜗搔。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天八堡,我揣著相機(jī)與錄音樟凄,去河邊找鬼。 笑死兄渺,一個胖子當(dāng)著我的面吹牛缝龄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播挂谍,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼叔壤,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了口叙?” 一聲冷哼從身側(cè)響起炼绘,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎妄田,沒想到半個月后俺亮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡疟呐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年脚曾,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片启具。...
    茶點(diǎn)故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡本讥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出鲁冯,到底是詐尸還是另有隱情拷沸,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布薯演,位于F島的核電站撞芍,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏涣仿。R本人自食惡果不足惜勤庐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一示惊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧愉镰,春花似錦米罚、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至碗降,卻和暖如春隘竭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背讼渊。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工动看, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人爪幻。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓菱皆,卻偏偏與公主長得像,于是被迫代替她去往敵國和親挨稿。 傳聞我的和親對象是個殘疾皇子仇轻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容