This commit is contained in:
2025-02-12 17:05:52 +03:00
commit 0b30f2e1b6
8 changed files with 200 additions and 0 deletions

25
.dockerignore Normal file
View File

@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

16
Threading.sln Normal file
View File

@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Threading", "Threading\Threading.csproj", "{C2139401-FCF7-45F0-BF27-443572B69D19}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C2139401-FCF7-45F0-BF27-443572B69D19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C2139401-FCF7-45F0-BF27-443572B69D19}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2139401-FCF7-45F0-BF27-443572B69D19}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2139401-FCF7-45F0-BF27-443572B69D19}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

21
Threading/Dockerfile Normal file
View File

@@ -0,0 +1,21 @@
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
USER $APP_UID
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Threading/Threading.csproj", "Threading/"]
RUN dotnet restore "Threading/Threading.csproj"
COPY . .
WORKDIR "/src/Threading"
RUN dotnet build "Threading.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "Threading.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Threading.dll"]

9
Threading/Program.cs Normal file
View File

@@ -0,0 +1,9 @@
// See https://aka.ms/new-console-template for more information
using Threading;
//Console.WriteLine("Hello, World!");
//await WaitThread.RunTaskBlocking();
await ShaRunner.RunMultiple(10000);

39
Threading/SHA256.cs Normal file
View File

@@ -0,0 +1,39 @@
using System.Security.Cryptography;
using System.Text;
namespace Threading;
public static class ShaRunner
{
public static async Task RunMultiple(int count)
{
List<Task<string>> tasks = new List<Task<string>>();
for (int i = 0; i < count; i++)
{
var i1 = i;
tasks.Add(Task.Run(() => new SHA256ExampleBuggy().Calculate(i1)));
}
var results = await Task.WhenAll(tasks);
Console.WriteLine($"Completed: {string.Join("\n", results)}");
}
}
public class SHA256ExampleBuggy
{
private static readonly SHA256 _hash = SHA256.Create();
public string Calculate(int n)
{
string str = (n * 1000).ToString();
Span<byte> data = stackalloc byte[Encoding.UTF8.GetByteCount(str)];
Encoding.UTF8.GetBytes(str, data);
Span<byte> buffer = stackalloc byte[32];
_hash.TryComputeHash(data, buffer, out _);
return Convert.ToBase64String(buffer);
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>
</Project>

68
Threading/WaitThread.cs Normal file
View File

@@ -0,0 +1,68 @@
namespace Threading;
public class WaitThread
{
public static void RunThread()
{
Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} started: ");
var thr = new Thread(() =>
{
Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} started:");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(1));
Console.WriteLine($"Number:{i}");
}
Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} ended");
});
thr.Start();
//thr.Join();
Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} ended.");
}
public static async Task RunTaskNonBlocking()
{
Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} started: ");
await Task.Run(async () =>
{
Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} started:");
for (int i = 0; i < 10; i++)
{
await Task.Delay(TimeSpan.FromSeconds(1));
Console.WriteLine($"Number:{i}");
}
Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} ended");
});
//thr.Join();
Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} ended.");
}
public static async Task RunTaskBlocking()
{
Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} started: ");
await Task.Run(async () =>
{
Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} started:");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(1));
//await Sleep();
Console.WriteLine($"Number:{i}");
}
Console.WriteLine($"Sleeping thread {Thread.CurrentThread.ManagedThreadId} ended");
});
Console.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId} ended.");
}
public static async Task Sleep()
{
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}