GPS platform, site construction, software development, system operation and maintenance, to find a large forest network technology!
http://cnsendnet.taobao.com
from Sen Science and Technology official blog
http://www.cnsendblog.com/index.php/?p=431
1, the current thread
Thread.CurrentThread
2, thread control
- Start (): start the thread
- Sleep (int): static method, pause the currently specified number of milliseconds threads
- Abort (): This method is generally used to terminate a thread, unrecoverable error will again perform Start
- Suspend (): This method does not terminate unfinished threads, it just hung threads, also after recovery
- Resume (): resume the suspended Suspend () method thread of execution
- Join (): the main thread to wait until the end of the current thread
3, ThreadState property
Thread.ThreadState this property represents a thread running state:
- Aborted: The thread has stopped
- AbortRequested: thread Thread.Abort () method has been called, but the thread has not stopped
- Background: thread in the background, related to property Thread.IsBackground
- Running: The thread is operating normally
- Stopped: thread has been stopped
- StopRequested: the thread is being asked to stop
- Suspended: the threads have been suspended (in this state, you can rerun by calling the Resume () method)
- SuspendRequested: threads are asked to be suspended, but not enough time to respond
- Unstarted: Thread.Start not invoke () to start the thread running
- WaitSleepJoin: Because the thread calls Wait (), Sleep () or Join () methods in a lockdown
4, priority
Can be set to five different priority levels, from highest to lowest are the Highest, AboveNormal, Normal, BelowNormal, Lowest, if you do not specify a priority, the system defaults to ThreadPriority.Normal when creating a thread.
// set priorities for the lowest
myThread.Priority = ThreadPriority.Lowest;
5, multi-threaded examples
Thread inlet ThreadStart by the proxy to provide, as can be understood ThreadStart a function pointer to a function of thread to be executed, after a call to the Thread.Start () method, a thread starts executing functions pointed ThreadStart.
class Program { static void Main(string[] args) { TestClass test = new TestClass(); Thread th1 = new Thread(new ThreadStart(test.ThreadMethod)); th1.Start(); // main thread suspended 4ms, CPU execution thread th1 Thread.Sleep ( 4 ); th1.Abort (); th1.Join(); Console.ReadLine(); } } public class TestClass { public void ThreadMethod() { while (true) { Console.WriteLine ( " Thread 1 is running " ); } } }
6, thread deadlock
In order to prevent resources being used simultaneously, c # lock is used to lock and Monitor resource, processing is completed and then release resources. In fact, when the C # compiler to compile lock statement, call the Monitor lock compiled into class, so you can use the Monitor replacement lock. Using this method will most likely lead to a deadlock situation.
Two resource preemption two threads, thread 1 grab the resource A, the resource B required; 2 grab thread resource B, but require resources A; the result is deadlock.
Resolve the deadlock problem, you can use Monitor.TryEntry, set the timeout
if(Monitor.TryEntry(lockObj, 1000)) { try{ } finally{ Monitor.Exit(lockObj); } } the else { // process code timeout }
GPS platform, site construction, software development, system operation and maintenance, to find a large forest network technology!
http://cnsendnet.taobao.com
from Sen Science and Technology official blog
http://www.cnsendblog.com/index.php/?p=431