1 19 20 package EDU.oswego.cs.dl.util.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() { return done; } 177 178 183 184 protected final void setDone() { done = true; } 185 186 196 197 public void cancel() { setDone(); } 198 199 200 209 210 public void reset() { done = false; } 211 212 213 224 225 public void start() { getFJTaskRunnerGroup().executeTask(this); } 226 227 228 249 250 public void fork() { getFJTaskRunner().push(this); } 251 252 278 279 public static void yield() { getFJTaskRunner().taskYield(); } 280 281 287 288 public void join() { getFJTaskRunner().taskJoin(this); } 289 290 300 301 public static void invoke(FJTask t) { 302 if (!t.isDone()) { 303 t.run(); 304 t.setDone(); 305 } 306 } 307 308 366 367 public static void coInvoke(FJTask task1, FJTask task2) { 368 getFJTaskRunner().coInvoke(task1, task2); 369 } 370 371 372 380 381 public static void coInvoke(FJTask[] tasks) { 382 getFJTaskRunner().coInvoke(tasks); 383 } 384 385 390 391 public static class Wrap extends FJTask { 392 protected final Runnable runnable; 393 public Wrap(Runnable r) { runnable = r; } 394 public void run() { runnable.run(); } 395 } 396 397 398 404 public static class Seq extends FJTask { 405 protected final FJTask[] tasks; 406 407 411 public Seq(FJTask[] tasks) { 412 this.tasks = tasks; 413 } 414 415 418 public Seq(FJTask task1, FJTask task2) { 419 this.tasks = new FJTask[] { task1, task2 }; 420 } 421 422 public void run() { 423 for (int i = 0; i < tasks.length; ++i) FJTask.invoke(tasks[i]); 424 } 425 } 426 427 431 432 public static FJTask seq(FJTask[] tasks) { 433 return new Seq(tasks); 434 } 435 436 443 public static class Par extends FJTask { 444 protected final FJTask[] tasks; 445 446 450 public Par(FJTask[] tasks) { 451 this.tasks = tasks; 452 } 453 454 457 public Par(FJTask task1, FJTask task2) { 458 this.tasks = new FJTask[] { task1, task2 }; 459 } 460 461 462 public void run() { 463 FJTask.coInvoke(tasks); 464 } 465 } 466 467 468 472 public static FJTask par(FJTask[] tasks) { 473 return new Par(tasks); 474 } 475 476 482 public static class Seq2 extends FJTask { 483 protected final FJTask fst; 484 protected final FJTask snd; 485 public Seq2(FJTask task1, FJTask task2) { 486 fst = task1; 487 snd = task2; 488 } 489 public void run() { 490 FJTask.invoke(fst); 491 FJTask.invoke(snd); 492 } 493 } 494 495 499 500 public static FJTask seq(FJTask task1, FJTask task2) { 501 return new Seq2(task1, task2); 502 } 503 504 510 public static class Par2 extends FJTask { 511 protected final FJTask fst; 512 protected final FJTask snd; 513 public Par2(FJTask task1, FJTask task2) { 514 fst = task1; 515 snd = task2; 516 } 517 public void run() { 518 FJTask.coInvoke(fst, snd); 519 } 520 } 521 522 523 527 public static FJTask par(FJTask task1, FJTask task2) { 528 return new Par2(task1, task2); 529 } 530 531 } 532 | Popular Tags |