qwerty787788

Facebook parallel template

Nov 2nd, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5.  
  6. public class Test {
  7.     FastScanner in;
  8.     PrintWriter out;
  9.  
  10.     class Solver implements Runnable {
  11.         int test;
  12.         Random rnd = new Random();
  13.  
  14.         Solver(int test) {
  15.             this.test = test;
  16.         }
  17.  
  18.         Solver readInput() {
  19.             x = in.nextInt();
  20.             y = in.nextInt();
  21.             System.err.println("TEST " + test + ", read input");
  22.             return this;
  23.         }
  24.  
  25.         int x, y;
  26.         int sum;
  27.  
  28.         @Override
  29.         public void run() {
  30.             System.err.println("TEST " + test + ", start computing");
  31.             sum = x + y;
  32.             try {
  33.                 Thread.sleep(1000 + rnd.nextInt(1000));
  34.             } catch (InterruptedException e) {
  35.                 e.printStackTrace();
  36.             }
  37.             System.err.println("TEST " + test + ", stop computing");
  38.         }
  39.     }
  40.  
  41.     void solve() {
  42.         ExecutorService executor = Executors.newFixedThreadPool(5);
  43.  
  44.         int tc = in.nextInt();
  45.         Solver[] workers = new Solver[tc];
  46.         for (int t = 0; t < tc; t++) {
  47.             workers[t] = new Solver(t).readInput();
  48.             executor.execute(workers[t]);
  49.         }
  50.         executor.shutdown();
  51.         while (!executor.isTerminated()) {
  52.             try {
  53.                 Thread.sleep(100);
  54.             } catch (InterruptedException e) {
  55.                 e.printStackTrace();
  56.             }
  57.         }
  58.         for (int t = 0; t < tc; t++) {
  59.             System.err.println("Case #" + (t + 1) + ": " + workers[t].sum);
  60.         }
  61.  
  62.     }
  63.  
  64.     void run() {
  65.         try {
  66.             in = new FastScanner(new File("Test.in"));
  67.             out = new PrintWriter(new File("Test.out"));
  68.  
  69.             solve();
  70.  
  71.             out.close();
  72.         } catch (FileNotFoundException e) {
  73.             e.printStackTrace();
  74.         }
  75.     }
  76.  
  77.     void runIO() {
  78.  
  79.         in = new FastScanner(System.in);
  80.         out = new PrintWriter(System.out);
  81.  
  82.         solve();
  83.  
  84.         out.close();
  85.     }
  86.  
  87.     class FastScanner {
  88.         BufferedReader br;
  89.         StringTokenizer st;
  90.  
  91.         public FastScanner(File f) {
  92.             try {
  93.                 br = new BufferedReader(new FileReader(f));
  94.             } catch (FileNotFoundException e) {
  95.                 e.printStackTrace();
  96.             }
  97.         }
  98.  
  99.         public FastScanner(InputStream f) {
  100.             br = new BufferedReader(new InputStreamReader(f));
  101.         }
  102.  
  103.         String next() {
  104.             while (st == null || !st.hasMoreTokens()) {
  105.                 String s = null;
  106.                 try {
  107.                     s = br.readLine();
  108.                 } catch (IOException e) {
  109.                     e.printStackTrace();
  110.                 }
  111.                 if (s == null)
  112.                     return null;
  113.                 st = new StringTokenizer(s);
  114.             }
  115.             return st.nextToken();
  116.         }
  117.  
  118.         boolean hasMoreTokens() {
  119.             while (st == null || !st.hasMoreTokens()) {
  120.                 String s = null;
  121.                 try {
  122.                     s = br.readLine();
  123.                 } catch (IOException e) {
  124.                     e.printStackTrace();
  125.                 }
  126.                 if (s == null)
  127.                     return false;
  128.                 st = new StringTokenizer(s);
  129.             }
  130.             return true;
  131.         }
  132.  
  133.         int nextInt() {
  134.             return Integer.parseInt(next());
  135.         }
  136.  
  137.         long nextLong() {
  138.             return Long.parseLong(next());
  139.         }
  140.  
  141.         double nextDouble() {
  142.             return Double.parseDouble(next());
  143.         }
  144.     }
  145.  
  146.     public static void main(String[] args) {
  147.         new Test().run();
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment