1 19 20 package org.logicalcobwebs.concurrent; 21 22 23 132 133 public abstract class FJTask implements Runnable { 134 135 142 143 private volatile boolean done; 145 152 153 public static FJTaskRunner getFJTaskRunner() { 154 return (FJTaskRunner) (Thread.currentThread()); 155 } 156 157 162 public static FJTaskRunnerGroup getFJTaskRunnerGroup() { 163 return getFJTaskRunner().getGroup(); 164 } 165 166 167 175 176 public final boolean isDone() { 177 return done; 178 } 179 180 185 186 protected final void setDone() { 187 done = true; 188 } 189 190 200 201 public void cancel() { 202 setDone(); 203 } 204 205 206 215 216 public void reset() { 217 done = false; 218 } 219 220 221 232 233 public void start() { 234 getFJTaskRunnerGroup().executeTask(this); 235 } 236 237 238 259 260 public void fork() { 261 getFJTaskRunner().push(this); 262 } 263 264 290 291 public static void yield() { 292 getFJTaskRunner().taskYield(); 293 } 294 295 301 302 public void join() { 303 getFJTaskRunner().taskJoin(this); 304 } 305 306 316 317 public static void invoke(FJTask t) { 318 if (!t.isDone()) { 319 t.run(); 320 t.setDone(); 321 } 322 } 323 324 382 383 public static void coInvoke(FJTask task1, FJTask task2) { 384 getFJTaskRunner().coInvoke(task1, task2); 385 } 386 387 388 396 397 public static void coInvoke(FJTask[] tasks) { 398 getFJTaskRunner().coInvoke(tasks); 399 } 400 401 406 407 public static class Wrap extends FJTask { 408 protected final Runnable runnable; 409 410 public Wrap(Runnable r) { 411 runnable = r; 412 } 413 414 public void run() { 415 runnable.run(); 416 } 417 } 418 419 420 426 public static class Seq extends FJTask { 427 protected final FJTask[] tasks; 428 429 433 public Seq(FJTask[] tasks) { 434 this.tasks = tasks; 435 } 436 437 440 public Seq(FJTask task1, FJTask task2) { 441 this.tasks = new FJTask[]{task1, task2}; 442 } 443 444 public void run() { 445 for (int i = 0; i < tasks.length; ++i) FJTask.invoke(tasks[i]); 446 } 447 } 448 449 453 454 public static FJTask seq(FJTask[] tasks) { 455 return new Seq(tasks); 456 } 457 458 465 public static class Par extends FJTask { 466 protected final FJTask[] tasks; 467 468 472 public Par(FJTask[] tasks) { 473 this.tasks = tasks; 474 } 475 476 479 public Par(FJTask task1, FJTask task2) { 480 this.tasks = new FJTask[]{task1, task2}; 481 } 482 483 484 public void run() { 485 FJTask.coInvoke(tasks); 486 } 487 } 488 489 490 494 public static FJTask par(FJTask[] tasks) { 495 return new Par(tasks); 496 } 497 498 504 public static class Seq2 extends FJTask { 505 protected final FJTask fst; 506 protected final FJTask snd; 507 508 public Seq2(FJTask task1, FJTask task2) { 509 fst = task1; 510 snd = task2; 511 } 512 513 public void run() { 514 FJTask.invoke(fst); 515 FJTask.invoke(snd); 516 } 517 } 518 519 523 524 public static FJTask seq(FJTask task1, FJTask task2) { 525 return new Seq2(task1, task2); 526 } 527 528 534 public static class Par2 extends FJTask { 535 protected final FJTask fst; 536 protected final FJTask snd; 537 538 public Par2(FJTask task1, FJTask task2) { 539 fst = task1; 540 snd = task2; 541 } 542 543 public void run() { 544 FJTask.coInvoke(fst, snd); 545 } 546 } 547 548 549 553 public static FJTask par(FJTask task1, FJTask task2) { 554 return new Par2(task1, task2); 555 } 556 557 } 558 | Popular Tags |