Question

Does each static call will initiate a new thread?

For E.g.:

class A
{
    public static void displayName()
    {
        Console.WriteLine("myName");
    }

    public static void displayAge()
    {
        Console.WriteLine("myAge");
    }
}


class B
{
    public void Foo()
    {
        A.displayName();
        A.displayAge();           
    }
} 

Will the above calls runs independent of each other? If yes, then, is it something similar to Threading?

Was it helpful?

Solution

Reply to your doubt regarding static call:

But i heard each static call is independent of each other as there is no instance and only static members are involved. is it so? – Sri Kumar 36 mins ago

You can think there is a 'global' instance of your class, and all static methods are held by this instance.

As in your example, you can create a 'user' instance by calling ClassA myA = new ClassA(). Meanwhile, there will be a 'global' instance, which is created by the runtime but invisible to you, and static methods reside in this instance. Static methods behavior as instance methods within this 'global' instance.

Amazingly, in C# there is a static constructor, which will be called when the 'global' instance is initialized by the runtime.

You can test this code:

class A
{
    static A() {
       Console.WriteLine("Creating the global instance of class A....");
    }

    public static void displayName()
    {
        Console.WriteLine("myName");
    }

    public static void displayAge()
    {
        Console.WriteLine("myAge");
    }
}


class B
{
    public void Foo()
    {
        A.displayName();
        A.displayAge();           
    }
}

The output will be:

Creating the global instance of class A....
myName
myAge

Apart from this, static methods have nothing difference from instance methods.

Variables in each static method will have its own scope, and they are independent from one method to another method.

OTHER TIPS

No they will run sequentially on the same thread

No. Static methods are like any other methods unless you invoke them in different threads they will run in same thread from which you have invoked.

It really depends on where do you invoke. Static methods itself does not create any threads and run within.

A static method is a method that does not have any class associated with it. This means that calling a static method does not have to go through any indirection, unlike a virtual method which is resolved based on the object it is called on.

All methods will run on the thread of the invoking method, whether virtual or static. If you want a method to run on a different thread, use the thread pool or some other technique.

One more thing:

i heard each static call is independent of each other

Static method calls are not really independent.

Static method can still access other static fields and properties within the same class, or publicly visible static fields and properties of other classes, which are also global and retained during the lifetime of your app.

Static methods which are changing the state of static fields are not "independent", as you suggested. The only thing you can say for sure that they are always invoked on a single instance (always using the same private data).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top