Created
May 22, 2014 09:10
-
-
Save benbai123/74a1ff91b829b5d84e21 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.ScheduledExecutorService; | |
| import com.google.common.util.concurrent.ListeningScheduledExecutorService; | |
| import com.google.common.util.concurrent.MoreExecutors; | |
| public class Test { | |
| private static ListeningScheduledExecutorService mListeningScheduledExecutorService = null; | |
| private static ScheduledExecutorService mScheduledExecutorService = null; | |
| private static List<doWork> works = new ArrayList<doWork>(); | |
| private static boolean isTurnOn = false; | |
| public static class doWork implements Runnable { | |
| private String name = null; | |
| public doWork(String name) { | |
| this.name = name; | |
| } | |
| public void run() { | |
| // 一定會打開電源後才開工 | |
| System.out.println(name + " 完工!"); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| mScheduledExecutorService = Executors.newScheduledThreadPool(5, | |
| Executors.defaultThreadFactory()); | |
| mListeningScheduledExecutorService = MoreExecutors | |
| .listeningDecorator(mScheduledExecutorService); | |
| for (int i = 1; i <= 9; i++) { | |
| works.add(new doWork("任務" + i)); | |
| } | |
| if (isTurnOn) { | |
| startWork(); | |
| } else { | |
| // 打開電源的工作 | |
| mListeningScheduledExecutorService.submit(new Runnable() { | |
| public void run() { | |
| isTurnOn = true; | |
| System.out.println("開電"); | |
| startWork(); | |
| } | |
| }); | |
| } | |
| } | |
| private static void startWork () { | |
| for (doWork dw : works) { | |
| mListeningScheduledExecutorService.submit(dw); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment