Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package test1.tasks;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. public interface TaskAction<TRet> {
  6.     TRet action() throws InterruptedException;
  7. }
  8.  
  9. public interface ParametrizedTaskAction<TArg, TRet> {
  10.     TRet action(TArg arg) throws InterruptedException;
  11. }
  12.  
  13. public interface Task<TRet> {
  14.     TRet result() throws InterruptedException;
  15.     boolean isCompleted();
  16.    
  17.     <R> Task<R> continueWith(ParametrizedTaskAction<TRet, R> act);
  18. }
  19.  
  20. public class TaskManager {
  21.    
  22.     private abstract class TaskImplBase {
  23.         protected final Object _lock = new Object();
  24.         protected boolean _isCompleted;
  25.         protected TaskImplBase _next;
  26.        
  27.         public TaskImplBase() {
  28.         }
  29.        
  30.         public TaskImplBase execute() throws InterruptedException {
  31.            
  32.             this.executeImpl();
  33.            
  34.             synchronized (_lock) {
  35.                 _isCompleted = true;
  36.                 _lock.notifyAll();
  37.                 return _next;
  38.             }
  39.         }
  40.        
  41.         protected abstract void executeImpl() throws InterruptedException;
  42.  
  43.         public boolean isCompleted() {
  44.             synchronized (_lock) {
  45.                 return _isCompleted;
  46.             }
  47.         }
  48.     }  
  49.    
  50.     private class TaskImpl<TRet> extends TaskImplBase implements Task<TRet> {
  51.         private final TaskAction<TRet> _action;
  52.         private TRet _result;
  53.        
  54.         public TaskImpl(TaskAction<TRet> action) {
  55.             _action = action;
  56.         }
  57.        
  58.         @Override
  59.         protected void executeImpl() throws InterruptedException {
  60.             _result = _action.action();
  61.         }
  62.        
  63.         @Override
  64.         public TRet result() throws InterruptedException {
  65.             synchronized (_lock) {
  66.                 if (_isCompleted)
  67.                     return _result;
  68.                
  69.                 _lock.wait();
  70.                 return _result;
  71.             }
  72.         }
  73.  
  74.         @Override
  75.         public <R> Task<R> continueWith(ParametrizedTaskAction<TRet, R> act) {
  76.             synchronized (_lock) {
  77.                 TaskAction<R> action = () -> act.action(TaskImpl.this.result());
  78.                
  79.                 if (_isCompleted) {
  80.                     return TaskManager.this.start(action);
  81.                 } else {
  82.                     TaskImpl<R> next = TaskManager.this.new TaskImpl<R>(action);
  83.                     _next = next;
  84.                     return next;
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90.     private class TaskThread extends Thread {
  91.         private final Object _lock = new Object();
  92.        
  93.         private TaskImplBase _currTask;
  94.  
  95.         @Override
  96.         public void run() {
  97.             try {
  98.                
  99.                 do {
  100.                     synchronized (_lock) {
  101.                         if(_currTask == null)
  102.                             _lock.wait();
  103.                     }
  104.                    
  105.                     _currTask = _currTask.execute();
  106.                 } while (_currTask != null);
  107.                
  108.                 TaskManager.this.releaseTaskThread(this);
  109.                
  110.             } catch (Exception e) {
  111.                 // TODO: handle exception
  112.                 System.out.println(e.toString());
  113.             }
  114.         }
  115.        
  116.         public void doWork(TaskImplBase task) {
  117.             synchronized (_lock) {
  118.                 _currTask = task;
  119.                 _lock.notify();
  120.             }
  121.         }
  122.        
  123.     }
  124.    
  125.     private final Object _syncRoot = new Object();
  126.     private final LinkedList<TaskThread> _availableThreads = new LinkedList<>();
  127.     private final LinkedList<TaskThread> _activeThreads = new LinkedList<>();
  128.    
  129.     public TaskManager() {
  130.     }
  131.    
  132.     private void releaseTaskThread(TaskThread th) {
  133.         synchronized (_syncRoot) {
  134.             _activeThreads.remove(th);
  135.             _availableThreads.addLast(th);
  136.         }
  137.     }
  138.  
  139.     public <R> Task<R> start(TaskAction<R> act) {
  140.         synchronized (_syncRoot) {
  141.             TaskImpl<R> task = new TaskImpl<R>(act);
  142.             TaskThread th;
  143.            
  144.             if (_availableThreads.size() > 0) {
  145.                 th =  _availableThreads.removeFirst();
  146.                 th.doWork(task);
  147.             } else {
  148.                 th = new TaskThread();
  149.                 th.doWork(task);
  150.                 th.start();            
  151.             }
  152.  
  153.             _activeThreads.addLast(th);
  154.            
  155.             return task;
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement