第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么在@PostConstruct 方法中實際上只能啟動一個線程?

為什么在@PostConstruct 方法中實際上只能啟動一個線程?

jeck貓 2023-06-08 17:22:59
@Componentclass Type{    @PostConstruct    private void postConstructor() {        Runnable threadAlpha = () -> {            while (true) {                workWithSomething();                try {                    Thread.sleep(1000 * 60);                } catch (InterruptedException e) {                }            }        };        Runnable threadBeta = () -> {            while (true) {                workWithOtherthing();                try {                    Thread.sleep(1000 * 3);                } catch (InterruptedException e) {                }            }        };        threadBeta.run();        threadAlpha.run();    }}使用 spring-framework,我正在努力處理這段代碼,問題是只有一個線程可以實際啟動run(),首先調(diào)用,另一個似乎凍結(jié),如果我將位置切換為:        threadAlpha.run();        threadBeta.run();然后 threadBeta 從未啟動,為什么會發(fā)生這樣的事情?
查看完整描述

4 回答

?
飲歌長嘯

TA貢獻(xiàn)1951條經(jīng)驗 獲得超3個贊

因為您沒有創(chuàng)建線程。取而代之的是,您正在創(chuàng)建Runnable實例,然后運行它們的run方法。

而是這樣做:

new Thread(threadAlpha).start();
new Thread(threadBeta).start();


查看完整回答
反對 回復(fù) 2023-06-08
?
手掌心

TA貢獻(xiàn)1942條經(jīng)驗 獲得超3個贊

當(dāng)您調(diào)用threadBeta.run()threadAlpha.run()時,您正在調(diào)用一個具有無限循環(huán)的方法。您不是在創(chuàng)建線程。這就是為什么即使threadBeta.run()處于無限循環(huán)中,threadAlpha.run() 也不會執(zhí)行。相反,您可以這樣做:

new Thread(threadBeta).start();
new Thread(threadAlpha).start();


查看完整回答
反對 回復(fù) 2023-06-08
?
蠱毒傳說

TA貢獻(xiàn)1895條經(jīng)驗 獲得超3個贊

Runnable run() 在當(dāng)前線程中執(zhí)行,因此行為。如果您想在兩個單獨的線程中運行,請使用Thread并調(diào)用start它們:


public class SpringMultipleThreads {

    public static void main(String[] args) {

        new SpringMultipleThreads().postConstructor();

    }

    private void postConstructor() {


        Thread threadAlpha = new Thread(() -> {

            while (true) {

                System.out.println("1");

                try {

                    Thread.sleep(1000 * 3);

                } catch (InterruptedException e) {

                }

            }

        });

        Thread threadBeta = new Thread(() -> {

            while (true) {

                System.out.println("2");

                try {

                    Thread.sleep(1000 * 3);

                } catch (InterruptedException e) {

                }

            }

        });

        threadBeta.start();

        threadAlpha.start();

    }

}


查看完整回答
反對 回復(fù) 2023-06-08
?
明月笑刀無情

TA貢獻(xiàn)1828條經(jīng)驗 獲得超4個贊

當(dāng)您調(diào)用threadAlpha.run()并threadBeta.run()在當(dāng)前線程中執(zhí)行它時。為了在新線程中簡單執(zhí)行,您可以使用:


Thread t1 = new Thread(threadAlpha);

t1.start();

Thread t2 = new Thread(threadBeta);

t2.start();


查看完整回答
反對 回復(fù) 2023-06-08
  • 4 回答
  • 0 關(guān)注
  • 361 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號