The default thread is the foreground thread, which means that any foreground thread will keep the program alive during running.
Background thread: as long as there is a foreground thread running, the application process is running. If multiple foreground threads are running and the Main () method is finished, the application process is activated until all foreground threads complete their tasks. Apple (14)
The only difference between a foreground thread and a background thread is that the background thread does not stop the process from terminating.
By default, all threads created using the Thread class are foreground threads. Threads in the thread pool are always background threads.
When using the Thread class to create a Thread, you can set the IsBackground attribute to determine whether the Thread is a (false) Foreground Thread or a (true) Background Thread.
Using System. Threading;
Namespace ConsoleApplication1
{
Class Program
{
Static void myThread ()
{
Console. WriteLine ("the new thread starts to output messages ");
Console. ReadKey ();
Thread. Sleep (4000); // The main Thread can be terminated 4 seconds earlier than NewThread.
Console. WriteLine ("New thread end Output Message ");
Console. ReadKey ();
}
Static void Main (string [] args)
{
Thread thread = new Thread (myThread );
Thread. Name = "NewThread ";
// When the IsBackground attribute is set to false, the thread NewThread can print two messages after the main thread completes execution.
// If IsBackground is true, the second message cannot be printed after the main thread ends because the main thread (foreground thread) ends,
The background thread also exits.
Thread. IsBackground = false;
Thread. Start ();
Console. WriteLine ("the main thread has ended ");
}
}
}
Applicable scenarios for foreground and background threads
Generally, the background thread is very suitable for completing background tasks. You should set the thread for passive listening activity as the background thread, and set the thread for sending data as the foreground thread, the thread will not be terminated until all data is sent.
For example, if the Word application is disabled, it makes no sense for the spelling checker to continue the running process. At the end of the application, the spelling checker can be closed.
Thread priority
After a thread running on windows runs for a certain period of time (a time slice), windows will "Schedule" the thread to specify the priority, which can affect the scheduling.
In windows, programs start to run based on their priorities. That is to say, if a thread with a priority of 25 can be executed, windows will not call a thread with a priority of 24. However, windows is a "preemptible" operating system.
(A thread executed on windows can be preemptible at any time). If a thread with a higher priority is ready to run and is currently running a thread with a lower priority, windows will force lower-priority threads to stop running and start
Run a thread with a higher priority.
Since thread calls on windows are implemented through the thread priority (in general), if we want to make our program be scheduled as much as possible, you need to set the thread priority, video card
In the Thread class, you can set the Priority attribute to affect the basic Priority of the Thread. The Priority attribute requires a value defined by the ThreadPriority enumeration.
Highest> AboveNormal> Normal> BelowNormal> Lowest
Generally, we do not need to set the thread priority or set it to the Normal priority.
Using System. Threading;
Namespace ConsoleApplication1
{
Class Program
{
Static bool loopSwitch = true;
Static void myThread ()
{
Long threadCount = 0;
// Perform Addition
While (loopSwitch) {threadCount ++ ;}
// Display the result
Console. WriteLine ("{0}, priority: {1}" + "count: {2 }",
Thread. CurrentThread. Name,
Thread. CurrentThread. Priority. ToString (),
ThreadCount. ToString ());
Console. ReadLine ();
}
Static void Main (string [] args)
{
Thread threadOne = new Thread (myThread );
ThreadOne. Name = "ThreadOne ";
Thread threadTwo = new Thread (myThread );
ThreadTwo. Name = "ThreadTwo ";
ThreadTwo. Priority = ThreadPriority. Lowest;
ThreadOne. Start ();
ThreadTwo. Start ();
Thread. Sleep (2000 );
LoopSwitch = false;
}
}
}
Running result:
ThreadTwo, priority: Lowest to: 744795888
ThreadOne, priority: Normal to 745653704
Obviously, the number of threadones with a higher thread priority needs to be greater, that is, it executes faster.
Note that the thread priority is related to the platform. Different Operating Systems will schedule different threads. That is to say, the priority you set in Windows may not be used in other operating systems at all.
We recommend that you do not increase the thread priority. If the thread priority is too high, it may affect the execution of other threads, leading to serious, unpredictable, and unimaginable consequences.
Author: "The blog of daemon"