Created
May 25, 2018 08:46
-
-
Save Const-me/27bc9607bc844a59ec3e8dd9e4b902bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ThreadsTest | |
{ | |
static class Program | |
{ | |
static readonly object syncRoot = new object(); | |
static readonly int[] array = new int[] { 1,2,3,4,5,6 }; | |
static int i = 0; | |
static void thread() | |
{ | |
lock( syncRoot ) | |
{ | |
for( ; i < array.Length; i++ ) | |
{ | |
Monitor.Pulse( syncRoot ); | |
Monitor.Wait( syncRoot ); | |
Console.WriteLine( "Thread {0}: {1}", Environment.CurrentManagedThreadId, array[ i ] ); | |
} | |
} | |
} | |
static void Main( string[] args ) | |
{ | |
Action act = thread; | |
Task.WhenAll( Task.Run( act ), Task.Run( act ) ).Wait(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment