C # multithreading (II)

Source: Internet
Author: User

This section introduces the basic knowledge of threads. Next we will study the priority of multithreading.

Using system;

Using system. Threading;

namespace test
{< br> class testthread
{< br> Private Static void firstthreadfun ()
{< br> for (INT I = 0; I <10; I ++)
{< br> console. writeline (thread. currentthread. name + "I =" + I);
}< br> console. writeline (thread. currentthread. name + "Priority:" + thread. currentthread. priority);
console. writeline (thread. currentthread. name + "completed");
}

Private Static void secondthreadfun ()
{< br> for (Int J = 0; j <10; j ++)
{< br> console. writeline (thread. currentthread. name + "J =" + J);
}< br> console. writeline (thread. currentthread. name + "Priority:" + thread. currentthread. priority);
console. writeline (thread. currentthread. name + "completed");
}

Static void main (string [] ARGs)
{
//ProgramWhen the main () function is called, a process is started and a thread is also started [this thread is the main thread].

Thread. currentthread. Name = "mainthread ";

 

// Create the second thread

Thread secondthread = new thread (New threadstart (testthread. secondthreadfun ));
// Obtain the name secondthread.
Secondthread. Name = "secondthread ";
// Set the thread priority to abovenormal. [The execution sequence is second in all created threads.]

Secondthread. Priority = threadpriority. abovenormal;

 

// Create the first thread
Thread firstthread = new thread (New threadstart (testthread. firstthreadfun ));
// Obtain the name firstthread
Firstthread. Name = "firstthread ";
// Set the thread priority to the highest level. [in all newly created threads, the first thread is executed. The default thread priority is normal. There are 5 levels in total: highest, abovenormal, normal, belownormal, lowest]

Firstthread. Priority = threadpriority. Highest;

 

For (INT z = 0; Z <20; Z ++)
{
If (Z = 10)
{
Firstthread. Start ();
Firstthread. Join ();

Secondthread. Start ();
Secondthread. Join ();
}
Else
{
Console. writeline (thread. currentthread. Name + "z =" + Z );
}
}
Console. Read ();
}
}

}

Running result:

 

Running result display:

1. The main thread is always the first to run, but when the new thread starts [thread. Start ()] and adds [thread. Join ()], the main thread will be suspended.

2. Two new threads firstthread and secondthread are created. The priority of firstthread is set to highest priority, and that of secondthread is set to second.

3. firstthread is started and added in order, followed by secondthread.

4. if I change the sequence of firstthread secondthread startup [thread. Start ()], what will happen? [Discussed in the next section]

 

Thread priority

When the threads compete for the CPU time, the CPU is given the service according to the thread priority. In the C # application, you can set five different priorities, from high to lowHighest, abovenormal, normal, belownormal, lowestIf the priority is not specified during thread creation, the system defaults to threadpriority. Normal.

To specify a priority for a thread, we can use the followingCode:
// Set the priority to the lowest
Mythread. Priority = threadpriority. Lowest;

By setting the thread priority, we can arrange some important threads for priority execution, such as user response.

 

 

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.