简体   繁体   English

Executor System.out.println 未显示在控制台中

[英]Executor System.out.println not showing in console

package com.company;

import java.io.Console;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Main {

    static boolean isToastShewnLeft = false;

    public static void main(String[] args) {

        showToastLeft("first", "", "");
        showToastLeft("second", "", "");
        showToastLeft("third", "", "");
        showToastLeft("fourth", "", "");
        showToastLeft("fifth", "", "");
    }

    public static void showToastLeft(final String paramToastString1, final String paramToastString2, final String paramToastString3) {

        final long start = System.currentTimeMillis();

        ScheduledExecutorService worker1 = Executors.newSingleThreadScheduledExecutor();

        ScheduledExecutorService worker2 = Executors.newSingleThreadScheduledExecutor();

        if (isToastShewnLeft) {

            final Runnable r = new Runnable() {
                @SuppressWarnings("StatementWithEmptyBody")
                public void run() {
                    while (isToastShewnLeft) {

                    }

                    isToastShewnLeft = true;

                    //This line does not appear in console output
                    //I can set a breakpoint on this println and see that it does execute
                    System.out.println(paramToastString1);

                    ScheduledExecutorService worker3 = Executors.newSingleThreadScheduledExecutor();

                    final Runnable r2 = new Runnable() {
                        public void run() {
                            isToastShewnLeft = false;

                            System.out.println(System.currentTimeMillis() - start + " Set toast not shewn");
                        }
                    };

                    worker3.schedule(r2, 5, TimeUnit.SECONDS);

                }
            };

            worker1.schedule(r, 0, TimeUnit.SECONDS);

        } else {
            isToastShewnLeft = true;

            System.out.println(paramToastString1);

            final Runnable r2 = new Runnable() {
                public void run() {
                    isToastShewnLeft = false;
                    System.out.println(System.currentTimeMillis() - start + " Set toast not shewn");
                }
            };

            worker2.schedule(r2, 5, TimeUnit.SECONDS);

        }
    }
}
            /Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/bin/java -Didea.launcher.port=7543 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 14 CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/lib/javafx-doclet.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Users/ericsepich/IdeaProjects/untitled1/out/production/untitled1:/Applications/IntelliJ IDEA 14 CE.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain com.company.Main
            first
            5008 Set toast not shewn

My intent is to show the texts 5 seconds apart at each call to showToastLeft.我的目的是在每次调用 showToastLeft 时显示间隔 5 秒的文本。 Is it possible that there is a problem using println from the executors thread?从执行程序线程使用 println 是否可能存在问题? I am using IntelliJ idea as my IDE.我使用 IntelliJ idea 作为我的 IDE。

I will be posting a screenshot showing the value of the boolean to be true in my debugger.我将发布一个屏幕截图,显示我的调试器中布尔值的值为真。在此处输入图片说明

Thank you for any responses....感谢您的任何回应......

You are not letting the Java threading system a chance to share the CPU between your threads.您不会让 Java 线程系统有机会在您的线程之间共享 CPU。

                while (isToastShewnLeft) {
                  // Add this.
                  Thread.sleep(10);
                }

You have multiple threads reading and writing the isToastShewnLeft variable without any concurrency protection.您有多个线程在没有任何并发​​保护的情况下读取和写入isToastShewnLeft变量。 Try enclosing all read/write operations on isToastShewnLeft in synchronized and see if this makes a difference.尝试将isToastShewnLeft上的所有读/写操作isToastShewnLeftsynchronized ,看看这是否有所不同。 The issue here may be a thread-related visibility problem: the subtle situation where threads keep their own copies of variables and reorder operations for efficiency.这里的问题可能是与线程相关的可见性问题:线程保留自己的变量副本并重新排序操作以提高效率的微妙情况。 Synchronization is one way of preventing this with any shared variables.同步是防止任何共享变量发生这种情况的一种方法。 Here is one good explanation of visibility , and here is another . 这是对可见性的一个很好的解释这是另一个

Besides synchronized , you could use a java.util.concurrent.AtomicBoolean , if let's say you were really concerned about performance (which I doubt given the 5 seconds intervals).除了synchronized ,您还可以使用java.util.concurrent.AtomicBoolean ,如果假设您真的很关心性能(考虑到 5 秒的间隔,我对此表示怀疑)。 It's a bit more subjective whether you could get away with just making isToastShewnLeft into a volatile variable, but I would shy away from that given that the state of isToastShewnLeft affects what is written to it.您是否可以通过将isToastShewnLeft变成一个volatile变量来逃避,这有点主观,但鉴于isToastShewnLeft的状态会影响写入它的内容,我会回避这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM