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

时间:2015-05-26 12:11:53

标签: java scheduledexecutorservice

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

我的目的是在每次调用showToastLeft时间隔5秒显示文本。是否有可能使用println存在问题 执行者线程?我使用IntelliJ的想法作为我的IDE。

我将在调试器中发布一个屏幕截图,显示布尔值为true。 enter image description here

感谢您的回复......

2 个答案:

答案 0 :(得分:2)

您不会让Java线程系统有机会在您的线程之间共享CPU。

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

答案 1 :(得分:1)

您有多个线程在没有任何并发​​保护的情况下读取和写入isToastShewnLeft变量。尝试将所有读/写操作封装在isToastShewnLeft的{​​{1}}上,看看这是否有所不同。这里的问题可能是与线程相关的可见性问题:线程保留自己的变量副本并重新排序操作以提高效率的微妙情况。同步是使用任何共享变量来防止这种情况的一种方法。 Here is one good explanation of visibilityhere is another

除了synchronized之外,你可以使用java.util.concurrent.AtomicBoolean,如果让我说你真的关心性能(我怀疑给出了5秒的间隔)。你是否可以将synchronized变成isToastShewnLeft变量而更加主观,但我会回避那些因为volatile的状态会影响写入它的内容