Job: introduced "Job" and "Overseer" for small job
authorSeokYeon Hwang <syeon.hwang@samsung.com>
Thu, 11 Jun 2015 07:43:55 +0000 (16:43 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Fri, 12 Jun 2015 08:29:52 +0000 (17:29 +0900)
Jobs can be run concurrently.

Change-Id: I1eef528cfffe517ab5f9e93747a16ebc14913a0f
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
src/org/tizen/emulator/manager/job/Job.java [new file with mode: 0644]
src/org/tizen/emulator/manager/job/Overseer.java [new file with mode: 0644]

diff --git a/src/org/tizen/emulator/manager/job/Job.java b/src/org/tizen/emulator/manager/job/Job.java
new file mode 100644 (file)
index 0000000..545445d
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Emulator Manager
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ * JiHye Kim <jihye424.kim@samsung.com>
+ * MinKee Lee <minkee.lee@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.emulator.manager.job;
+
+interface Job {
+       public void work();
+}
diff --git a/src/org/tizen/emulator/manager/job/Overseer.java b/src/org/tizen/emulator/manager/job/Overseer.java
new file mode 100644 (file)
index 0000000..111efee
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * Emulator Manager
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ * JiHye Kim <jihye424.kim@samsung.com>
+ * MinKee Lee <minkee.lee@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.emulator.manager.job;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.logging.Level;
+
+import org.tizen.emulator.manager.logging.EMLogger;
+
+public class Overseer {
+       private final List<Class<? extends Job>> jobClasses = new ArrayList<Class<? extends Job>>();
+       private Thread overseerThread;
+       private CountDownLatch doneSignal;
+
+       private Overseer() {};
+
+       protected void startWorkInternal() throws InterruptedException {
+               doneSignal = new CountDownLatch(jobClasses.size());
+
+               for (Class<? extends Job> jobCls : jobClasses) {
+                       new Thread(new JobRunnable(doneSignal, jobCls)).start();
+               }
+
+               doneSignal.await();
+       }
+
+       public void registerJob(Class<? extends Job> jobCls) {
+               jobClasses.add(jobCls);
+       }
+
+       @SafeVarargs
+       public static Overseer newOverseer(Class<? extends Job>... jobClasses) {
+               Overseer overseer = new Overseer();
+
+               for (Class<? extends Job> jobCls: jobClasses) {
+                       overseer.registerJob(jobCls);
+               }
+
+               return overseer;
+       }
+
+       public void startWork() throws InterruptedException {
+               EMLogger.getLogger().log(Level.INFO, "is starting...");
+               if (overseerThread != null) {
+                       // already started
+                       assert false : "Already started !!!"; // for debugging...
+
+                       return;
+               }
+
+               overseerThread = new Thread(new Runnable() {
+                       @Override
+                       public void run() {
+                               try {
+                                       Overseer.this.startWorkInternal();
+                               } catch (InterruptedException e) {
+                                       e.printStackTrace();
+                               }
+                       }
+               });
+
+               overseerThread.start();
+       }
+
+       public void join() throws InterruptedException {
+               if (overseerThread == null) {
+                       return;
+               }
+
+               if (overseerThread.isAlive()) {
+                       overseerThread.join();
+
+                       return;
+               }
+       }
+}
+
+class JobRunnable implements Runnable {
+       private final CountDownLatch doneSignal;
+       private final Class<? extends Job> jobCls;
+
+       JobRunnable(CountDownLatch doneSignal, Class<? extends Job> jobCls) {
+               this.doneSignal = doneSignal;
+               this.jobCls = jobCls;
+       }
+
+       @Override
+       public void run() {
+               Job job = null;
+               try {
+                       job = jobCls.newInstance();
+                       job.work();
+                       doneSignal.countDown();
+               } catch (InstantiationException | IllegalAccessException e) {
+                       e.printStackTrace();
+               }
+       }
+}