albertnetymk
6/17/2015 - 9:36 PM

block.java

import java.util.*;
import java.util.concurrent.*;

public class block {
    static ForkJoinPool pool;
    static Server s;
    static Client c1, c2;
    static volatile int x = 0;
    static class Client extends RecursiveAction {
        @Override
        protected void compute() {
            if (this == c1) {
                s.fork();
                pool.execute(c2);
                s.join();
                pool.shutdown();
            } else {
                System.out.println("nonblock");
            }
        }
    }

    static class Server extends RecursiveAction {
        // copied from https://it.wikibooks.org/wiki/Implementazioni_di_algoritmi/Bubble_sort#Java
        void bubbleSort(int[] x) {
            int temp = 0;
            int j = x.length-1;
            while(j>0)
            {
                for(int i=0; i<j; i++)
                {
                    if(x[i]>x[i+1])
                    {
                        temp=x[i];
                        x[i]=x[i+1];
                        x[i+1]=temp;
                    }
                }
                j--;
            }
        }

        void heavy() {
            int length = 1*100*1000;
            // int length = 10;
            int[] arr = new int[length];
            for (int i = 0; i < length; ++i) {
                arr[0] = i+1;
            }
            bubbleSort(arr);
        }


        @Override
        protected void compute() {
            heavy();
            System.out.println("heavy");
        }
    }

    public static void main(String[] args) {
        pool = new ForkJoinPool(1);

        s = new Server();
        c1 = new Client();
        c2 = new Client();
        pool.execute(c1);
        try {
            while(!pool.isTerminated()) {
                pool.awaitTermination(5, TimeUnit.SECONDS);
            }
        } catch (Exception e) {
            System.out.println("exception");
        }
    }
}