2015-05-26 154 views
0
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。Executor System.out.println未在控制檯中顯示

我將在我的調試器中發佈一個屏幕截圖,顯示布爾值的值爲true。 enter image description here

感謝您的任何反應....

+0

所以你看到的唯一的輸出是'第一'然後'5008設置烤麪包沒有shewn'? –

+0

正確。 println有可能存在線程問題嗎? – Giuseppe

+0

我做了一個可以粘貼到IntelliJ中的複製粘貼示例。我目前在優勝美地環境中。我認爲JDK版本已發佈。 – Giuseppe

回答

2

您不讓Java線程系統有機會在您的線程之間共享CPU。

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

在這個位置的睡眠似乎是一個關鍵的補充。沒有時間的睡眠,它是行不通的。 – Giuseppe

+0

排序是我必須解決的問題,如果我無法弄清楚,我可能不得不發表另外一個問題。非常感謝你們這些非常有啓發性的職位。 – Giuseppe

1

你有多個線程讀取,沒有任何保護的併發寫入isToastShewnLeft變量。嘗試在isToastShewnLeft的​​上附上所有讀/寫操作,看看這是否有所作爲。這裏的問題可能是線程相關的可見性問題:線程保留自己的變量副本和重新排序操作以提高效率的細微情況。同步是阻止任何共享變量的一種方法。 Here is one good explanation of visibilityhere is another

除了​​,你可以使用java.util.concurrent.AtomicBoolean,如果假設你真的關心性能(我懷疑給定了5秒的間隔)。不管你是否可以將isToastShewnLeft變成volatile變量,但是我認爲isToastShewnLeft的狀態會影響所寫的內容,我都會迴避這一點。

+0

通常我想我明白你在說什麼,我需要閱讀這些文章。現在我正在考慮的部分是,現在這個敬酒正在適當的時間間隔內展示,但它們並沒有相繼出現。訂單已更改。 – Giuseppe

相關問題