001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2007 Charles O Nutter <headius@headius.com>
015: *
016: * Alternatively, the contents of this file may be used under the terms of
017: * either of the GNU General Public License Version 2 or later (the "GPL"),
018: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
019: * in which case the provisions of the GPL or the LGPL are applicable instead
020: * of those above. If you wish to allow use of your version of this file only
021: * under the terms of either the GPL or the LGPL, and not to allow others to
022: * use your version of this file under the terms of the CPL, indicate your
023: * decision by deleting the provisions above and replace them with the notice
024: * and other provisions required by the GPL or the LGPL. If you do not delete
025: * the provisions above, a recipient may use your version of this file under
026: * the terms of any one of the CPL, the GPL or the LGPL.
027: ***** END LICENSE BLOCK *****/package org.jruby.internal.runtime;
028:
029: import org.jruby.RubyThread;
030:
031: import edu.emory.mathcs.backport.java.util.concurrent.ExecutionException;
032: import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
033: import edu.emory.mathcs.backport.java.util.concurrent.Executors;
034: import edu.emory.mathcs.backport.java.util.concurrent.Future;
035: import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
036: import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
037: import edu.emory.mathcs.backport.java.util.concurrent.TimeoutException;
038:
039: /**
040: * @author cnutter
041: */
042: public class FutureThread implements ThreadLike {
043: private Future future;
044: private Runnable runnable;
045: public RubyThread rubyThread;
046:
047: private static class DaemonThreadFactory implements ThreadFactory {
048: public Thread newThread(Runnable runnable) {
049: Thread thread = new Thread(runnable);
050: thread.setDaemon(true);
051:
052: return thread;
053: }
054: }
055:
056: private static ExecutorService executor = Executors
057: .newCachedThreadPool(new DaemonThreadFactory());
058:
059: public FutureThread(RubyThread rubyThread, RubyRunnable runnable) {
060: this .rubyThread = rubyThread;
061: this .runnable = runnable;
062: }
063:
064: public void start() {
065: future = executor.submit(runnable);
066: }
067:
068: public void interrupt() {
069: future.cancel(true);
070: }
071:
072: public boolean isAlive() {
073: return future != null && !future.isDone();
074: }
075:
076: public void join() throws InterruptedException, ExecutionException {
077: future.get();
078: }
079:
080: public void join(long millis) throws InterruptedException,
081: ExecutionException, TimeoutException {
082: future.get(millis, TimeUnit.MILLISECONDS);
083: }
084:
085: public int getPriority() {
086: return 1;
087: }
088:
089: public void setPriority(int priority) {
090: //nativeThread.setPriority(priority);
091: }
092:
093: public boolean isCurrent() {
094: return rubyThread == rubyThread.getRuntime()
095: .getCurrentContext().getThread();
096: }
097:
098: public boolean isInterrupted() {
099: return future.isCancelled();
100: }
101: }
|