Advertisement
majczel23000

[PWJJ] Create Threads and check if terminated

Nov 13th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. package lab1;
  2. import java.util.ArrayList;
  3. import java.util.concurrent.*;
  4.  
  5. class Licznik implements Callable<Integer>{
  6.     String name;
  7.     int value = 0;
  8.    
  9.     public Licznik(String name, int value) {
  10.         this.name = name;
  11.         this.value = value;
  12.     }
  13.    
  14.     public String toString() {
  15.         return "Licznik (name: " + name + ")";
  16.     }
  17.    
  18.     // licznik zlicza od 0 do 4
  19.     public Integer call(){
  20.         Integer x = 0;
  21.         for(int i = 0; i < 4; i++) {
  22.             if(Thread.currentThread().isInterrupted()) {
  23.                 System.out.println(this + " interrupted");
  24.             }
  25.             x += 1;
  26.             System.out.println(this + " " + x);
  27.         }
  28.         return x;
  29.     }
  30. }
  31.  
  32. class FLicznik<T> extends FutureTask<T>{
  33.     String name;
  34.     public FLicznik(Callable<T> c) {
  35.         super(c);
  36.         name = "" + c;
  37.     }
  38.    
  39.     // metoda done, czyli jak zadanie sie skonczy to sie wywoła
  40.     protected void done() {
  41.         T result = null;
  42.         try {
  43.             if(Thread.currentThread().isInterrupted()){
  44.                 System.out.println(this + " PRZERWANY!");
  45.                 return;
  46.             }
  47.             if(isCancelled()){
  48.                 System.out.println(this + " ANULOWANY!");
  49.                 return;
  50.             }
  51.             result = this.get();
  52.             System.out.println("Zad " + this + " WYKONANE | Wynik: " + result);
  53.         } catch (InterruptedException | ExecutionException e) {
  54.             e.printStackTrace();
  55.         }
  56.     }
  57.    
  58.     // czyli to sie wypisze za kazdym razem zamiast 'this'
  59.     public String toString() {
  60.         return "FLicznik (name: " + name + ")";
  61.     }
  62. }
  63.  
  64. public class LicznikTest {
  65.  
  66.     public static void main(String[] args) {
  67.         ArrayList<Licznik> liczniki = new ArrayList<Licznik>();
  68.         ArrayList<Future<Integer>> licznikiFuture = new ArrayList<Future<Integer>>();
  69.        
  70.         ExecutorService exec = Executors.newFixedThreadPool(2);
  71.        
  72.         // tworzenie 10 licznikow
  73.         for(int i = 0; i < 10; i++) {
  74.             liczniki.add(new Licznik("" + i, 0));
  75.         }
  76.  
  77.         // wywołanie metody submit z argumentem callable, zwraca nam obiekt Future
  78.         for(Licznik l : liczniki) {
  79.             licznikiFuture.add(exec.submit(l));
  80.         }
  81.        
  82.         // zamykamy żeby nie przyjmować nowych zadań do wykonania
  83.         exec.shutdown();
  84.         try {
  85.             // czekamy dwie sekundy, albo do momentu skończenia
  86.             exec.awaitTermination(2, TimeUnit.SECONDS);
  87.         } catch(InterruptedException e) {
  88.             e.printStackTrace();
  89.         }
  90.        
  91.         // przechodząc przez liste z Future'ami sprawdzamy stan zadań
  92.         for(Future<Integer> f: licznikiFuture) {
  93.             String msg = "";
  94.              try {
  95.                 if(f.isCancelled())
  96.                     msg += "zad " + f + " ANULOWANE";
  97.                 else if(f.isDone())
  98.                     msg += "zad" + f + " WYKONANE | Wynik: " + f.get();
  99.                 else
  100.                     msg += "zad " + f + " CZEKAMY";
  101.              }
  102.             catch (InterruptedException | ExecutionException e) {
  103.                 e.printStackTrace();
  104.             }
  105.             System.out.println(msg);
  106.         }
  107.        
  108.         // i wypisyjemy czy zakończone
  109.         System.out.println("ExecutorService isTerminated: " + exec.isTerminated());
  110.         System.out.println("=====================================================");
  111.         System.out.println("=====================================================");
  112.         System.out.println("=====================================================");
  113.         System.out.println("=====================================================");
  114.         System.out.println("=====================================================");
  115.        
  116.        
  117.         // lista na obiekty klasy FLicznik
  118.         ArrayList<FLicznik<Integer>> fliczniki = new ArrayList<FLicznik<Integer>>();
  119.        
  120.         ExecutorService exec2 = Executors.newFixedThreadPool(2);
  121.    
  122.         // tworze 10 FLiczników które do konstruktowa przyjmują obiekt Licznika
  123.         // jako Callable
  124.         for (int i = 0; i < 10; i++) {
  125.             fliczniki.add(new FLicznik<Integer>(new Licznik("Licznik_" + i, 0)));
  126.         }
  127.        
  128.         // tym sposobem nie puszczam submita, tylko robie execute'a
  129.         for (FLicznik<Integer> f : fliczniki) {
  130.             exec2.execute(f);
  131.         }
  132.        
  133.         exec2.shutdown();
  134.         try {
  135.             exec2.awaitTermination(10, TimeUnit.SECONDS);
  136.         } catch (InterruptedException e) {
  137.             e.printStackTrace();
  138.         }
  139.        
  140.         // i wypisyjemy czy zakończone
  141.         System.out.println("ExecutorService isTerminated: " + exec2.isTerminated());
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement