Interesting multithreaded Programming (3)--How is the thread internal?

Source: Internet
Author: User
Programming | multithreading
Take a look at the following two examples of running results:
TestThread.cs
Using system;using system.threading;public class test{ static int count=0; static void Main () { ThreadStart job = new ThreadStart (threadjob); Thread thread = new thread (job); Thread. Start (); for (int i=0 i < 5; i++) { count++; } Thread. Join (); Console.WriteLine ("Final count: {0}", count); } static void Threadjob () {for (int i=0; i < 5; i++) { count++; } }}
InnerDataThread.cs
Using system;using system.threading;public class test{static int count=0;        static void Main () {ThreadStart job = new ThreadStart (threadjob);        Thread thread = new thread (job); Thread.                Start ();            for (int i=0 i < 5; i++) {int tmp = count;            Console.WriteLine ("Read count={0}", TMP);            Thread.Sleep (50);            tmp++;            Console.WriteLine ("incremented tmp to {0}", TMP);            Thread.Sleep (20);            Count = tmp;            Console.WriteLine ("Written count={0}", TMP);        Thread.Sleep (30); } thread.        Join ();    Console.WriteLine ("Final count: {0}", count);            static void Threadjob () {for (int i=0; i < 5; i++) {int tmp = count;            Console.WriteLine ("\t\t\t\tread count={0}", TMP);            Thread.Sleep (20);            tmp++;       Console.WriteLine ("\t\t\t\tincremented tmp to {0}", TMP);     Thread.Sleep (10);            Count = tmp;            Console.WriteLine ("\t\t\t\twritten count={0}", TMP);        Thread.Sleep (40); }    }}
  
  
 Read count=0 read count=0 incremented tmp to 1                                Written count=1incremented tmp to 1Written count=1 Read count=1                                incremented tmp to 2Read count=1 written count=2                                Read count=2incremented tmp to 2 incremented TMP to 3Written count=2                                Written count=3read count=3 Read count=3incremented tmp to 4                                incremented TMP to 4 written Count=4written count=4                                Read count=4read count=4 incremented tmp to 5 Written count=5incremented tmp to 5Written count=5read count=5incremented tmp to 6Written count=6final count:6 

Compare the following example:

UseMonitor.Enter/Exit
MonitorThread.cs

Using System;
Using System.Threading;

public class Test
{
static int count=0;
Static ReadOnly Object Countlock = new Object ();

static void Main ()
{
ThreadStart job = new ThreadStart (threadjob);
Thread thread = new thread (job);
Thread. Start ();

for (int i=0 i < 5; i++)
{
Monitor.Enter (Countlock);
int tmp = count;
Console.WriteLine ("Read count={0}", TMP);
Thread.Sleep (50);
tmp++;
Console.WriteLine ("incremented tmp to {0}", TMP);
Thread.Sleep (20);
Count = tmp;
Console.WriteLine ("Written count={0}", TMP);
Monitor.Exit (Countlock);
Thread.Sleep (30);
}

Thread. Join ();
Console.WriteLine ("Final count: {0}", count);
}

static void Threadjob ()
{
for (int i=0 i < 5; i++)
{
Monitor.Enter (Countlock);
int tmp = count;
Console.WriteLine ("\t\t\t\tread count={0}", TMP);
Thread.Sleep (20);
tmp++;
Console.WriteLine ("\t\t\t\tincremented tmp to {0}", TMP);
Thread.Sleep (10);
Count = tmp;
Console.WriteLine ("\t\t\t\twritten count={0}", TMP);
Monitor.Exit (Countlock);
Thread.Sleep (40);
}
}
}

The results are not the same as the previous example InnerDataThread.cs, because of the use of monitor.

Read count=0incremented tmp to 1Written count=1                                Read count=1                                incremented tmp to 2                                written Count=2read count=2 incremented tmp to 3Written count=3                                Read count=3                                incremented tmp to 4                                written count=4read count=4incremented TMP to 5Written count=5                                Read count=5                                incremented tmp to 6                                written count=6read count=6incremented tmp to 7Writt En count=7                                Read count=7                                incremented tmp to 8                                written count=8read count=8incremented tmp to 9Written count=9                                Read count=9                                incremented tmp to                                written count=10final Count:10

The following uses lock to lock the thread:
LockThread.cs
Using System;
Using System.Threading;

public class Test
{
static int count=0;
Static ReadOnly Object Countlock = new Object ();

static void Main ()
{
ThreadStart job = new ThreadStart (threadjob);
Thread thread = new thread (job);
Thread. Start ();

for (int i=0 i < 5; i++)
{
Lock (Countlock)
{
int tmp = count;
Console.WriteLine ("Read count={0}", TMP);
Thread.Sleep (50);
tmp++;
Console.WriteLine ("incremented tmp to {0}", TMP);
Thread.Sleep (20);
Count = tmp;
Console.WriteLine ("Written count={0}", TMP);
}
Thread.Sleep (30);
}

Thread. Join ();
Console.WriteLine ("Final count: {0}", count);
}

static void Threadjob ()
{
for (int i=0 i < 5; i++)
{
Lock (Countlock)
{
int tmp = count;
Console.WriteLine ("\t\t\t\tread count={0}", TMP);
Thread.Sleep (20);
tmp++;
Console.WriteLine ("\t\t\t\tincremented tmp to {0}", TMP);
if (Count < 100)
throw new Exception ();
Thread.Sleep (10);
Count = tmp;
Console.WriteLine ("\t\t\t\twritten count={0}", TMP);
}
Thread.Sleep (40);
}
}
}

How did it turn out? Compare it with MonitorThread.cs and think again.



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.