Last active
August 29, 2015 14:23
-
-
Save albertnetymk/a442551cb26de10556ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment