+1 vote
in Other by
I found this book on internet: Threading in C# by Joseph Albahari. I tried its example:

class ThreadTest

{

    static void Main()

    {

        Thread t = new Thread(WriteY);          // Kick off a new thread

        t.Start();                              // running WriteY()

        // Simultaneously, do something on the main thread.

        for (int i = 0; i < 10000000; i++) Console.Write("x");

        Console.ReadLine();

    }

    static void WriteY()

    {

        for (int i = 0; i < 10000000; i++) Console.Write("y");

    }

}

The problem is, when I run this program (I gave higher values in for loops to observe) my CPU utilization sticks to 100%. I didn't want this, I mean, is there anyway to reduce to make this program less CPU intensive? I am just new to multithreading concept so I thought I should ask in advance.

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Multi-threading can improve your application if you can use multiple resources at the same time. For instance, if you have multiple core's, or multiple CPU's, I believe that the above example should perform better.

Or, if you have a thread that uses the CPU, and another thread that simultaneously uses the disk for instance, it also will perform better if you use multi-threading.

If however, you have one single CPU or one single core, the example above won't perform better. It will perform even worse.
...