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

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

如何讓程序等到計時器線程完成后再繼續(xù)?

如何讓程序等到計時器線程完成后再繼續(xù)?

子衿沉夜 2023-11-10 15:26:54
我正在做一個學(xué)習(xí)java的小程序,我想打印一些東西,等待幾秒鐘(通過打印顯示......)然后繼續(xù)該程序。我通過使用Timer和做到了這一點TimerTask。我想知道如何讓程序的執(zhí)行等到我的計時器線程結(jié)束。我嘗試timer.wait()在某些地方添加但不起作用。public class Main{    public static void main(String[] args)    {        System.out.println("text1");        clock currentClock = new clock();        currentClock.run();        // i want the program to stop until this is over, adding currentClock.wait(); doesnt work        System.out.println("text2");    }}class clockHelper extends TimerTask{ // printing class, called by clock class    int actual = 0;    public void run()    {        if (actual < 3)        {            System.out.println(".");            actual++;        } else        {            cancel();        }    }}class clock{ // called by main class    public static void run()    {        Timer watch = new Timer();        TimerTask timer2 = new clockHelper();        watch.schedule(timer2, 1000, 500);        // adding watch.wait(); doesn't work, i can also stop it here and it should work fine    }}這是我得到的錯誤:java.lang.IllegalMonitorStateException我想要的結(jié)果是打印這個:“text”。。?!拔谋?”如果我刪除wait(這會導(dǎo)致錯誤),則會打?。?"text1" "text2" 。。。在此先感謝您的任何信息。
查看完整描述

1 回答

?
慕哥9229398

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

你可以這樣做:


public class Main

{

    public static void main(String[] args) throws InterruptedException

    {

        System.out.println("text1");

        for (int i = 0; i < 3; i++) {

            System.out.print(".");

            Thread.sleep(1000);

        }

        System.out.println();

        System.out.println("text2");

    }

}


查看完整回答
反對 回復(fù) 2023-11-10
  • 1 回答
  • 0 關(guān)注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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