Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab1;
- import java.util.ArrayList;
- import java.util.concurrent.*;
- class Licznik implements Callable<Integer>{
- String name;
- int value = 0;
- public Licznik(String name, int value) {
- this.name = name;
- this.value = value;
- }
- public String toString() {
- return "Licznik (name: " + name + ")";
- }
- // licznik zlicza od 0 do 4
- public Integer call(){
- Integer x = 0;
- for(int i = 0; i < 4; i++) {
- if(Thread.currentThread().isInterrupted()) {
- System.out.println(this + " interrupted");
- }
- x += 1;
- System.out.println(this + " " + x);
- }
- return x;
- }
- }
- class FLicznik<T> extends FutureTask<T>{
- String name;
- public FLicznik(Callable<T> c) {
- super(c);
- name = "" + c;
- }
- // metoda done, czyli jak zadanie sie skonczy to sie wywoła
- protected void done() {
- T result = null;
- try {
- if(Thread.currentThread().isInterrupted()){
- System.out.println(this + " PRZERWANY!");
- return;
- }
- if(isCancelled()){
- System.out.println(this + " ANULOWANY!");
- return;
- }
- result = this.get();
- System.out.println("Zad " + this + " WYKONANE | Wynik: " + result);
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- }
- // czyli to sie wypisze za kazdym razem zamiast 'this'
- public String toString() {
- return "FLicznik (name: " + name + ")";
- }
- }
- public class LicznikTest {
- public static void main(String[] args) {
- ArrayList<Licznik> liczniki = new ArrayList<Licznik>();
- ArrayList<Future<Integer>> licznikiFuture = new ArrayList<Future<Integer>>();
- ExecutorService exec = Executors.newFixedThreadPool(2);
- // tworzenie 10 licznikow
- for(int i = 0; i < 10; i++) {
- liczniki.add(new Licznik("" + i, 0));
- }
- // wywołanie metody submit z argumentem callable, zwraca nam obiekt Future
- for(Licznik l : liczniki) {
- licznikiFuture.add(exec.submit(l));
- }
- // zamykamy żeby nie przyjmować nowych zadań do wykonania
- exec.shutdown();
- try {
- // czekamy dwie sekundy, albo do momentu skończenia
- exec.awaitTermination(2, TimeUnit.SECONDS);
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- // przechodząc przez liste z Future'ami sprawdzamy stan zadań
- for(Future<Integer> f: licznikiFuture) {
- String msg = "";
- try {
- if(f.isCancelled())
- msg += "zad " + f + " ANULOWANE";
- else if(f.isDone())
- msg += "zad" + f + " WYKONANE | Wynik: " + f.get();
- else
- msg += "zad " + f + " CZEKAMY";
- }
- catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
- System.out.println(msg);
- }
- // i wypisyjemy czy zakończone
- System.out.println("ExecutorService isTerminated: " + exec.isTerminated());
- System.out.println("=====================================================");
- System.out.println("=====================================================");
- System.out.println("=====================================================");
- System.out.println("=====================================================");
- System.out.println("=====================================================");
- // lista na obiekty klasy FLicznik
- ArrayList<FLicznik<Integer>> fliczniki = new ArrayList<FLicznik<Integer>>();
- ExecutorService exec2 = Executors.newFixedThreadPool(2);
- // tworze 10 FLiczników które do konstruktowa przyjmują obiekt Licznika
- // jako Callable
- for (int i = 0; i < 10; i++) {
- fliczniki.add(new FLicznik<Integer>(new Licznik("Licznik_" + i, 0)));
- }
- // tym sposobem nie puszczam submita, tylko robie execute'a
- for (FLicznik<Integer> f : fliczniki) {
- exec2.execute(f);
- }
- exec2.shutdown();
- try {
- exec2.awaitTermination(10, TimeUnit.SECONDS);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- // i wypisyjemy czy zakończone
- System.out.println("ExecutorService isTerminated: " + exec2.isTerminated());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement