如何在Java中将每个线程的输出放在一行中

如何在Java中将每个线程的输出放在一行中,java,multithreading,parallel-processing,synchronization,system.out,Java,Multithreading,Parallel Processing,Synchronization,System.out,我正在学习多线程,但同时我对Java不是很有经验。从这个例子 输出是这样的 thread1: Java thread2: Java thread1: is thread2: is thread1: hot, thread1: aromatic, thread2: hot, thread1: and thread1: invigorating. Thread 1 is dead. thread2: aromatic, thread2: and 有可能有这样的输出吗 thread1:Java is

我正在学习多线程,但同时我对Java不是很有经验。从这个例子

输出是这样的

thread1: Java
thread2: Java
thread1: is
thread2: is
thread1: hot,
thread1: aromatic,
thread2: hot,
thread1: and
thread1: invigorating.
Thread 1 is dead.
thread2: aromatic,
thread2: and
有可能有这样的输出吗

thread1:Java is hot, aromatic, and invigorating. is dead.
thread2:Java is hot, aromatic, and

因此,基本上thread1的输出始终链接在终端的第一行,thread2链接在第二行?

您可以使用StringBuilder将字符串附加到您在该点已有的字符串中,并在处理结束时打印完整的字符串:

StringBuilder sb = new StringBuilder();
sb.append(text);
然后使用

System.out.println(sb.toString());
这将在处理结束时显示结果


请注意,如果多个线程正在修改应用程序中的
StringBuilder
的同一个实例,则某些修改可能会丢失。在这种情况下,您应该使用
StringBuffer

您可以使用StringBuilder将字符串附加到您在该点已有的字符串,并在处理结束时打印完整的字符串:

StringBuilder sb = new StringBuilder();
sb.append(text);
然后使用

System.out.println(sb.toString());
这将在处理结束时显示结果

请注意,如果多个线程正在修改应用程序中的
StringBuilder
的同一个实例,则某些修改可能会丢失。在这种情况下,您应该使用
StringBuffer

有可能有这样的输出吗

thread1:Java is hot, aromatic, and invigorating. is dead.
thread2:Java is hot, aromatic, and
线程1:爪哇是热,芳香,和振兴。他死了

thread2:Java是热的,芳香的,并且

当然,您只需要确保每个线程一次性打印整个字符串

所以基本上thread1的输出总是链接在第一行 在端子中,螺纹2在第2行中

有几种方法可以实现这一点,例如,您可以使用:

允许一组线程全部等待的同步辅助工具 彼此到达一个共同的障碍点。骑自行车的人很有用 在涉及固定大小线程组的程序中,必须 偶尔互相等待。这种势垒被称为循环势垒,因为 释放等待的线程后,可以重新使用它

协调线程,使输出一个接一个地进行

以下是一个完整的示例:

public class Mult {
 
    public static void main(String[] args) {

        final int total_threads = 2;
        final CyclicBarrier barrier = new CyclicBarrier(total_threads);
        String msg1 = "thread1:Java is hot, aromatic, and invigorating. is dead.";
        String msg2 = "thread2:Java is hot, aromatic, and";
        Thread thread1 = new Thread(() -> parallel_task1(barrier, msg1));
        Thread thread2 = new Thread(() -> parallel_task2(barrier, msg2));
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
    }

    private static void parallel_task1(CyclicBarrier barrier, String msg1) {
        System.out.println(msg1);
        wait_for_barrier(barrier);
    }

    private static void parallel_task2(CyclicBarrier barrier, String msg1) {
        wait_for_barrier(barrier);
        System.out.println(msg1);
    }

    private static void wait_for_barrier(CyclicBarrier barrier) {
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
另一种方法是向用户报告

这种方法的一个例子是:

public class Mult {

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

        final int total_threads = 2;
        ExecutorService executor = Executors.newFixedThreadPool(total_threads);
        Future<String> future1 = executor.submit(Mult::parallel_task1);
        Future<String> future2 = executor.submit(Mult::parallel_task2);
        String thread1_msg = future1.get();
        String thread2_msg = future2.get();
        System.out.println(thread1_msg);
        System.out.println(thread2_msg);
        executor.shutdown();
        executor.awaitTermination(1, TimeUnit.SECONDS);
    }

    private static String parallel_task1() {
        return "thread1:Java is hot, aromatic, and invigorating. is dead.";
    }

    private static String parallel_task2() {
        return "thread2:Java is hot, aromatic, and";
    }
}
公共类Mult{
公共静态void main(字符串[]args)引发InterruptedException、ExecutionException{
最终int总螺纹=2;
ExecutorService executor=Executors.newFixedThreadPool(线程总数);
Future future1=执行者提交(Mult::parallel_task1);
Future future2=执行者提交(Mult::parallel_task2);
字符串thread1_msg=future1.get();
字符串thread2_msg=future2.get();
System.out.println(thread1_msg);
System.out.println(thread2_msg);
executor.shutdown();
执行器等待终止(1,时间单位秒);
}
专用静态字符串并行_task1(){
return“thread1:Java是热的、芳香的、有活力的。已经死了。”;
}
专用静态字符串并行_task2(){
return“thread2:Java是热的、芳香的和热的”;
}
}
我正在学习多线程,但同时我也不是很好 有Java方面的经验。从这个例子开始

在我看来,您不应该浪费太多时间来协调线程,以便它们能够很好地打印输出。在IMO中,并行更多的是关于加速应用程序,而不是协调输出。只按原样输出或只使用一个线程负责输出要容易得多。这种类型的教程以输出为例,因为它更方便用户使用,但根据我的经验,我从未协调过来自
System.out
的输出

有可能有这样的输出吗

thread1:Java is hot, aromatic, and invigorating. is dead.
thread2:Java is hot, aromatic, and
线程1:爪哇是热,芳香,和振兴。他死了

thread2:Java是热的,芳香的,并且

当然,您只需要确保每个线程一次性打印整个字符串

所以基本上thread1的输出总是链接在第一行 在端子中,螺纹2在第2行中

有几种方法可以实现这一点,例如,您可以使用:

允许一组线程全部等待的同步辅助工具 彼此到达一个共同的障碍点。骑自行车的人很有用 在涉及固定大小线程组的程序中,必须 偶尔互相等待。这种势垒被称为循环势垒,因为 释放等待的线程后,可以重新使用它

协调线程,使输出一个接一个地进行

以下是一个完整的示例:

public class Mult {
 
    public static void main(String[] args) {

        final int total_threads = 2;
        final CyclicBarrier barrier = new CyclicBarrier(total_threads);
        String msg1 = "thread1:Java is hot, aromatic, and invigorating. is dead.";
        String msg2 = "thread2:Java is hot, aromatic, and";
        Thread thread1 = new Thread(() -> parallel_task1(barrier, msg1));
        Thread thread2 = new Thread(() -> parallel_task2(barrier, msg2));
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
    }

    private static void parallel_task1(CyclicBarrier barrier, String msg1) {
        System.out.println(msg1);
        wait_for_barrier(barrier);
    }

    private static void parallel_task2(CyclicBarrier barrier, String msg1) {
        wait_for_barrier(barrier);
        System.out.println(msg1);
    }

    private static void wait_for_barrier(CyclicBarrier barrier) {
        try {
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
另一种方法是向用户报告

这种方法的一个例子是:

public class Mult {

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

        final int total_threads = 2;
        ExecutorService executor = Executors.newFixedThreadPool(total_threads);
        Future<String> future1 = executor.submit(Mult::parallel_task1);
        Future<String> future2 = executor.submit(Mult::parallel_task2);
        String thread1_msg = future1.get();
        String thread2_msg = future2.get();
        System.out.println(thread1_msg);
        System.out.println(thread2_msg);
        executor.shutdown();
        executor.awaitTermination(1, TimeUnit.SECONDS);
    }

    private static String parallel_task1() {
        return "thread1:Java is hot, aromatic, and invigorating. is dead.";
    }

    private static String parallel_task2() {
        return "thread2:Java is hot, aromatic, and";
    }
}
公共类Mult{
公共静态void main(字符串[]args)引发InterruptedException、ExecutionException{
最终int总螺纹=2;
ExecutorService executor=Executors.newFixedThreadPool(线程总数);
Future future1=执行者提交(Mult::parallel_task1);
Future future2=执行者提交(Mult::parallel_task2);
字符串thread1_msg=future1.get();
字符串thread2_msg=future2.get();
System.out.println(thread1_msg);
System.out.println(thread2_msg);
executor.shutdown();
执行器等待终止(1,时间单位秒);
}
专用静态字符串并行_task1(){
return“thread1:Java是热的、芳香的、有活力的。已经死了。”;
}
专用静态字符串并行_task2(){
return“thread2:Java是热的、芳香的和热的”;
}
}
我正在学习多线程,但同时我也不是很好 有Java方面的经验。从这个例子开始

在我看来,您不应该浪费太多时间来协调线程,以便它们能够很好地打印输出。在IMO中,并行更多的是关于加速应用程序,而不是协调输出。它是更容易要么只是输出,因为它是或只是使用一个thr