<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp_by_API%2FSystem.Threading%2FMonitor</id>
		<title>Csharp/C Sharp by API/System.Threading/Monitor - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp_by_API%2FSystem.Threading%2FMonitor"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp_by_API/System.Threading/Monitor&amp;action=history"/>
		<updated>2026-04-29T19:24:52Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp_by_API/System.Threading/Monitor&amp;diff=3996&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp_by_API/System.Threading/Monitor&amp;diff=3996&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:35Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp_by_API/System.Threading/Monitor&amp;diff=3997&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp_by_API/System.Threading/Monitor&amp;diff=3997&amp;oldid=prev"/>
				<updated>2010-05-26T12:08:47Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Monitor.Enter==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;  &lt;br /&gt;
using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
&lt;br /&gt;
public class EnterExit {&lt;br /&gt;
    private int result = 0;&lt;br /&gt;
    public void NonCriticalSection() {&lt;br /&gt;
        Console.WriteLine(&amp;quot;Entered Thread &amp;quot; + Thread.CurrentThread.GetHashCode());&lt;br /&gt;
        for (int i = 1; i &amp;lt;= 5; i++) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Result = &amp;quot; + result++ + &amp;quot;  ThreadID &amp;quot;&lt;br /&gt;
                              + Thread.CurrentThread.GetHashCode());&lt;br /&gt;
            Thread.Sleep(1000);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Exiting Thread &amp;quot; + Thread.CurrentThread.GetHashCode());&lt;br /&gt;
    }&lt;br /&gt;
    public void CriticalSection() {&lt;br /&gt;
        Monitor.Enter(this);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Entered Thread &amp;quot; + Thread.CurrentThread.GetHashCode());&lt;br /&gt;
        for (int i = 1; i &amp;lt;= 5; i++) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Result = &amp;quot; + result++ + &amp;quot;  ThreadID &amp;quot; +&lt;br /&gt;
                              Thread.CurrentThread.GetHashCode());&lt;br /&gt;
            Thread.Sleep(1000);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;Exiting Thread &amp;quot; + Thread.CurrentThread.GetHashCode());&lt;br /&gt;
        Monitor.Exit(this);&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main(String[] args) {&lt;br /&gt;
        EnterExit e = new EnterExit();&lt;br /&gt;
        Thread nt1 = new Thread(new ThreadStart(e.NonCriticalSection));&lt;br /&gt;
        nt1.Start();&lt;br /&gt;
        Thread nt2 = new Thread(new ThreadStart(e.NonCriticalSection));&lt;br /&gt;
        nt2.Start();&lt;br /&gt;
        Thread ct1 = new Thread(new ThreadStart(e.CriticalSection));&lt;br /&gt;
        ct1.Start();&lt;br /&gt;
        Thread ct2 = new Thread(new ThreadStart(e.CriticalSection));&lt;br /&gt;
        ct2.Start();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Monitor.Exit==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
Code revised from Book published by &lt;br /&gt;
(C) Copyright 1992-2006 by Deitel &amp;amp;amp; Associates, Inc. and&lt;br /&gt;
Pearson Education, Inc. All Rights Reserved.  &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
public class SynchronizedBuffer&lt;br /&gt;
{&lt;br /&gt;
   private int buffer = -1; &lt;br /&gt;
   private int occupiedBufferCount = 0;  &lt;br /&gt;
   public int Buffer&lt;br /&gt;
   {      &lt;br /&gt;
      get&lt;br /&gt;
      { &lt;br /&gt;
         Monitor.Enter( this );&lt;br /&gt;
         if ( occupiedBufferCount == 0 )&lt;br /&gt;
         {&lt;br /&gt;
            Console.WriteLine(Thread.CurrentThread.Name + &amp;quot; tries to read.&amp;quot; );&lt;br /&gt;
            DisplayState( &amp;quot;Buffer empty. &amp;quot; +Thread.CurrentThread.Name + &amp;quot; waits.&amp;quot; );&lt;br /&gt;
            Monitor.Wait( this );&lt;br /&gt;
         } &lt;br /&gt;
         --occupiedBufferCount;    &lt;br /&gt;
                              &lt;br /&gt;
         DisplayState( Thread.CurrentThread.Name + &amp;quot; reads &amp;quot; + buffer );&lt;br /&gt;
         Monitor.Pulse( this );&lt;br /&gt;
         int bufferCopy = buffer;&lt;br /&gt;
         Monitor.Exit( this );&lt;br /&gt;
         return bufferCopy;&lt;br /&gt;
      }&lt;br /&gt;
      set&lt;br /&gt;
      {&lt;br /&gt;
         Monitor.Enter( this );&lt;br /&gt;
         if ( occupiedBufferCount == 1 )&lt;br /&gt;
         {&lt;br /&gt;
            Console.WriteLine(Thread.CurrentThread.Name + &amp;quot; tries to write.&amp;quot; );&lt;br /&gt;
            DisplayState( &amp;quot;Buffer full. &amp;quot; + Thread.CurrentThread.Name + &amp;quot; waits.&amp;quot; );&lt;br /&gt;
            Monitor.Wait( this );&lt;br /&gt;
         }&lt;br /&gt;
         buffer = value;&lt;br /&gt;
         ++occupiedBufferCount;&lt;br /&gt;
         DisplayState( Thread.CurrentThread.Name + &amp;quot; writes &amp;quot; + buffer );&lt;br /&gt;
         Monitor.Pulse( this );&lt;br /&gt;
         Monitor.Exit( this );&lt;br /&gt;
      } &lt;br /&gt;
   }&lt;br /&gt;
   public void DisplayState( string operation )&lt;br /&gt;
   {&lt;br /&gt;
      Console.WriteLine( &amp;quot;{0,-35}{1,-9}{2}\n&amp;quot;,operation, buffer, occupiedBufferCount );&lt;br /&gt;
   }&lt;br /&gt;
   static void Main( string[] args )&lt;br /&gt;
   {&lt;br /&gt;
      SynchronizedBuffer shared = new SynchronizedBuffer();&lt;br /&gt;
      Random random = new Random();&lt;br /&gt;
      Console.WriteLine( &amp;quot;{0,-35}{1,-9}{2}\n&amp;quot;,&amp;quot;Operation&amp;quot;, &amp;quot;Buffer&amp;quot;, &amp;quot;Occupied Count&amp;quot; );&lt;br /&gt;
      shared.DisplayState( &amp;quot;Initial state&amp;quot; );&lt;br /&gt;
      Producer producer = new Producer( shared, random );&lt;br /&gt;
      Consumer consumer = new Consumer( shared, random );&lt;br /&gt;
      Thread producerThread = new Thread( new ThreadStart( producer.Produce ) );&lt;br /&gt;
      producerThread.Name = &amp;quot;Producer&amp;quot;;&lt;br /&gt;
      Thread consumerThread = new Thread( new ThreadStart( consumer.Consume ) );&lt;br /&gt;
      consumerThread.Name = &amp;quot;Consumer&amp;quot;;&lt;br /&gt;
      producerThread.Start();&lt;br /&gt;
      consumerThread.Start();&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
public class Consumer&lt;br /&gt;
{&lt;br /&gt;
   private SynchronizedBuffer sharedLocation;&lt;br /&gt;
   private Random randomSleepTime;&lt;br /&gt;
   public Consumer( SynchronizedBuffer shared, Random random )&lt;br /&gt;
   {&lt;br /&gt;
      sharedLocation = shared;&lt;br /&gt;
      randomSleepTime = random;&lt;br /&gt;
   }&lt;br /&gt;
   public void Consume()&lt;br /&gt;
   {&lt;br /&gt;
      int sum = 0;&lt;br /&gt;
      for ( int count = 1; count &amp;lt;= 10; count++ )&lt;br /&gt;
      {&lt;br /&gt;
         Thread.Sleep( randomSleepTime.Next( 1, 1001 ) );&lt;br /&gt;
         sum += sharedLocation.Buffer;&lt;br /&gt;
      }&lt;br /&gt;
      Console.WriteLine(&amp;quot;{0} read values totaling: {1}.\nTerminating {0}.&amp;quot;,Thread.CurrentThread.Name, sum );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
public class Producer &lt;br /&gt;
{&lt;br /&gt;
   private SynchronizedBuffer sharedLocation;&lt;br /&gt;
   private Random randomSleepTime;&lt;br /&gt;
   public Producer( SynchronizedBuffer shared, Random random )&lt;br /&gt;
   {&lt;br /&gt;
      sharedLocation = shared;&lt;br /&gt;
      randomSleepTime = random;&lt;br /&gt;
   }&lt;br /&gt;
   public void Produce()&lt;br /&gt;
   {&lt;br /&gt;
      for ( int count = 1; count &amp;lt;= 10; count++ ) &lt;br /&gt;
      {&lt;br /&gt;
         Thread.Sleep( randomSleepTime.Next( 1, 1001 ) );&lt;br /&gt;
         sharedLocation.Buffer = count; &lt;br /&gt;
      }&lt;br /&gt;
      Console.WriteLine( &amp;quot;{0} done producing.\nTerminating {0}.&amp;quot;,Thread.CurrentThread.Name );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Monitor.Pulse==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;  &lt;br /&gt;
using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MessageBoard {&lt;br /&gt;
    private String messages = &amp;quot;no messages&amp;quot;;&lt;br /&gt;
    public void Reader() {&lt;br /&gt;
        try {&lt;br /&gt;
            Monitor.Enter(this);&lt;br /&gt;
            if (messages == &amp;quot;no messages&amp;quot;) {&lt;br /&gt;
                Console.WriteLine(&amp;quot;{0} {1}&amp;quot;,Thread.CurrentThread.Name, messages);&lt;br /&gt;
                Console.WriteLine(&amp;quot;{0} waiting&amp;quot;,Thread.CurrentThread.Name);&lt;br /&gt;
                Monitor.Wait(this);&lt;br /&gt;
            }&lt;br /&gt;
            Console.WriteLine(&amp;quot;{0} {1}&amp;quot;,Thread.CurrentThread.Name, messages);&lt;br /&gt;
        } finally {&lt;br /&gt;
            Monitor.Exit(this);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public void Writer() {&lt;br /&gt;
        try {&lt;br /&gt;
            Monitor.Enter(this);&lt;br /&gt;
            messages = &amp;quot;Greetings!&amp;quot;;&lt;br /&gt;
            Console.WriteLine(&amp;quot;{0} Done writing message&amp;quot;,Thread.CurrentThread.Name);&lt;br /&gt;
            Monitor.Pulse(this);&lt;br /&gt;
        } finally {&lt;br /&gt;
            Monitor.Exit(this);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        MessageBoard myMessageBoard = new MessageBoard();&lt;br /&gt;
        Thread reader = new Thread(new ThreadStart(myMessageBoard.Reader));&lt;br /&gt;
        reader.Name = &amp;quot;ReaderThread:&amp;quot;;&lt;br /&gt;
        Thread writer = new Thread(new ThreadStart(myMessageBoard.Writer));&lt;br /&gt;
        writer.Name = &amp;quot;WriterThread:&amp;quot;;&lt;br /&gt;
        reader.Start();&lt;br /&gt;
        writer.Start();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Monitor.TryEnter==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;  &lt;br /&gt;
using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
public class TryEnter {&lt;br /&gt;
    public void CriticalSection() {&lt;br /&gt;
        bool b = Monitor.TryEnter(this, 1000);&lt;br /&gt;
        Console.WriteLine(&amp;quot;Thread &amp;quot; +&lt;br /&gt;
                          Thread.CurrentThread.GetHashCode() +&lt;br /&gt;
                          &amp;quot; TryEnter Value &amp;quot; + b);&lt;br /&gt;
        for (int i = 1; i &amp;lt;= 3; i++) {&lt;br /&gt;
            Thread.Sleep(1000);&lt;br /&gt;
            Console.WriteLine(i + &amp;quot; &amp;quot; +&lt;br /&gt;
                              Thread.CurrentThread.GetHashCode() + &amp;quot; &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        Monitor.Exit(this);&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        TryEnter a = new TryEnter();&lt;br /&gt;
        Thread t1 = new Thread(new ThreadStart(a.CriticalSection));&lt;br /&gt;
        Thread t2 = new Thread(new ThreadStart(a.CriticalSection));&lt;br /&gt;
        t1.Start();&lt;br /&gt;
        t2.Start();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>