AnyThread.java :  » Web-Framework » anvil » anvil » core » runtime » Java Open Source

Java Open Source » Web Framework » anvil 
anvil » anvil » core » runtime » AnyThread.java
/*
 * $Id: AnyThread.java,v 1.21 2002/09/16 08:05:03 jkl Exp $
 *
 * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
 *
 * Use is subject to license terms, as defined in
 * Anvil Sofware License, Version 1.1. See LICENSE 
 * file, or http://njet.org/license-1.1.txt
 */
package anvil.core.runtime;

import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.script.Context;
import anvil.script.Function;

/// @class Thread
/// A thread is a thread of execution in a program. 
/// Multiple threads of execution may be running
/// concurrently. 

/**
 * class AnyThread
 *
 * @author: Jani Lehtimki
 */
public class AnyThread extends AnyAbstractClass
{

  public static final anvil.core.RuntimePermission CAN_USE = new anvil.core.RuntimePermission("anvil.runtime.Thread", false);

  public static final Any MIN_PRIORITY = Any.create(Thread.MIN_PRIORITY);
  public static final Any NORMAL_PRIORITY = Any.create(Thread.NORM_PRIORITY);
  public static final Any MAX_PRIORITY = Any.create(Thread.MAX_PRIORITY);


  private static int _threadCount = 0;
  
  /// @constructor Thread
  /// Creates new thread.
  /// @synopsis Thread()
  /// @synopsis Thread(string name)
  public static final Object[] newInstance = { null, "*name", null };
  public static final Any newInstance(Context context, String name)
  {
    context.checkAccess(AnyThread.CAN_USE);
    if (name == null) {
      name = "Task-"+(++_threadCount);
    }
    return new AnyThread(new FunctionThread(context, new FunctionTask(), name));
  }



  private FunctionThread _thread;
  
  
  public AnyThread(FunctionThread thread)
  {
    _thread = thread;
  }
  

  public final anvil.script.ClassType classOf() {
    return __class__;
  }


  public Object toObject()
  {
    return _thread;
  }


  /// @method getName
  /// Returns the name of this thread.
  /// @synopsis string getName()
  public Any m_getName() 
  {
    return Any.create(_thread.getName());
  }


  /// @method getPriority
  /// Returns the priority setting for this thread.
  /// @synopsis int getPriority()
  public Any m_getPriority() 
  {
    return Any.create(_thread.getPriority());
  }


  /// @method setName
  /// Sets the name of this thread.
  /// @synopsis Thread setName(string name)
  public static final Object[] p_setName = { "name" };
  public Any m_setName(String name) 
  {
    _thread.setName(name);
    return this;
  }


  /// @method setPriority
  /// Sets the priority for this thread.
  /// @synopsis Thread setPriority(int priority)
  public static final Object[] p_setPriority = { "priority" };
  public Any m_setPriority(int p) 
  {
    if (p < Thread.MIN_PRIORITY) { 
      p = Thread.MIN_PRIORITY;
    } else if (p > Thread.MAX_PRIORITY) {
      p = Thread.MAX_PRIORITY;
    }
    _thread.setPriority(p);
    return this;
  }


  /// @method interrupt
  /// Interrupts this thread, possibly generating 
  /// Interrupted, or InterruptedIO exception in currently 
  /// pending operation in this thread.
  /// @synopsis Thread interrupt()
  public Any m_interrupt() 
  {
    _thread.interrupt();
    return this;
  }


  /// @method interrupted
  /// Checks if this thread has been interrupted.
  /// Interrupted status is cleared when this method is called.
  /// @synopsis boolean interrupted()
  public Any m_interrupted() 
  {
    return _thread.interrupted() ? TRUE : FALSE;
  }


  /// @method isInterrupted
  /// Checks if this thread has been interrupted.
  /// Interrupted status is NOT cleared when this method is called.
  /// @synopsis boolean isInterrupted()
  public Any m_isInterrupted() 
  {
    return _thread.isInterrupted() ? TRUE : FALSE;
  }


  /// @method isAlive
  /// Checks if this thread is still alive.
  /// @synopsis boolean isAlive()
  public Any m_isAlive() 
  {
    return _thread.isAlive() ? TRUE : FALSE;
  }
  


  /// @method isDaemon
  /// Checks if this thread is marked as daemon.
  /// @synopsis boolean isDaemon()
  public Any m_isDaemon() 
  {
    return _thread.isDaemon() ? TRUE : FALSE;
  }


  /// @method setDaemon
  /// Sets the daemon status of this thread.
  /// @synopsis Thread setDaemon(boolean isDaemon)
  /// @throws BadState If this thread is active
  public static final Object[] p_setDaemon = { null, "isDaemon" };
  public Any m_setDaemon(Context context, boolean daemon) 
  {
    try {
      _thread.setDaemon(daemon);
      return this;
    } catch (IllegalStateException e) {
      throw context.BadState(e.getMessage());
    }
  }


  /// @method join
  /// @synopsis Thread join() ;
  /// Waits indefinitly for this thread to terminate.
  /// @synopsis Thread join(int millis) ;
  /// Waits at most specified milliseconds for this thread to terminate.
  /// @synopsis Thread join(int millis, int nanos) ;
  /// Waits at most specified milli- and nanoseconds for this thread to terminate.
  /// @throws Interrupted if another thread has interrupted the current thread. 
  public static final Object[] p_join = { null, "*millis", new Long(0), "*nanos", new Integer(0) }; 
  public Any m_join(Context context, long millis, int nanos) 
  {
    if (nanos < 0) {
      nanos = 0;
    }
    if (millis < 0) {
      millis = 0;
    }
    try {
      _thread.join(millis, nanos);
      return this;
    } catch (InterruptedException e) {
      throw context.Interrupted(e.getMessage());
    }
  }


  /// @method start
  /// Starts the execution of thread.
  /// @synopsis Thread start(Function callable, ..parameters)
  /// @throws BadState If thread is already started
  public static final Object[] p_start = { null, "callable", "parameters" };
  public Any m_start(Context context, Any callable, Any[] parameters) 
  {
    try {
      _thread.start(callable, parameters);
      return this;
    } catch (IllegalStateException e) {
      throw context.BadState(e.getMessage());
    }
  }


  public static final anvil.script.compiler.NativeClass __class__ = 
    new anvil.script.compiler.NativeClass("Thread", AnyThread.class,
    //DOC{{
    ""+
      " @class Thread\n" +
      " A thread is a thread of execution in a program. \n" +
      " Multiple threads of execution may be running\n" +
      " concurrently. \n" +
      " @constructor Thread\n" +
      " Creates new thread.\n" +
      " @synopsis Thread()\n" +
      " @synopsis Thread(string name)\n" +
      " @method getName\n" +
      " Returns the name of this thread.\n" +
      " @synopsis string getName()\n" +
      " @method getPriority\n" +
      " Returns the priority setting for this thread.\n" +
      " @synopsis int getPriority()\n" +
      " @method setName\n" +
      " Sets the name of this thread.\n" +
      " @synopsis Thread setName(string name)\n" +
      " @method setPriority\n" +
      " Sets the priority for this thread.\n" +
      " @synopsis Thread setPriority(int priority)\n" +
      " @method interrupt\n" +
      " Interrupts this thread, possibly generating \n" +
      " Interrupted, or InterruptedIO exception in currently \n" +
      " pending operation in this thread.\n" +
      " @synopsis Thread interrupt()\n" +
      " @method interrupted\n" +
      " Checks if this thread has been interrupted.\n" +
      " Interrupted status is cleared when this method is called.\n" +
      " @synopsis boolean interrupted()\n" +
      " @method isInterrupted\n" +
      " Checks if this thread has been interrupted.\n" +
      " Interrupted status is NOT cleared when this method is called.\n" +
      " @synopsis boolean isInterrupted()\n" +
      " @method isAlive\n" +
      " Checks if this thread is still alive.\n" +
      " @synopsis boolean isAlive()\n" +
      " @method isDaemon\n" +
      " Checks if this thread is marked as daemon.\n" +
      " @synopsis boolean isDaemon()\n" +
      " @method setDaemon\n" +
      " Sets the daemon status of this thread.\n" +
      " @synopsis Thread setDaemon(boolean isDaemon)\n" +
      " @throws BadState If this thread is active\n" +
      " @method join\n" +
      " @synopsis Thread join() ;\n" +
      " Waits indefinitly for this thread to terminate.\n" +
      " @synopsis Thread join(int millis) ;\n" +
      " Waits at most specified milliseconds for this thread to terminate.\n" +
      " @synopsis Thread join(int millis, int nanos) ;\n" +
      " Waits at most specified milli- and nanoseconds for this thread to terminate.\n" +
      " @throws Interrupted if another thread has interrupted the current thread. \n" +
      " @method start\n" +
      " Starts the execution of thread.\n" +
      " @synopsis Thread start(Function callable, ..parameters)\n" +
      " @throws BadState If thread is already started\n" 
    //}}DOC
    );
  static {
    RuntimeModule.class.getName();
  }
  
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.