Topics - Runtime Modeling

I have two secondary threads that execute the WriteX method as described below:

    static void Main(string[] args)
    {

        ThreadStart one = new ThreadStart(WriteX);
        Thread startOne = new Thread(one);
        ThreadStart two = new ThreadStart(WriteX);
        Thread startTwo = new Thread(two);
        startOne.Start();
        startTwo.Start();
        Console.ReadKey(true);
    }

    public static void WriteX()
    {
        for (int i = 1; i <= 3; i++)
        {
            Console.WriteLine("Hello");
            Thread.Sleep(1000);
        }
    }

      

(1) How can I find the time (in milliseconds) using "startOne" and "startTwo" to complete my task?

(2) I started Thread (Start ()). Shouldn't the thread be stopped on success or will it be handled by the main thread (Main () in this case)?

(3) How can I print a message, say startOne executes the WriteX () method and startTwo executes the WriteX () method?

+2


source to share


3 answers


In every serious logging framework, you can include the thread ID or thread name in the output message. If you just want to use the console, you may need to manually add the current thread ID / name to your post.

Same:

public static void WriteX()
{
    var sw = Stopwatch.StartNew();
    for (int i = 1; i <= 3; i++)
    {
        Console.WriteLine("Hello");
        Thread.Sleep(1000);
    }
    sw.Stop();
    Console.WriteLine("{0} {1}", 
                      Thread.CurrentThread.ManagedThreadId, 
                      sw.Elapsed);
}

      



To name a stream, simply use the Name property on the stream instance:

ThreadStart one = new ThreadStart(WriteX);
Thread startOne = new Thread(one);
startOne.Name = "StartOne";

//...

Console.WriteLine("{0} {1}", Thread.CurrentThread.Name, sw.Elapsed);

      

Also, when the method that you pass as a constructor argument ThreadStart

ends, the stream ends automatically. Use this method Join

if you want to wait for the end of the stream in the main.

+1


source


  • Wrap WriteX()

    in something that stores the current time before and after (or just does it at the beginning and end WriteX()

    ) and prints the difference at the end.

  • When the method passed to ThreadStart

    ends, the thread completes itself (and cleans itself)

  • Wrap again WriteX()

    with logging. I suggest looking at the source for ThreadStart

    to see how it is implemented and how you can extend and port it.



If you have no idea what I'm talking about, this article might help.

0


source


  • It depends on whether you want to get the sum of the execution time of two independent tasks or the largest one. In the first case, you need to measure the required time in WriteX. In the second case, before startOne and stop measuring when both have finished. Use Stopwatch

    in both cases.

  • Use Thread.Join

    to wait for both threads to complete.

  • Use parameterized launch Thread.Start(object)

    and pass the task name there.

0


source







All Articles