-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using Exercise; | ||
|
||
namespace Concurrent | ||
{ | ||
public class ConPrimeNumbers : PrimeNumbers | ||
{ | ||
public ConPrimeNumbers() | ||
{ | ||
} | ||
/// <summary> | ||
/// This method | ||
/// </summary> | ||
/// <param name="m"> is the minimum number</param> | ||
/// <param name="M"> is the maximum number</param> | ||
/// <param name="nt"> is the number of threads. For simplicity assume two.</param> | ||
public void runConcurrent(int m, int M) | ||
{ | ||
// Todo 1: Create two threads, define their segments and start them. Join them all to have all the work done. | ||
} | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
|
||
namespace Exercise | ||
{ | ||
public class PrimeNumbers | ||
{ | ||
public PrimeNumbers() { } | ||
|
||
public static void printPrimes(int lower, int upper) | ||
{ | ||
Boolean isPrime = true; | ||
|
||
if (lower > upper) | ||
{ | ||
Console.WriteLine("invalid inputs"); | ||
return; | ||
} | ||
|
||
for (int n = lower; n <= upper; n++) | ||
{ | ||
if (n % 1000 == 0) // This condition fakes an IO operation. | ||
Thread.Sleep(100); | ||
|
||
isPrime = true; // assume n is a prime number | ||
|
||
for (int i = 2; i < n && isPrime; i++) | ||
if (n % i == 0) | ||
isPrime = false; // our assumption was not correct | ||
|
||
if (isPrime) | ||
Console.WriteLine("{0}",n); // report prime number if our assumption was correct | ||
} | ||
|
||
} | ||
|
||
public void runSequential(int m, int M) | ||
{ | ||
Stopwatch sw = new Stopwatch(); | ||
sw.Start(); | ||
PrimeNumbers.printPrimes(m, M); | ||
sw.Stop(); | ||
|
||
Console.WriteLine("Time for sequential version is {0} msec,", sw.ElapsedMilliseconds); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using Exercise; | ||
//using Concurrent; | ||
using Solution; | ||
|
||
/// <summary> | ||
/// This example implements a concurrent version of finding and printing prime-numbers between two given numbers. | ||
/// </summary> | ||
namespace Program | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
|
||
int min = 5, max = 100000; | ||
|
||
ConPrimeNumbers pn = new ConPrimeNumbers(); | ||
|
||
pn.runSequential(min, max); | ||
|
||
Thread.Sleep(5000); | ||
|
||
pn.runConcurrent(min, max); | ||
|
||
|
||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using Exercise; | ||
|
||
/// <summary> | ||
/// This example implements a concurrent version of finding and printing prime-numbers between two numbers. | ||
/// </summary> | ||
|
||
namespace Solution | ||
{ | ||
public class ConPrimeNumbers: PrimeNumbers | ||
{ | ||
/// <summary> | ||
/// This method | ||
/// </summary> | ||
/// <param name="m"> is the minimum number</param> | ||
/// <param name="M"> is the maximum number</param> | ||
/// <param name="nt"> is the number of threads. For simplicity assume two.</param> | ||
public void runConcurrent(int m, int M) | ||
{ | ||
int nt = 2; // number of devisions | ||
|
||
// Create nt number of threads, define their segments and start them. Join them all to have all the work done. | ||
Stopwatch sw = new Stopwatch(); | ||
|
||
int numTs = nt; | ||
int s = (M - m) / nt; | ||
|
||
Thread[] ts = new Thread[numTs]; | ||
// this loop divides numbers between m up to M to nt segments and each segment is given to a separate thread | ||
for (int i = 0; i < numTs; i++) | ||
{ | ||
int l = m + s * i; | ||
int u = 0; | ||
if (i == numTs - 1) | ||
u = M; | ||
else | ||
u = m + s * (i + 1); | ||
|
||
ts[i] = new Thread(() => PrimeNumbers.printPrimes(l, u)); | ||
} | ||
|
||
sw.Start(); | ||
for (int i = 0; i < numTs; i++) | ||
ts[i].Start(); | ||
|
||
// Here, the main thread can be busy with something else | ||
|
||
for (int i = 0; i < numTs; i++) | ||
ts[i].Join(); | ||
|
||
sw.Stop(); | ||
Console.WriteLine("Time for concurrent version with {0} threads is {1} msec,", nt, sw.ElapsedMilliseconds); | ||
} | ||
|
||
} | ||
} |