Summary of C # programming (v) Reflections on multiple lines

Source: Internet
Author: User
Tags resource thread

1. When to use multithreading?

This problem, for system architects, designers, programmers, are the first to face a problem.

When do you use multithreading technology?

In many common situations, multithreading can be used to significantly improve the responsiveness and availability of your applications.

In the previous chapter, we talked about several multi-threaded application cases, and the main application scenarios were also introduced. Don't repeat it here.

Http://www.cnblogs.com/yank/p/3232955.html

2, how to ensure thread safety?

With multiple threads, this is a problem that must be clarified. Only by understanding the impact of multithreading on the structure and the program, you can really use multiple threads, so that it can play its due effect.

Why is it not safe to apply multithreading?

A criterion of thread safety, there is no critical resources between the threads, if there is a critical resource, and no reasonable synchronization mechanism, there will be multiple threads competing for a resource, if multiple threads are in order to get the required resources, will occur deadlock. Deadlock, the thread will be deadlocked with each other, the system is stagnant, if the consequences of serious, then directly lead to system crashes. Common cases are: producer and consumer issues, philosophers dining problems, and so on.

I will make a simplification according to the philosophers dining problem: Two people go to a restaurant to eat, because the resource is tight, only a pair of chopsticks, everyone is hungry, want to eat, and at the same time to grab chopsticks, evenly, two people each grabbed a chopsticks, only use a pair of chopsticks to eat. At this time you will say, I can hold the hand to eat, hehe. If it is just out of the pot dumplings, afraid you can't catch up. Two people can only looked at each other, Kango, is not eat. If there's a yinianbanzai, you're starving. Ha ha. If we give an agreement, when taking chopsticks, one to get a pair, and after eating to exchange to each other. Then two people happily ate. Chopsticks are critical resources. Of course, when two people are deadlocked, external intervention is possible, making two people have food to eat. For example: forcing one side to free out chopsticks, then the other side of the rice to eat. After eating chopsticks free out, then another person also has rice to eat.

As long as we handle the critical resource problem, the thread safety problem is solved.

Multithreading does not necessarily require a good thread synchronization, but if there is a critical resource, it must be thread-synchronized.

3. How can I write code that is thread safe?

In OOP, programmers use nothing more than variables, objects (properties, methods), types, and so on.

1) variable

Variables include value types and reference types.

Value types are thread-safe, but if they are attributes of an object, the value type is appended to the object, and the object's thread security needs to be referenced.

Reference type, it should be noted that for reference objects, he includes references and object instances, which need to be accessed by reference to their storage location, for

Private Object o = new Object (),

Can actually be decomposed into two sentences:

Private Object o;

o = new Object ();

Where Private object o is a reference that defines an object, that is, a pointer to a Record object instance, not the object itself. This reference is stored on the stack, occupies 4 bytes; when no o = new Object () is used, the value of the reference itself is null, that is, it does not point to any valid position; when o = new Object (), the space is allocated to the object instance in the managed heap based on the size of the object. The pointer position of the instance is then assigned to the preceding reference. This completes the instantiation of an object.

The security of a reference type is that it can be referred to by multiple references while pointing to a memory address. If one reference is modified, the other is modified.

Using System;
    
Namespace Variablesample
{
    class program
    {
        static void Main (string[] args)
        {
            Box B1 = new Box () ;
            B1. Name = "BigBox";
    
            Console.WriteLine ("Create box B1.");
            Console.WriteLine ("Box:b1 ' Name is {0}.", B1. Name);
            Console.WriteLine ("Create same box B2.");
    
    
    
            Box b2 = B1;
            B2. Name = "Littlebox";
    
            Console.WriteLine ("Box:b2 ' s Name is {0}.", B2. Name);
            Console.WriteLine ("Box:b1 ' s Name is {0}.", B1. Name);
    
            Console.readkey ();
        }
    
    <summary>
    ///Box
    ///</summary> Public
    class box
    {
        ///<summary>
        ///name
        ///</summary> public
        string name
        {get
            ;
            Set;}}}
    

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.