Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This file is part of the Jikes RVM project (http://jikesrvm.org).
- *
- * This file is licensed to You under the Eclipse Public License (EPL);
- * You may not use this file except in compliance with the License. You
- * may obtain a copy of the License at
- *
- * http://www.opensource.org/licenses/eclipse-1.0.php
- *
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership.
- */
- package org.jikesrvm.compilers.opt.scheduler;
- public interface OptQueue{
- public void enqueue(Object item) throws InterruptedException;
- public Object dequeue() throws InterruptedException;
- }
- /*
- * This file is part of the Jikes RVM project (http://jikesrvm.org).
- *
- * This file is licensed to You under the Eclipse Public License (EPL);
- * You may not use this file except in compliance with the License. You
- * may obtain a copy of the License at
- *
- * http://www.opensource.org/licenses/eclipse-1.0.php
- *
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership.
- */
- package org.jikesrvm.compilers.opt.scheduler;
- public interface OptQueue{
- public void enqueue(Object item) throws InterruptedException;
- public Object dequeue() throws InterruptedException;
- }
- /*
- * This file is part of the Jikes RVM project (http://jikesrvm.org).
- *
- * This file is licensed to You under the Eclipse Public License (EPL);
- * You may not use this file except in compliance with the License. You
- * may obtain a copy of the License at
- *
- * http://www.opensource.org/licenses/eclipse-1.0.php
- *
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership.
- */
- package org.jikesrvm.compilers.opt.scheduler;
- import org.jikesrvm.scheduler.SystemThread;
- import org.vmmagic.pragma.NonMoving;
- @NonMoving
- public class OptCompilerThread extends SystemThread {
- private OptCompilerBlockingQueue taskQueue = null;
- private boolean isStopped = false;
- /**
- * Used to shut down threads
- */
- private static final ThreadDeath threadDeath = new ThreadDeath();
- public OptCompilerThread(OptCompilerBlockingQueue queue){
- super("OptCompilerThread");
- taskQueue = queue;
- }
- @Override
- public void run(){
- while (!isStopped()) {
- try {
- Runnable runnable = (Runnable) taskQueue.dequeue();
- runnable.run();
- } catch (Exception e){
- //log or otherwise report exception,
- //but keep pool thread alive.
- }
- }
- }
- public synchronized void stop(){
- isStopped = true;
- notifyAll();
- this.stop(threadDeath);
- }
- public synchronized boolean isStopped(){
- return isStopped;
- }
- /*
- * This file is part of the Jikes RVM project (http://jikesrvm.org).
- *
- * This file is licensed to You under the Eclipse Public License (EPL);
- * You may not use this file except in compliance with the License. You
- * may obtain a copy of the License at
- *
- * http://www.opensource.org/licenses/eclipse-1.0.php
- *
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership.
- */
- package org.jikesrvm.compilers.opt.scheduler;
- import java.util.LinkedList;
- import java.util.List;
- /**
- * OptCompilerBlockingQueue class used to hold runnable objects See OptCompilerThread.java
- */
- public final class OptCompilerBlockingQueue implements OptQueue {
- private List<Object> queue = new LinkedList<Object>();
- private int limit = 1;
- public OptCompilerBlockingQueue(int limit){
- this.limit = limit;
- }
- public synchronized void enqueue(Object item)
- throws InterruptedException {
- while(queue.size() == limit) {
- wait();
- }
- if(queue.size() == 0) {
- notifyAll();
- }
- queue.add(item);
- }
- public synchronized Object dequeue()
- throws InterruptedException{
- while(queue.size() == 0){
- wait();
- }
- if(queue.size() == limit){
- notifyAll();
- }
- return queue.remove(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement