document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.  
  3.  *  This file is part of the Jikes RVM project (http://jikesrvm.org).
  4.  *
  5.  *  This file is licensed to You under the Eclipse Public License (EPL);
  6.  *  You may not use this file except in compliance with the License. You
  7.  *  may obtain a copy of the License at
  8.  *
  9.  *      http://www.opensource.org/licenses/eclipse-1.0.php
  10.  *
  11.  *  See the COPYRIGHT.txt file distributed with this work for information
  12.  *  regarding copyright ownership.
  13.  */
  14. package org.jikesrvm.compilers.opt.scheduler;
  15.  
  16. public interface OptQueue{
  17.     public void enqueue(Object item) throws InterruptedException;
  18.     public Object dequeue() throws InterruptedException;
  19. }    
  20.  
  21.  
  22.  
  23. /*
  24.  
  25.  *  This file is part of the Jikes RVM project (http://jikesrvm.org).
  26.  *
  27.  *  This file is licensed to You under the Eclipse Public License (EPL);
  28.  *  You may not use this file except in compliance with the License. You
  29.  *  may obtain a copy of the License at
  30.  *
  31.  *      http://www.opensource.org/licenses/eclipse-1.0.php
  32.  *
  33.  *  See the COPYRIGHT.txt file distributed with this work for information
  34.  *  regarding copyright ownership.
  35.  */
  36. package org.jikesrvm.compilers.opt.scheduler;
  37.  
  38. public interface OptQueue{
  39.     public void enqueue(Object item) throws InterruptedException;
  40.     public Object dequeue() throws InterruptedException;
  41. }    
  42.  
  43.  
  44. /*
  45.  
  46.  *  This file is part of the Jikes RVM project (http://jikesrvm.org).
  47.  *
  48.  *  This file is licensed to You under the Eclipse Public License (EPL);
  49.  *  You may not use this file except in compliance with the License. You
  50.  *  may obtain a copy of the License at
  51.  *
  52.  *      http://www.opensource.org/licenses/eclipse-1.0.php
  53.  *
  54.  *  See the COPYRIGHT.txt file distributed with this work for information
  55.  *  regarding copyright ownership.
  56.  */
  57. package org.jikesrvm.compilers.opt.scheduler;
  58. import org.jikesrvm.scheduler.SystemThread;
  59. import org.vmmagic.pragma.NonMoving;
  60.  
  61. @NonMoving
  62. public class OptCompilerThread extends SystemThread {
  63.  
  64.      private OptCompilerBlockingQueue taskQueue = null;
  65.      private boolean isStopped = false;
  66.          
  67.  
  68.      /**    
  69.       * Used to shut down threads
  70.       */
  71.       private static final ThreadDeath threadDeath = new ThreadDeath();
  72.          
  73.          
  74.       public OptCompilerThread(OptCompilerBlockingQueue queue){
  75.       super("OptCompilerThread");
  76.       taskQueue = queue;  
  77.       }  
  78.  
  79.       @Override
  80.       public void run(){
  81.         while (!isStopped()) {
  82.           try {    
  83.              Runnable runnable = (Runnable) taskQueue.dequeue();
  84.              runnable.run();  
  85.               } catch (Exception e){
  86.                 //log or otherwise report exception,
  87.                 //but keep pool thread alive.
  88.              }  
  89.           }  
  90.       }  
  91.  
  92.       public synchronized void stop(){
  93.         isStopped = true;
  94.         notifyAll();
  95.         this.stop(threadDeath);      
  96.       }
  97.  
  98.       public synchronized boolean isStopped(){
  99.         return isStopped;
  100.       }
  101.  
  102.  
  103. /*
  104.  
  105.  *  This file is part of the Jikes RVM project (http://jikesrvm.org).
  106.  *
  107.  *  This file is licensed to You under the Eclipse Public License (EPL);
  108.  *  You may not use this file except in compliance with the License. You
  109.  *  may obtain a copy of the License at
  110.  *
  111.  *      http://www.opensource.org/licenses/eclipse-1.0.php
  112.  *
  113.  *  See the COPYRIGHT.txt file distributed with this work for information
  114.  *  regarding copyright ownership.    
  115.  */
  116. package org.jikesrvm.compilers.opt.scheduler;
  117.  
  118. import java.util.LinkedList;
  119. import java.util.List;
  120.  
  121.  
  122. /**
  123.  * OptCompilerBlockingQueue class used to hold runnable objects See OptCompilerThread.java
  124.  */
  125. public final class OptCompilerBlockingQueue implements OptQueue {
  126.  
  127.       private List<Object> queue = new LinkedList<Object>();
  128.       private int limit = 1;    
  129.  
  130.       public OptCompilerBlockingQueue(int limit){
  131.         this.limit = limit;
  132.       }
  133.  
  134.                    
  135.       public synchronized void enqueue(Object item)
  136.       throws InterruptedException  {
  137.         while(queue.size() == limit) {
  138.           wait();
  139.         }
  140.         if(queue.size() == 0) {        
  141.           notifyAll();
  142.         }  
  143.           queue.add(item);
  144.       }
  145.  
  146.  
  147.       public synchronized Object dequeue()
  148.       throws InterruptedException{
  149.         while(queue.size() == 0){
  150.           wait();
  151.         }
  152.         if(queue.size() == limit){
  153.           notifyAll();
  154.         }
  155.  
  156.         return queue.remove(0);
  157.       }
  158. }
');