Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>class Quad implements Runnable{ //Shared Variables static volatile double [][] stack; static volatile boolean first=true; static Object lock1; static Object lock2; static double FinalResult; static AtomicInteger threadCounter; static AtomicInteger stackpointer; static int nthreads; //Constants static final int stacksize = 1000; static final int il = 0; static final int ir = 1; static final int ie = 2; static final int dims = 3; //Private Variables int tid; double left,right,eps;</p> <pre><code>private double result; private double l,r,ep; private boolean calculate; public Quad(double left, double right, double eps,int tid,int nthreads) { this.left = left; this.right = right; this.eps = eps; this.tid = tid; Quad.nthreads = nthreads; result = 0.0; synchronized(this){ //Only the first thread will do it if(first==true){ first=false; lock1 = new Object(); lock2 = new Object(); stack = new double [dims][stacksize]; threadCounter= new AtomicInteger(0); stackpointer = new AtomicInteger(1); stack[il][stackpointer.get()] = left; stack[ir][stackpointer.get()] = right; stack[ie][stackpointer.get()] = eps; FinalResult=0.0; System.out.println("I am tid= "+tid ); } } } public void run(){ stackops(); add(); } public void stackops() { double abserror,m, est1, est2; est2=est1=m=abserror=0; while ((stackpointer.get() &gt;= 1)|| threadCounter.get()&gt;0) { // Pop next interval off stack. synchronized (lock1){ pop(); } // Compute estimates. if (calculate == true){ m = 0.5 * (l + r); est1 = 0.5 * (r - l) * (func(l) + func(r)) ; est2 = 0.5 * ((m - l) * (func(l) + func(m)) + (r - m) * (func(m) + func(r))); abserror = Math.abs(est2-est1) / 3.0; if (abserror &lt;= ep) { result += est2; } else { //Push into the stack synchronized (lock1){ push(m); } }//end else threadCounter.decrementAndGet(); } }//end while System.out.println("I am " + tid+" result = "+result); }//end method private void add(){ synchronized(lock1){ FinalResult +=result; } } private void pop(){ if(stackpointer.get()&gt;0){ threadCounter.incrementAndGet(); calculate =true; l = stack[il][stackpointer.get()]; r = stack[ir][stackpointer.get()]; ep = stack[ie][stackpointer.get()]; stackpointer.decrementAndGet(); }else{ calculate =false; } } private void push (double m){ if(stackpointer.get()&gt;=-1){ stackpointer.incrementAndGet(); stack[il][stackpointer.get()] = l; stack[ir][stackpointer.get()] = m; stack[ie][stackpointer.get()] = ep * 0.5; stackpointer.incrementAndGet(); stack[il][stackpointer.get()] = m; stack[ir][stackpointer.get()] = r; stack[ie][stackpointer.get()] = ep * 0.5; } } public double getResult(){ return FinalResult; } private double func(double x) { double q; int n; n = 10000; q = 1000.0; for(int i=0;i&lt;n;i++) { q -= x; } if (q == 1.0e10) System.out.println("q = " + q); return x * x; } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload