Thread series 01, foreground thread, background thread, thread synchronization, 01 foreground
In the console application set, the Main method starts with a thread. If you want to create another thread, you need to use the namespace System. Threading.
□Create the first thread
using System;
using System.Threading;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
var thread = new Thread(DoSth);
thread.Start();
}
static void DoSth()
{
Console. WriteLine ("I am from another thread ");
}
}
}
□Foreground thread and background thread
The newly created thread is a foreground thread by default. You can set the IsBackground attribute to true to define the thread as a background thread. Once it is defined as a background thread, as long as the foreground thread ends, no matter whether the background thread ends or not, the application process ends.
using System;
using System.Threading;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
var thread = new Thread(DoSth);
thread.IsBackground = true;
thread.Start(1);
Console. WriteLine ("leaving the main thread ");
}
static void DoSth(object threadId)
{
Console. WriteLine ("I am from another thread" + threadId );
}
}
}
○ The ManagedThreadId attribute is the managed thread Id, which is unique within the process and is not the same as the thread Id of the operating system.
○ The Start method can contain parameters, which will be passed to the thread method.
○ IsBackground attribute, set whether the thread is a background thread
□Thread Synchronization
※The thread is not synchronized.
Assume that there is a static variable in the main program and there is an infinite loop in the method of the main program. Each time you increase the static variable by 1.
If you give this method to a thread.
class Program
{
private static int count = 0;
static void Main(string[] args)
{
var t1 = new Thread(AddCount);
t1.Start();
}
static void AddCount()
{
while (true)
{
int temp = count;
Thread.Sleep(1000);
count = temp + 1;
Console. WriteLine ("My managed Thread ID is:" + Thread. CurrentThread. ManagedThreadId + "the current count value is:" + count );
Thread.Sleep(1000);
}
}
}
Running well. The displayed count value increases continuously by 1.
If you give this method to two threads.
class Program
{
private static int count = 0;
static void Main(string[] args)
{
var t1 = new Thread(AddCount);
var t2 = new Thread(AddCount);
t1.Start();
t2.Start();
}
......
}
We found that the count value is not incremental. That is to say, the count value is not synchronized.
→ Enter thread 1, temp = 0, thread 1 start sleep
→ Enter thread 2, temp = 0, thread 2 start sleep
→ Thread 1 "wake up", let count = 1, show the count value is 1, and sleep
→ Thread 2 "wake up", temp is still 0, so count is still 1, show count value is 1, and sleep
→ This loop
The problem here is: I wanted to keep count increasing, but thread 1 and thread 2 were not synchronized in due time. How can this problem be solved?
※Synchronize threads.
With the lock block, two threads can be synchronized, so that only one thread enters a part of the program execution at a time.
class Program
{
private static int count = 0;
static object o = new object();
static void Main(string[] args)
{
var t1 = new Thread(AddCount);
var t2 = new Thread(AddCount);
t1.Start();
t2.Start();
}
static void AddCount()
{
while (true)
{
lock (o)
{
int temp = count;
Thread.Sleep(1000);
count = temp + 1;
Console. WriteLine ("My managed Thread ID is:" + Thread. CurrentThread. ManagedThreadId + "the current count value is:" + count );
}
Thread.Sleep(1000);
}
}
}
Summary:
○ If a main thread is allowed to end and other threads end regardless of execution conditions, set other threads as background threads.
○ Lock statement block ensures Thread Synchronization
The thread series includes:
Thread series 01, front-end thread, back-end thread, thread synchronization thread series 02, multiple threads simultaneously process a time-consuming task to save time thread series 03, multi-thread shared data, multi-thread data sharing
Some Questions about the foreground thread and background thread
I hope it will help you. "Exit after running all foreground Threads" means the exit of the process, which is more accurate to the exit of the current application domain. When your main thread is closed, if the CLR detects that there are other foreground threads, AppDomain will wait for its end, then call the unload method, and then clear the resource. If there are no other application domains, the process also exits. This is the general order. As for the sudden shutdown of the process in the task manager, you can force the process to exit, but it is only an exception. If it is hard to understand, imagine killing the process, destroy the memory, and the system will crash.
Is the main () thread a foreground thread or a background thread? What are the differences between the frontend and backend threads?
The main () function is the main function, which is a foreground thread. The foreground process must be executed in the program, and the background thread ends after all foreground threads in java, backend threads are mainly used for memory allocation and so on. I know that.