NET multi-thread exploration-NET thread Basics

Source: Internet
Author: User
Tags net thread

Foreground thread and background thread

Foreground thread: when the application ends, if the foreground thread does not complete the task, the foreground thread will not end. Unless you forcibly end all foreground threads in the application process. Foreground threads are suitable for tasks that must be completed.
Background thread: when the application ends, the background thread is forcibly terminated by CLR and no exception is thrown. Example:
Static void Main (string [] args)
{
Thread t = new Thread (Test );
T. IsBackground = true;
// Here the thread is a background thread, and the application will end immediately
// If it is a foreground thread, it will end in about 5 seconds
T. Start ();
Console. WriteLine ("");
}
Static void Test ()
{
Thread. Sleep (5*1000 );
Console. WriteLine ("B ");
}
 
Determine whether the thread is finished
 
Public bool IsAlive {get ;}
Public void Join ();
 
Static void Main (string [] args)
{
Thread A = new Thread () =>{ Thread. Sleep (2*1000 );});
Thread B = new Thread () =>{ Thread. Sleep (3*1000 );});
A. Start ();
B. Start ();
While (A. IsAlive)
{
Console. WriteLine ("thread A is executing ...");
Thread. Sleep (500 );
}
Console. WriteLine ("thread A is finished ...");
// This is the blocking method, and the code after the execution can only be executed until thread B completes.
B. Join ();
Console. WriteLine ("thread B execution completed ...");
Console. Read ();
}
 
Passing parameters for threads
 
Public delegate void ParameterizedThreadStart (object obj );
[SecuritySafeCritical]
Public Thread (ParameterizedThreadStart start );
 
Static void Main (string [] args)
{
Thread A = new Thread (object str) =>
{
Thread. Sleep (1*1000 );
Console. WriteLine (str );
});
A. Start ("A Thread ...");
A. Join (); // The blocking program is executed until the execution of thread A is completed.

Thread B = new Thread (new ParameterizedThreadStart (object str) =>
{
Thread. Sleep (1*1000 );
Console. WriteLine (str );
}));
B. Start ("B Thread ...");
B. Join ();

Thread C = new Thread (new ParameterizedThreadStart (Test ));
C. Start ("C Thread ...");
Console. Read ();
}
Static void Test (object str)
{
Thread. Sleep (1*1000 );
Console. WriteLine (str );
}
 
Thread priority: www.2cto.com

Threads with higher priority occupy more CPU time.
However, when high-priority threads are waiting for some resources, low-priority threads can run.
Public class MyThread
{
// Execution count
Public Int32 Count;
// Execute the switch
Public static bool Stop;
// Execution thread
Public Thread t;

Public MyThread (string ThreadName)
{
// Set the switch
Stop = false;
// Set the thread method
T = new Thread (new ThreadStart () =>
{
While (! Stop & Count <10000000)
{
Count ++;
}
Stop = true;
Console. WriteLine ("{0} thread ended...", t. Name );
}));
// Set the thread name
T. Name = ThreadName;
}
}
 
Static void Main (string [] args)
{
MyThread A = new MyThread ("Hige ");
MyThread B = new MyThread ("Low B ");

A. t. Priority = ThreadPriority. Highest;
B. t. Priority = ThreadPriority. Lowest;

A. t. Start ();
B. t. Start ();

A. t. Join ();
B. t. Join ();

Console. WriteLine ("A thread Count {0}", A. Count );
Console. WriteLine ("B thread Count {0}", B. Count );
Console. Read ();
}
 
 
Thread Pool

Note: 1. the threads in the managed thread pool are all background threads. 2. The actual number of threads in the thread pool can be smaller than the minimum values when there are few requirements.
Advantages of the thread pool: Both thread creation and thread destruction consume a large amount of system resources, while the thread pool always maintains a thread list. When the thread is idle, it only sleeps,
Once a task comes in, it will be awakened to execute the task.
Static void Main (string [] args)
{
ThreadPool. QueueUserWorkItem (object str) =>
{
Thread. Sleep (1*1000 );
Console. WriteLine (str );
}, "Hello ");

Int max, min, io;
ThreadPool. GetMaxThreads (out max, out io );
Console. WriteLine ("Maximum number of auxiliary threads in the thread pool: {0}, \ n maximum number of asynchronous I/O threads in the thread pool {1}", max, io );
ThreadPool. GetMinThreads (out min, out io );
Console. writeLine ("the thread pool creates a minimum number of auxiliary threads as needed: {0}, \ n the thread pool creates a minimum number of asynchronous I/O threads {1 }", min, io );
Console. Read ();
}
The thread pool is suitable for some simple tasks. If you need full control permissions on the task thread, declare the thread independently.

 

From the sea, not blue

Related Article

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.