C # Sleep Example-thread hangs

Source: Internet
Author: User
Tags terminates

Using System;
Using System.Threading;
public class arr
{
public static void Main ()
{
Int[] arr;
arr = new Int[5];
int Luzi;
for (luzi=1;luzi<10000;luzi++)
{
Console.WriteLine ("+luzi+", "line");
if (luzi==5000)
{
Thread.Sleep (10000);
}
}
}
}

Line No. 5000 starts to pause for 10 seconds

The System.Threading namespace of the NET base Class library provides a number of classes and interfaces that support multithreading. There are many classes in this namespace. The System.Threading.Thread class is the most common class that creates and controls a thread, sets its priority, and obtains its state. He has a lot of methods, here we will be more common and important methods to do the introduction:
Thread.Start (): Initiates the execution of the thread;
Thread.Suspend (): Suspends the thread, or does not work if the thread is suspended;
Thread.Resume (): continues the suspended thread;
Thread.Interrupt (): Aborts the thread in wait or sleep or join thread state;
Thread.Join (): Blocks the calling thread until a thread terminates
Thread.Sleep (): Blocks the current thread for the specified number of milliseconds;
Thread.Abort (): To begin the process of terminating this thread.   If the thread is already terminating, the thread cannot be started by Thread.Start (). The thread can be paused/blocked by calling Thread.sleep,thread.suspend or Thread.Join. Calling the sleep () and Suspend () methods means that the thread will no longer get CPU time. The two methods of suspending a thread are different, and Sleep () causes the thread to stop executing immediately, but the common language runtime must reach a security point before calling the Suspend () method. One thread cannot call the sleep () method on another thread, but the suspend () method can be called to cause another thread to pause execution. Calling the Thread.Resume () method on a thread that has already been suspended causes it to continue executing. No matter how many times the Suspend () method is used to block a thread, the resume () method is called only once to allow the thread to continue executing. A thread that has been terminated and has not yet started execution cannot use a hang. Thread.Sleep (int x) causes the thread to block x milliseconds. Only if the thread is being called by another thread by calling the Thread.Interrupt () or Thread.Abort () method can it be awakened. If you call the Thread.Interrupt () method on a thread that is in a blocking state, the thread state will change, but the threadinterupptedexception exception will be thrown, and you can catch the exception and make a deal. You can also ignore this exception and let the runtime terminate the thread. Within a certain wait time, Thread.Interrupt () and Thread.Abort () can wake up a thread immediately.
We can permanently destroy a thread by using the Thread.Abort () method, and will throw a ThreadAbortException exception. So that the terminating thread can catch the exception but it is difficult to control the recovery, the only way is to call Thread.resetabort () to cancel just the call, and only if the exception is caused by the calling thread. For A and b two threads, the a thread can use the Thread.Abort () method correctly for the B thread, but the B thread cannot call Thread.resetabort () to cancel the Thread.Abort () operation.
The Thread.Abort () method allows the system to silently destroy the thread without notifying the user. Once the Thread.Abort () operation is implemented, the thread cannot be restarted. Calling this method does not mean that the thread is destroyed immediately, so in order to determine whether the thread is destroyed, we can call Thread.Join () to determine its destruction, Thread.Join () is a blocking call until the thread does terminate before returning. However, it is possible that one thread calls the Thread.Interrupt () method to abort another thread while the thread is waiting for the return of the Thread.Join () call.
Do not use the Suspend () method to suspend blocking threads as much as possible, because it is easy to create deadlocks. Suppose you suspend a thread, and the resources of that thread are what the other threads need and what happens. Therefore, we try to give different priorities to threads of different importance, using the Thread.priority () method instead of the Thread.Suspend () method.
The thread class has a lot of attributes, and these are important attributes that we have to master for multithreaded programming.
Thread.isalive property: Gets a value that indicates the execution state of the current thread. True if this thread has started and has not been terminated or aborted properly; otherwise, false.
Thread.Name property: Gets or sets the name of the thread.
Thread.priority property: Gets or sets a value that indicates the scheduling priority of the thread.
Thread.threadstate property: Gets a value that contains the state of the current thread. Thread Status
The System.Threading.Thread.ThreadState property defines the state of the thread at execution time. When a thread terminates from creation to thread, it must be in one of the states. When a thread is created, it is in the unstarted state, and the start () method of the thread class causes the thread state to become running state, and the thread will remain in this state unless we call the appropriate method to suspend, block, destroy, or terminate it naturally. If the thread is suspended, it will be in the suspended state, unless we call the resume () method to re-execute it, and the thread will become running again. Once the thread is destroyed or terminated, the thread is in the stopped state. A thread that is in this state will no longer exist, as the thread will not be able to return to the unstarted state until it starts. The thread also has a background state that indicates whether the thread is running in the foreground or in the background. At a certain time, the thread may be in more than one state. As an example, a thread is called sleep and is blocked, and then another thread calls the Abort method on the blocked thread, when the thread is in both the WaitSleepJoin and abortrequested states. Once the thread response is switched to SLE blocking or aborting, a ThreadAbortException exception is thrown when destroyed. Thread Priority
System.Threading.Thread.Priority enumerates the priority levels of a thread, which determines how much CPU time the thread can get. High-priority threads typically get more CPU time than normal-priority threads, and if more than one high-priority thread, the operating system will cycle the CPU time between those threads. Low-priority threads get relatively little CPU time, and when there are no high-priority threads, the operating system picks the next low-priority thread to execute. Once a low-priority thread encounters a high-priority thread at execution time, it yields the CPU to the high-priority thread. The newly created thread priority is the general priority, and we can set the value of the thread's priority level, as shown below: Highest AboveNormal Normal belownormal Lowest

C # Sleep Example-thread hangs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.