.


:




:

































 

 

 

 


i




i , i i i "i-" .

i , i:

:

s = (x2 - x1) * (y2 - y1)

:

s = (x2 - x1) * (y2 - y1) = (x2 * y2 - x2 * y1) + (x1 * y1 - x1 * y2)

"i-"

i , i i, i .

i , i i ii.

i ii i i i .

ii Calculator, i i i.

, i, i i i Runnable.

i i, i i opSum, opSub, opMult opDiv.

' Calculator ? cop.

i i, ii ii i ii i data[] RectangleSquareCalc.

i .

Calculator i i ii i i.

:

  • op1 - i ;
  • op2 - i ;
  • res - i i.

, i Calculator i' name i '- RectangleSquareCalc.

i i Calculator i i, i , i i.

import java.util.concurrent.*; class Calculator implements Runnable { public final static int opSum = 1; public final static int opSub = 2; public final static int opMult = 3; public final static int opDiv = 4; int codeOp; int operator1; int operator2; int result; String threadName; RectangleSquareCalc manager; Calculator(String name, int cop, int op1, int op2, int res, RectangleSquareCalc rsc) { threadName = name; codeOp = cop; operator1 = op1; operator2 = op2; result = res; manager = rsc; System.out.println(threadName + " - Created"); } public void run() { System.out.println(threadName + " - Start of Work"); while (!manager.stopFlag) { if (manager.flags[operator1] && manager.flags[operator2]) { switch(codeOp) { case opSum: manager.data[result] = manager.data[operator1] + manager.data[operator2]; System.out.println(threadName + ": Sum: " + operator1 + " + " + operator2 + " ==> " + result); break; case opSub: manager.data[result] = manager.data[operator1] - manager.data[operator2]; System.out.println(threadName + ": Sub: " + operator1 + " + " + operator2 + " ==> " + result); break; case opMult: manager.data[result] = manager.data[operator1] * manager.data[operator2]; System.out.println(threadName + ": Mult: " + operator1 + " + " + operator2 + " ==> " + result); break; case opDiv: manager.data[result] = manager.data[operator1] / manager.data[operator2]; System.out.println(threadName + ": Div: " + operator1 + " + " + operator2 + " ==> " + result); break; default: break; } manager.flags[operator1] = false; manager.flags[operator2] = false; manager.flags[result] = true; } Thread.yield(); } } }

i "i-" RectangleSquareCalc.

public class RectangleSquareCalc { double data[]; boolean flags[]; boolean stopFlag = false; RectangleSquareCalc(double x1, double y1, double x2, double y2) { data = new double[15]; flags = new boolean[15]; int pos = 0; flags[pos] = true; data[pos++] = x2; flags[pos] = true; data[pos++] = y2; flags[pos] = false; data[pos++] = 0.0; flags[pos] = true; data[pos++] = x2; flags[pos] = true; data[pos++] = y1; flags[pos] = false; data[pos++] = 0.0; flags[pos] = true; data[pos++] = x1; flags[pos] = true; data[pos++] = y2; flags[pos] = false; data[pos++] = 0.0; flags[pos] = true; data[pos++] = x1; flags[pos] = true; data[pos++] = y1; flags[pos] = false; data[pos++] = 0.0; } public static void main(String argc[]) { System.out.println("Main process started"); RectangleSquareCalc rsc = new RectangleSquareCalc(1.0, 2.0, 3.0, 4.0); ExecutorService execSvc = Executors.newFixedThreadPool(7); execSvc.execute(new Calculator("Mult_1", Calculator.opMult, 0, 1, 2, rsc)); execSvc.execute(new Calculator("Mult_2", Calculator.opMult, 3, 4, 5, rsc)); execSvc.execute(new Calculator("Mult_3", Calculator.opMult, 6, 7, 8, rsc)); execSvc.execute(new Calculator("Mult_4", Calculator.opMult, 9, 10, 11, rsc)); execSvc.execute(new Calculator("Sub_5", Calculator.opSub, 2, 5, 12, rsc)); execSvc.execute(new Calculator("Sub_6", Calculator.opSub, 11, 8, 13, rsc)); execSvc.execute(new Calculator("Sum_7", Calculator.opSum, 12, 13, 14, rsc)); while (!rsc.flags[14]) { try { Thread.sleep(2); } catch(InterruptedException e) {} } rsc.stopFlag = true; System.out.println(); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 3; ++j) { System.out.print("(" + (3 * i + j) + "): " + rsc.data[3 * i + j] + ", "); } System.out.println(); } System.out.println("(" + 12 + "): " + rsc.data[12] + ", "); System.out.println("(" + 13 + "): " + rsc.data[13] + ", "); System.out.println("(" + 14 + "): " + rsc.data[14] + ", "); execSvc.shutdown(); System.out.println("Main process ended"); } }

 

:

  1. , ii i .
  2. ' i ii i i .
  3. ' i .

 





:


: 2016-11-24; !; : 358 |


:

:

.
==> ...

1538 - | 1467 -


© 2015-2024 lektsii.org - -

: 0.009 .