Commits

Anonymous committed a225255

add future examples

  • Participants
  • Parent commits 86c067b

Comments (0)

Files changed (6)

File src/concurrent/FutureTaskTest.java

 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 
 /**
  *
     public static void main(String[] args) throws Exception {
         ExecutorService exec = Executors.newCachedThreadPool();
         Future<String> image = exec.submit(new GetImageTask());
-        System.out.println(image.get());
+        System.out.println(image.get(5, TimeUnit.SECONDS));
         System.out.println("done!");
         System.out.println(image.get());
         exec.shutdown();

File src/concurrent/future/App.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package concurrent.future;
+
+import concurrent.future.ArchiveSearcher;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+/**
+ *
+ * @author sting
+ */
+public class App {
+    
+    ExecutorService exec = Executors.newCachedThreadPool();
+    final ArchiveSearcher searcher = new MySearcher();
+    
+    public void showSearch(final String target) {
+        Future<String> f = exec.submit(new Callable<String>() {
+            @Override
+            public String call() throws Exception {
+                return searcher.search(target);
+            }
+            
+        });
+        
+        showOthers();
+        try {
+            System.out.println(f.get());
+        } catch (InterruptedException ex) {
+        } catch (ExecutionException ex) {
+        }
+    }
+    
+    private void showOthers() {
+        System.out.println("Show others!");
+    }
+    
+    public static void main(String[] args) {
+        App app = new App();
+        app.showSearch("target");
+    }
+}
+
+class MySearcher implements ArchiveSearcher {
+
+    @Override
+    public String search(String target) {
+        try {
+            Thread.sleep(1000);
+        } catch (InterruptedException e) {}
+        return "result";
+    }
+    
+}

File src/concurrent/future/AppUsingQuery.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package concurrent.future;
+
+
+
+/**
+ *
+ * @author sting
+ */
+public class AppUsingQuery {
+    private final MySearcher s = new MySearcher();
+    public void search(final String target) {
+        
+        //System.out.println(s.search(target));//I have to wait
+        SearchThread t = new SearchThread(s, target);
+        new Thread(t).start();
+        showOthers();
+        while (t.result == null) {
+            
+        }
+        System.out.println(t.result);
+    }
+    
+    public void showOthers() {
+        System.out.println("others");
+    }
+    
+    public static void main(String[] args) {
+        AppUsingQuery a = new AppUsingQuery();
+        a.search("target");
+    }
+    
+    static class SearchThread implements Runnable {
+        String result;
+        private ArchiveSearcher s;
+        private String target;
+        
+        public SearchThread(ArchiveSearcher s, String target) {
+            this.s = s;
+            this.target = target;
+        }
+        
+        @Override
+        public void run() {
+            result = s.search(target);
+        }
+        
+    }
+}

File src/concurrent/future/AppUsingWait.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package concurrent.future;
+
+/**
+ *
+ * @author sting
+ */
+public class AppUsingWait {
+    public void showSearch(final String target) {
+        final SearcherUsingWait s = new SearcherUsingWait();
+        new Thread() {
+            @Override
+            public void run() {
+                MySearcher my = new MySearcher();
+                s.setResult(my.search(target));
+            }
+        }.start();
+        showOther();
+        System.out.println(s.search(target));
+    }
+    
+    public void showOther() {
+        System.out.println("Show others!!!");
+    }
+    
+    public static void main(String[] args) {
+        AppUsingWait a = new AppUsingWait();
+        a.showSearch("yes");
+    }
+}
+
+class SearcherUsingWait implements ArchiveSearcher {
+    private String result = null;
+    private boolean ready = false;
+    @Override
+    public synchronized String search(String target) {
+        while (!ready) {
+            try {
+                wait();
+            } catch (InterruptedException e) {}
+            
+        }
+        return result;
+    }
+    
+    public synchronized void setResult(String r) {
+        result = r;
+        ready = true;
+        notifyAll();
+    }
+    
+}

File src/concurrent/future/ArchiveSearcher.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package concurrent.future;
+
+/**
+ *
+ * @author sting
+ */
+public interface ArchiveSearcher {
+    String search(String target);
+}

File src/concurrent/future/FutureTest.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package concurrent.future;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.FutureTask;
+
+/**
+ *
+ * @author sting
+ */
+public class FutureTest {
+    static class MyFuture implements Callable<String> {
+
+        @Override
+        public String call() throws Exception {
+            return "yes";
+        }
+        
+    } 
+    
+    public static void main(String[] args) throws ExecutionException, InterruptedException {
+        ExecutorService exec = Executors.newCachedThreadPool();
+        Future<String> result = exec.submit(new MyFuture());
+        System.out.println(result.getClass().getName());
+        System.out.println(result.get());
+        exec.shutdown();
+//        FutureTask<String> f = new FutureTask<String>(new MyFuture());
+//        f.run();
+//        System.out.println(f.get());
+    }
+}