1 jls.loader.provide('jls.lang.Thread');
  2 
  3 jls.lang.Thread = jls.lang.Class.create( /** @lends jls.lang.Thread.prototype */
  4 {
  5     /**
  6      * Creates a thread.
  7      *
  8      * @param {Boolean} [daemon] The thread is a daemon.
  9      * @param {Boolean} [shared] Whether the thread to create shares the current context.
 10      * @constructs
 11      * @class This class represents a thread. Each thread is an execution entity that is
 12      * scheduled independently from other threads in the same process.
 13      * @see jls.lang.Monitor
 14      * @see jls.lang.Lock
 15      */
 16     initialize : function(daemon, shared) {
 17         this._thread = new _native.core.Thread(daemon || false, typeof shared == 'boolean' ? shared : true);
 18     },
 19     /**
 20      * Closes this thread.
 21      * 
 22      * @returns {jls.lang.Thread} This thread.
 23      */
 24     close : function() {
 25         this._thread.close();
 26         return this;
 27     },
 28     /**
 29      * This method should be overriden, it is called in the thread.
 30      */
 31     run : function() {
 32         // to be overriden
 33     },
 34     /**
 35      * Starts this thread.
 36      * 
 37      * @param {Object} [thisArg] The object to use as this when running this thread.
 38      * @returns {jls.lang.Thread} This thread.
 39      */
 40     start : function(thisArg) {
 41         if (thisArg && (typeof thisArg == 'object')) {
 42             // TODO make it native ?
 43             this._thread.start(this.run.bind(thisArg));
 44         } else {
 45             this._thread.start(this.run);
 46         }
 47         return this;
 48     },
 49     /**
 50      * Joins this thread.
 51      * 
 52      * @returns {jls.lang.Thread} This thread.
 53      */
 54     join : function() {
 55         this._thread.join();
 56         return this;
 57     }
 58 });
 59 
 60 Object.extend(jls.lang.Thread, /** @lends jls.lang.Thread */
 61 {
 62     /**
 63      * Causes the currently executing thread to sleep.
 64      * 
 65      * @param {Number} [ms] The length of time to sleep in milliseconds.
 66      */
 67     sleep : function(ms) {
 68         _native.core.sleep(ms || 0);
 69     }
 70 });
 71 
 72