同期関数の非同期化
8203 ワード
まず、非同期(Asynchronous)とマルチスレッド(Multi-threading)の違いについて説明します.非同期は同期に対して一般的に非ブロッキングを意味します.具体的にはマルチスレッドで実現されていますが、マルチスレッドを心配しなくても良いというメリットがあります.クリーンなインターフェースを実装して呼び出します.もちろん、linuxとjavascriptの環境では、非同期は一般的にイベント駆動機構/Event loopを理解する必要があります.ここで話しているのは主にNETです.同期関数の非同期化には多くの利点があるが、非同期化にはオーバーヘッドがある.具体的には、次の場合は非同期化に適しています.時間がかかるI/O操作、例えば、ファイルI/Oを読んで、ネットワークリソースネットワークI/Oにアクセスします. 時間がかかるCPUの動作:Task.Factoryなどのマルチスレッドと連携して新しいスレッドを起動し、スレッド内で非同期アクセスを行うことを推奨する .
例えば、ここでは同期方法Processがあります.
Event loopを理解するのキーワードで改造します.
例えば、ここでは同期方法Processがあります.
public class Test
{
public int Process(int a)
{
System.Threading.Thread.Sleep(3000);
return a + 1;
}
}
この同期方法を非同期方法に変換します.namespace ConsoleApplication6
{
public class Test
{
public int Process(int a)
{
System.Threading.Thread.Sleep(3000);
return a + 1;
}
public void ProcessAsync(int a, Action<int> callBackAction)
{
Func<int> func = () =>
{
System.Threading.Thread.Sleep(3000);
return a + 1;
};
func.BeginInvoke((ar) =>
{
var result = func.EndInvoke(ar);
callBackAction.Invoke(result);
},
null);
}
}
}
テスト方法:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
var tester = new Test();
Console.WriteLine("Sync start");
var result1 = tester.Process(1);
Console.WriteLine("Sync finish: " + result1);
Console.WriteLine("Async start");
tester.ProcessAsync(2, (r) =>
{
Console.WriteLine("Async finish: " + r + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
});
Console.WriteLine("Async continue" + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
Console.Read();
}
}
}
出力結果:Sync start
(wait 3 seconds)
Sync finish: 2
Async start
Async continue
(wait 3 seconds)
Async finish: 3
いくつかの操作を追加すると、スレッドがいくつか起きてみました.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
var tester = new Test();
Console.WriteLine("Sync start");
var result1 = tester.Process(1);
Console.WriteLine("Sync finish: " + result1);
Console.WriteLine("Async start");
tester.ProcessAsync(2, (r) =>
{
Console.WriteLine("Async finish: " + r + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
});
Console.WriteLine("Async continue" + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Async start");
tester.ProcessAsync(2, (r) =>
{
Console.WriteLine("Async finish: " + r + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
});
Console.WriteLine("Async continue" + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Async start");
tester.ProcessAsync(2, (r) =>
{
Console.WriteLine("Async finish: " + r + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
});
Console.WriteLine("Async continue" + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Async start");
tester.ProcessAsync(2, (r) =>
{
Console.WriteLine("Async finish: " + r + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
});
Console.WriteLine("Async continue" + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
Console.Read();
}
}
}
出力:Sync start
Sync finish: 2
Async start
Async continue thread id: 9
Async start
Async continue thread id: 9
Async start
Async continue thread id: 9
Async start
Async continue thread id: 9
Async finish: 3 thread id: 13
Async finish: 3 thread id: 11
Async finish: 3 thread id: 10
Async finish: 3 thread id: 12
説明の裏にマルチスレッドが使われています.Event loopを理解するのキーワードで改造します.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
public class Test
{
public void ProcessAsync(int a, Action<int> callBackAction)
{
Func<int> func = () =>
{
System.Threading.Thread.Sleep(3000);
return a + 1;
};
func.BeginInvoke((ar) =>
{
var result = func.EndInvoke(ar);
callBackAction.Invoke(result);
},
null);
}
public Task<int> TaskProcessAsync(Action<int> callBackAction)
{
var tcs = new TaskCompletionSource<int>();
ProcessAsync(3, (r) =>
{
callBackAction.Invoke(r);
});
return tcs.Task;
}
public async Task<int> QueryProcess(Action<int> callBackAction)
{
int i = await TaskProcessAsync(callBackAction);
return i;
}
}
public class Class1
{
public async Task<int> Test()
{
var tester = new Test();
var i = await tester.QueryProcess((r) =>
{
Console.WriteLine("await Async finish: " + r + " thread id: " +
System.Threading.Thread.CurrentThread.ManagedThreadId);
});
return i;
}
}
class Program
{
static void Main(string[] args)
{
var t = new Class1();
t.Test();
Console.Read();
}
}
}