none
threads static or instance method RRS feed

  • Question

  • I am studying for the 70-483 exam and could not figured out what is the benefit or drawback between starting a thread with a static method compared to an instance method?

    using System;
    using System.Threading;
    
    public class ServerClass
    {
        // The method that will be called when the thread is started.
        public void InstanceMethod()
        {
            Console.WriteLine(
                "ServerClass.InstanceMethod is running on another thread.");
    
            // Pause for a moment to provide a delay to make
            // threads more apparent.
            Thread.Sleep(3000);
            Console.WriteLine(
                "The instance method called by the worker thread has ended.");
        }
    
        public static void StaticMethod()
        {
            Console.WriteLine(
                "ServerClass.StaticMethod is running on another thread.");
    
            // Pause for a moment to provide a delay to make
            // threads more apparent.
            Thread.Sleep(5000);
            Console.WriteLine(
                "The static method called by the worker thread has ended.");
        }
    }
    
    public class Simple
    {
        public static void Main()
        {
            ServerClass serverObject = new ServerClass();
    
            // Create the thread object, passing in the
            // serverObject.InstanceMethod method using a
            // ThreadStart delegate.
            Thread InstanceCaller = new Thread(
                new ThreadStart(serverObject.InstanceMethod));
    
            // Start the thread.
            InstanceCaller.Start();
    
            Console.WriteLine("The Main() thread calls this after "
                + "starting the new InstanceCaller thread.");
    
            // Create the thread object, passing in the
            // serverObject.StaticMethod method using a
            // ThreadStart delegate.
            Thread StaticCaller = new Thread(
                new ThreadStart(ServerClass.StaticMethod));
    
            // Start the thread.
            StaticCaller.Start();
    
            Console.WriteLine("The Main() thread calls this after "
                + "starting the new StaticCaller thread.");
        }
    }
    // The example displays the output like the following:
    //    The Main() thread calls this after starting the new InstanceCaller thread.
    //    The Main() thread calls this after starting the new StaticCaller thread.
    //    ServerClass.StaticMethod is running on another thread.
    //    ServerClass.InstanceMethod is running on another thread.
    //    The instance method called by the worker thread has ended.
    //    The static method called by the worker thread has ended.



    Thursday, November 29, 2018 3:48 PM

Answers

  • You can go either way and it changes nothing as far as threading goes. The only "difference" is that an instance method would require that you first create an instance of the class that contains the method and the method would have access to the instance members. So the instance would stick around until the thread terminated. This would allow you to create multiple threads (one per instance) if you needed that. For example if you were creating a TCP server you would likely have a listener class to handle clients. Each one would need its own thread so you'd use the "client" instance's method to process client requests. Each connection gets its own client and therefore has its own instance.

    With a static method you'd have a single "instance" so if you needed to store data or anything it would have to be static as well or local to the thread method. For a one off thread this would be fine but if you suddenly needed to have multiple threads (e.g. the client example of earlier) you would then be trying to get a static class to act like a per-instance one.

    So, ultimately, the difference is really whether or not you have/need data during the thread's lifetime and whether you need multiple threads using this method or not. Most people tend to just use instance methods in my experience. There really isn't any disadvantage.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, November 29, 2018 4:30 PM
    Moderator

All replies

  • You can go either way and it changes nothing as far as threading goes. The only "difference" is that an instance method would require that you first create an instance of the class that contains the method and the method would have access to the instance members. So the instance would stick around until the thread terminated. This would allow you to create multiple threads (one per instance) if you needed that. For example if you were creating a TCP server you would likely have a listener class to handle clients. Each one would need its own thread so you'd use the "client" instance's method to process client requests. Each connection gets its own client and therefore has its own instance.

    With a static method you'd have a single "instance" so if you needed to store data or anything it would have to be static as well or local to the thread method. For a one off thread this would be fine but if you suddenly needed to have multiple threads (e.g. the client example of earlier) you would then be trying to get a static class to act like a per-instance one.

    So, ultimately, the difference is really whether or not you have/need data during the thread's lifetime and whether you need multiple threads using this method or not. Most people tend to just use instance methods in my experience. There really isn't any disadvantage.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, November 29, 2018 4:30 PM
    Moderator
  • thanks
    Thursday, November 29, 2018 4:38 PM