Java attack C # -- multithreading of syntax,
In the previous chapter, I will explain some of the unique syntax points of c #. I believe you can also see some of the magic POINTS OF C. This chapter focuses on multithreading. No matter whether C # or JAVA is used in the development process, multi-threaded programming is more or less used. Of course, I can't explain the multi-thread knowledge comprehensively. Here I will only talk about some of the things I often use. If you are interested, I hope to take the initiative to find the information.
It is no stranger to those who believe in JAVA for the Thread class. At one point, C # is similar to JAVA. However, there are some differences in usage. First of all, we must understand that C #'s multi-threaded functions are generally named under empty System. Threading. What is thread and process. I will not talk much about it. I believe everyone understands. I still like it. Let's take a look at the code for learning.
C #:
1 class Program 2 {3 static void Main (string [] args) 4 {5 Thread myThread1 = new Thread () => 6 {7 Console. writeLine ("this is a lambda expression creation Thread"); 8}); 9 10 Thread myThread2 = new Thread (threadstarqp); 11 12 Thread myThread3 = new Thread (object obj) => 13 {14 Console. writeLine ("this is a lambda expression that creates a thread parameter with parameters:" + obj. toString (); 15}); 16 17 myThread1.Start (); 18 myThread2.Start (); 19 myThread3.Start ("aomi"); 20 21} 22 23 public static void threadstaribd () 24 {25 Console. writeLine ("this is a method to create a thread"); 26} 27 28 29}
The author creates three thread codes above. MyThread1 and myThread2 are the same threads. It is a thread without parameters. MyThread3 is a thread with parameters. Whether there are parameters or not. The usage is basically the same. If there is a parameter, you just need to input a parameter to the Start method. We can see that there is a big difference between JAVA and JAVA. The parameter of the JAVA Thread constructor is passed into the Runnable interface. Unfortunately, C # is not. The following code.
C #:
public Thread(ParameterizedThreadStart start);public Thread(ThreadStart start);public Thread(ParameterizedThreadStart start, int maxStackSize);public Thread(ThreadStart start, int maxStackSize);
This code is in the source code. Press F12 to check the Thread class structure. We can see four constructors. We often use only two threads: Thread (ParameterizedThreadStart start) and Thread (ThreadStart start ). I thought ThreadStart would be the same as the Runnable interface of JAVA. You can only press F12 to check the structure. The following code
C #:
[ComVisible(true)]public delegate void ThreadStart();
C #:
[ComVisible(false)]public delegate void ParameterizedThreadStart(object obj);
We can clearly see a keyword delegate. I believe anyone who has read the previous chapter knows that he is related to the incident. No error. Is to define a delegate type. For future use in the transfer method. So we can understand that the parameters of the constructor of the Thread class can only be passed into the method. So I believe that the definition of the three threads above is significant.
MyThread1 thread: I use a lambda expression to create a thread. What is lambda expression in the previous chapter.
MyThread2 thread: defines a method to implement multithreading. This method can be a static method or an object method. The function of static keyword is the same as that of JAVA.
MyThread3 thread: implement multiple threads with parameters.
The above is just about the use of the Thread class. For some methods in the Thread class object, I will not introduce them much. As large as the Thread class in JAVA. For example, the Interrupt method. Read this information by readers.
When talking about multithreading pools, I believe everyone knows what it is. C # uses the ThreadPool class. It's a pity that I don't have much chance to use the multi-thread pool on the JAVA side. Therefore, it is unclear the differences between the ThreadPoolExecutor class and the Executors class in JAVA and the ThreadPool class in C. But if you are interested, you can take a look. Here I will talk about the use of the ThreadPool class. When there are more ThreadPool classes, there is one thing as a tool class. See the following code.
C #:
Class Program {static void Main (string [] args) {ThreadPool. queueUserWorkItem (obj) => {Console. writeLine ("this is a lambda expression that creates a thread parameter with parameters:" + obj. toString () ;}); ThreadPool. queueUserWorkItem (WaitCallbackImp);} public static void WaitCallbackImp (object obj) {Console. writeLine ("this is a method to create a thread ");}}
The above code is similar to the above Thread class. However, there is a big difference in nature. The multi-thread pool is used here. In addition, the multi-thread pool settings. As long as there are many methods in ThreadPool. XXXXX, you can set them.
If you use the above Thread class, you will find that sometimes it is difficult to stop a Thread when it is running. However, if you use the Task class, you will find that this difficulty does not exist. I do not know why C # introduces the Task class. It may be because the Thread class and ThreadPool class are too difficult to control. This is not a concern of the author. Let's take a look at how the Task class is used. The Task class is commonly called a Task in Chinese. So there is a saying. Single task or multi-task. Why does the author say this? As follows.
1. single task. Is a Task instance. This is relatively simple. The Code is as follows:
Class Program {static void Main (string [] args) {CancellationTokenSource cts = new CancellationTokenSource (); // used to cancel the Task myTask = new Task (obj) => {Console. writeLine ("this is a single task") ;}, cts); myTask. start (); Console. readKey ();}}
Here I only use the CancellationTokenSource class. This class is used to cancel a task. Let me write an example.
Class Program {static void Main (string [] args) {CancellationTokenSource cts = new CancellationTokenSource (); // used to cancel the Task myTask = new Task (obj) => {Console. writeLine ("this is a single task"); Thread. sleep (20000); // Let him Sleep for a while}, cts); myTask. start (); if (cts. isCancellationRequested) // determines whether to cancel {cts. cancel ();} Console. readKey ();}}
2. Multi-task. Is to cooperate with instances of multiple Task classes. At this time, someone will do it first. Who then did the problem.
Class Program {static void Main (string [] args) {CancellationTokenSource cts1 = new CancellationTokenSource (); // used to cancel Task myTask1 = new Task (obj) => {Console. writeLine ("this is a single task"); // Thread. sleep (20000); // Let him Sleep for a while}, cts1); CancellationTokenSource cts2 = new CancellationTokenSource (); // used to cancel myTask1.ContinueWith (task) => {Console. writeLine ("myTask2 task") ;}, cts2.Token); myTask1.Start (); // if (cts1.IsCancellationRequested) // determine whether to cancel /// {// cts1.Cancel (); //} Console. readKey ();}}
The above ContinueWith method indicates that after myTask1 is completed, the red code can be continued. Is another Task. In addition to the above instance, there is also a usage. The Code is as follows.
CancellationTokenSource cts3 = new CancellationTokenSource (); // used to cancel a Task in the middle. factory. startNew (obj) => {Console. writeLine ("this is a single Factory task used") ;}, cts3 );
I don't need to talk about it. There is a concept similar to multithreading pool. Note that there are some useful functions in Task. Factory. The ContinueWhenAll method is a good embodiment. And the above ContinueWith has an image. That is, the last task is executed after the task is completed. Use Task. Factory. To learn it. If no prompt is displayed, write the "ask. Factory." above and press Ctrl + J. There are various ways for you to learn.
This chapter mainly describes some of the knowledge about multithreading. I have not explained them in detail. Therefore, we hope that readers can continue to deepen their work based on what I have said.