/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
using System;
using System.Threading;
namespace Lucene.Net.Support
{
///
/// Support class used to handle threads
///
public class ThreadClass : IThreadRunnable
{
///
/// The instance of System.Threading.Thread
///
private System.Threading.Thread threadField;
///
/// Initializes a new instance of the ThreadClass class
///
public ThreadClass()
{
threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
}
///
/// Initializes a new instance of the Thread class.
///
/// The name of the thread
public ThreadClass(System.String Name)
{
threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
this.Name = Name;
}
///
/// Initializes a new instance of the Thread class.
///
/// A ThreadStart delegate that references the methods to be invoked when this thread begins executing
public ThreadClass(System.Threading.ThreadStart Start)
{
threadField = new System.Threading.Thread(Start);
}
///
/// Initializes a new instance of the Thread class.
///
/// A ThreadStart delegate that references the methods to be invoked when this thread begins executing
/// The name of the thread
public ThreadClass(System.Threading.ThreadStart Start, System.String Name)
{
threadField = new System.Threading.Thread(Start);
this.Name = Name;
}
///
/// This method has no functionality unless the method is overridden
///
public virtual void Run()
{
}
///
/// Causes the operating system to change the state of the current thread instance to ThreadState.Running
///
public virtual void Start()
{
threadField.Start();
}
///
/// Interrupts a thread that is in the WaitSleepJoin thread state
///
public virtual void Interrupt()
{
threadField.Interrupt();
}
///
/// Gets the current thread instance
///
public System.Threading.Thread Instance
{
get
{
return threadField;
}
set
{
threadField = value;
}
}
///
/// Gets or sets the name of the thread
///
public System.String Name
{
get
{
return threadField.Name;
}
set
{
if (threadField.Name == null)
threadField.Name = value;
}
}
public void SetDaemon(bool isDaemon)
{
threadField.IsBackground = isDaemon;
}
///
/// Gets or sets a value indicating the scheduling priority of a thread
///
public System.Threading.ThreadPriority Priority
{
get
{
try
{
return threadField.Priority;
}
catch
{
return ThreadPriority.Normal;
}
}
set
{
try
{
threadField.Priority = value;
}
catch { }
}
}
///
/// Gets a value indicating the execution status of the current thread
///
public bool IsAlive
{
get
{
return threadField.IsAlive;
}
}
///
/// Gets or sets a value indicating whether or not a thread is a background thread.
///
public bool IsBackground
{
get
{
return threadField.IsBackground;
}
set
{
threadField.IsBackground = value;
}
}
///
/// Blocks the calling thread until a thread terminates
///
public void Join()
{
threadField.Join();
}
///
/// Blocks the calling thread until a thread terminates or the specified time elapses
///
/// Time of wait in milliseconds
public void Join(long MiliSeconds)
{
threadField.Join(new System.TimeSpan(MiliSeconds * 10000));
}
///
/// Blocks the calling thread until a thread terminates or the specified time elapses
///
/// Time of wait in milliseconds
/// Time of wait in nanoseconds
public void Join(long MiliSeconds, int NanoSeconds)
{
threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));
}
///
/// Resumes a thread that has been suspended
///
public void Resume()
{
Monitor.PulseAll(threadField);
}
///
/// Raises a ThreadAbortException in the thread on which it is invoked,
/// to begin the process of terminating the thread. Calling this method
/// usually terminates the thread
///
public void Abort()
{
threadField.Abort();
}
///
/// Raises a ThreadAbortException in the thread on which it is invoked,
/// to begin the process of terminating the thread while also providing
/// exception information about the thread termination.
/// Calling this method usually terminates the thread.
///
/// An object that contains application-specific information, such as state, which can be used by the thread being aborted
public void Abort(object stateInfo)
{
threadField.Abort(stateInfo);
}
///
/// Suspends the thread, if the thread is already suspended it has no effect
///
public void Suspend()
{
Monitor.Wait(threadField);
}
///
/// Obtain a String that represents the current object
///
/// A String that represents the current object
public override System.String ToString()
{
return "Thread[" + Name + "," + Priority.ToString() + "]";
}
[ThreadStatic]
static ThreadClass This = null;
// named as the Java version
public static ThreadClass CurrentThread()
{
return Current();
}
public static void Sleep(long ms)
{
// casting long ms to int ms could lose resolution, however unlikely
// that someone would want to sleep for that long...
Thread.Sleep((int)ms);
}
///
/// Gets the currently running thread
///
/// The currently running thread
public static ThreadClass Current()
{
if (This == null)
{
This = new ThreadClass();
This.Instance = Thread.CurrentThread;
}
return This;
}
public static bool operator ==(ThreadClass t1, object t2)
{
if (((object)t1) == null) return t2 == null;
return t1.Equals(t2);
}
public static bool operator !=(ThreadClass t1, object t2)
{
return !(t1 == t2);
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj is ThreadClass) return this.threadField.Equals(((ThreadClass)obj).threadField);
return false;
}
public override int GetHashCode()
{
return this.threadField.GetHashCode();
}
}
}