如何在打印一个字符串的同时打印另一个字符串?

How can I print a string while another one keeps printing?

我写了一些简单的代码来显示一个不断左右移动的加载词。是否可以在前一个字符串继续打印的同时打印另一个字符串?

它看起来像这样:

..loading (The word keeps moving)
The main file loaded.
The backup file loaded.
.... (these line get printed only once while the loading is still moving)

我希望下一行的代码与加载代码分开。

加载字码:

public static void main( String[] args ) throws Exception
    {
        for ( int i=0; i<86; i++ )
        {
            if ( i%17 == 0 )
                System.out.print(".......... \r");
            if ( i%17 == 1 )
                System.out.print(".........L \r");
            if ( i%17 == 2 )
                System.out.print("........Lo \r");
            if ( i%17 == 3 )
                System.out.print(".......Loa \r");
            if ( i%17 == 4 )
                System.out.print("......Load \r");
            if ( i%17 == 5 )
                System.out.print(".....Laodi \r");
            if ( i%17 == 6 )
                System.out.print("....Loadin \r");
            if ( i%17 == 7 )
                System.out.print("...Loading \r");
            if ( i%17 == 8 )
                System.out.print("..Loading. \r");
            if ( i%17 == 9 )
                System.out.print(".Loading.. \r");
            if ( i%17 == 10 )
                System.out.print("Loading... \r");
            if ( i%17 == 11 )
                System.out.print("oading.... \r");
            if ( i%17 == 12 )
                System.out.print("ading..... \r");
            if ( i%17 == 13 )
                System.out.print("ding...... \r");
            if ( i%17 == 14 )
                System.out.print("ing....... \r");
            if ( i%17 == 15 )
                System.out.print("ng........ \r");
            if ( i%17 == 16 )
                System.out.print("g......... \r");


            Thread.sleep(150);
        }

    }

您必须在单独的 Thread 中执行此操作。不幸的是,您必须确保在 print 期间您的两个线程不会进行上下文切换。假设您要打印单词 loadingCOUNTING。然后,在没有任何同步的情况下,打印的消息很可能是这样的:loCOUaNTdiINGing.

请在此处阅读 Java 中的并发性:http://tutorials.jenkov.com/java-concurrency/index.html

Adam 正试图告诉您必须使用线程。

一个线程在java中是一个进程,代码在主线程处理器中执行,但你可以创建另一个线程处理器同时做另一件事,在计算机信息中,这种事情被称为并发。

我建议您在 Java 中了解有关线程的更多信息,也许这会对您有所帮助:http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html

对于另一边,你可以在条件的末尾加上一个System.out.println来放置另一个字符串。