Created
January 2, 2015 03:18
-
-
Save rafacouto/02b0413bd20935381fae to your computer and use it in GitHub Desktop.
This file contains 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
// compile: mono-csc Threads.cs | |
// execute: mono Threads.exe | |
using System; | |
using System.Threading; | |
namespace Test | |
{ | |
class Threads | |
{ | |
const int THREADS = 2; | |
const int MAX_COUNT = 10000000; | |
volatile static int counter = 0; | |
public static void Run(object nn) { | |
int n = (int)nn; | |
for (int i = 0; i < n; i++) | |
{ | |
counter += 1; | |
} | |
} | |
public static void Main (string[] args) | |
{ | |
Thread[] threads = new Thread[THREADS]; | |
int i; | |
for (i=0; i< THREADS; i++) { | |
threads[i] = new Thread(new ParameterizedThreadStart(Threads.Run)); | |
} | |
for (i=0; i< THREADS; i++) { | |
threads[i].Start(MAX_COUNT/THREADS); | |
} | |
for (i=0; i< THREADS; i++) { | |
threads[i].Join(); | |
} | |
Console.WriteLine("Counter value: {0} Expected: {1}\n", counter, MAX_COUNT); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
La tunda sobre Java es notable ;) http://imgur.com/Z0cnKAC