Skip to content

Commit 29cbc2e

Browse files
committed
多线程练习、多线程安全
1 parent 9f2d954 commit 29cbc2e

21 files changed

Lines changed: 492 additions & 1 deletion

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32421.90
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCoreConcurrentTest", "EFCoreConcurrentTest\EFCoreConcurrentTest.csproj", "{43970EFA-A428-456F-9530-44AE0462C495}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{43970EFA-A428-456F-9530-44AE0462C495}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{43970EFA-A428-456F-9530-44AE0462C495}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{43970EFA-A428-456F-9530-44AE0462C495}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{43970EFA-A428-456F-9530-44AE0462C495}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {3E75F495-9E53-43AF-B066-EEBBAC3B9460}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Blog
2+
{
3+
public int BlogId { get; set; }
4+
public string Url { get; set; }
5+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace EFCoreConcurrentTest.Controllers
5+
{
6+
[Route("api/[controller]")]
7+
[ApiController]
8+
public class BlogController : ControllerBase
9+
{
10+
private static readonly object _lock = new object();
11+
private readonly EDbContext _db;
12+
13+
public BlogController(EDbContext db)
14+
{
15+
_db = db;
16+
}
17+
18+
[HttpGet]
19+
[Route("delete")]
20+
public void Delete()
21+
{
22+
var blogs = _db.Blog.Where(t => t.Url == "set1" || t.Url == "set2").ToList();
23+
_db.Blog.RemoveRange(blogs);
24+
_db.SaveChanges();
25+
}
26+
27+
[HttpGet]
28+
[Route("set1")]
29+
public async Task Set1()
30+
{
31+
lock (_lock) { }
32+
Blog blog = new Blog() { Url = "set1" };
33+
_db.Add(blog);
34+
35+
await Task.Delay(5000);
36+
37+
_db.SaveChanges();
38+
}
39+
40+
[HttpGet]
41+
[Route("set2")]
42+
public void Set2()
43+
{
44+
lock (_lock)
45+
{
46+
Blog? blog1 = _db.Blog.Where(t => t.Url == "set1").FirstOrDefault();
47+
if (blog1 != null) return;
48+
Blog blog = new Blog() { Url = "set2" };
49+
_db.Add(blog);
50+
_db.SaveChanges();
51+
}
52+
}
53+
54+
[HttpGet]
55+
[Route("set3")]
56+
public void Set3()
57+
{
58+
lock (_lock)
59+
{
60+
Task.Delay(5000).Wait();
61+
Blog blog = new Blog() { Url = "set1" };
62+
_db.Add(blog);
63+
_db.SaveChanges();
64+
}
65+
}
66+
}
67+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
4+
public class EDbContext : DbContext
5+
{
6+
public virtual DbSet<Blog> Blog { get; set; }
7+
8+
public EDbContext(DbContextOptions<EDbContext> options) : base(options) { }
9+
10+
protected override void OnConfiguring(DbContextOptionsBuilder options)
11+
{
12+
base.OnConfiguring(options);
13+
}
14+
15+
protected override void OnModelCreating(ModelBuilder builder)
16+
{
17+
base.OnModelCreating(builder);
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
<PrivateAssets>all</PrivateAssets>
14+
</PackageReference>
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.4" />
16+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
5+
<Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath>
6+
</PropertyGroup>
7+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
var configuration = builder.Configuration;
6+
7+
// Add services to the container.
8+
9+
builder.Services.AddControllers();
10+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
11+
builder.Services.AddEndpointsApiExplorer();
12+
builder.Services.AddSwaggerGen();
13+
builder.Services.AddDbContext<EDbContext>(options => {
14+
options.UseSqlServer(configuration["ConnectionStrings:SqlServer"]);
15+
});
16+
17+
var app = builder.Build();
18+
19+
// Configure the HTTP request pipeline.
20+
if (app.Environment.IsDevelopment())
21+
{
22+
app.UseSwagger();
23+
app.UseSwaggerUI();
24+
}
25+
26+
app.UseHttpsRedirection();
27+
28+
app.UseAuthorization();
29+
30+
app.MapControllers();
31+
32+
app.Run();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:17025",
8+
"sslPort": 44324
9+
}
10+
},
11+
"profiles": {
12+
"EFCoreConcurrentTest": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "https://localhost:7074;http://localhost:5074",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"IIS Express": {
23+
"commandName": "IISExpress",
24+
"launchBrowser": true,
25+
"launchUrl": "swagger",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
}
30+
}
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*",
9+
"ConnectionStrings": {
10+
"SqlServer": "server=localhost;database=efcore;uid=sa;pwd=Qwe123456;",
11+
"MySql": "server=localhost;port=3306;database=efcore;user=root;password=123456;charset=utf8mb4;"
12+
}
13+
}

0 commit comments

Comments
 (0)