CSharp · Console
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
public class Program
{
public static async void Thread1()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Thread1 " + i);
await MyFunc();
}
}
public static async void Thread2()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Thread2 " + i);
await MyFunc();
}
}
public static async Task Task1()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Task1 " + i);
await MyFunc();
}
}
public static async Task Task2()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Task2 " + i);
await MyFunc();
}
}
public static async Task MyFunc()
{
}
public static void TestWithThreads()
{
var thread1 = new Thread(Thread1);
var thread2 = new Thread(Thread2);
thread1.Start();
thread2.Start();
while (thread1.IsAlive || thread2.IsAlive)
{
Thread.Sleep(1000);
}
}
public static void TestWithTasks()
{
Task.WaitAll
(
new[]
{
Task1(),
Task2()
}
);
}
public static void Main()
{
TestWithThreads();
TestWithTasks();
}
}