//synchronous join
//all thread in first group starts simultaneously and processed synchronously
//wait for all thread in first group to finish
//all thread in second group starts simultaneously and processed synchronously
//wait for all thread in second group to finish
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var timer = Stopwatch.StartNew();
Thread[] thread_array = new Thread[5];
for (int i = 0; i < thread_array.Length; i++)
{
thread_array[i] = new Thread(new ThreadStart(work));
thread_array[i].Name = "group 1 thread "+(i + 1).ToString();
thread_array[i].Start();
}
for (int i = 0; i < thread_array.Length; i++)
{
thread_array[i].Join();
}
Console.WriteLine("done group 1 in " + timer.Elapsed);
thread_array = new Thread[5];
for (int i = 0; i < thread_array.Length; i++)
{
thread_array[i] = new Thread(new ThreadStart(work));
thread_array[i].Name = "group 2 thread " + (i + 1).ToString();
thread_array[i].Start();
}
for (int i = 0; i < thread_array.Length; i++)
{
thread_array[i].Join();
}
timer.Stop();
Console.WriteLine("done all in " + timer.Elapsed);
Console.Read();
}
private static void work()
{
var work_timer = Stopwatch.StartNew();
Console.WriteLine("working on thread " + Thread.CurrentThread.Name);
Thread.Sleep(1000);
work_timer.Stop();
Console.WriteLine("done thread " + Thread.CurrentThread.Name + " in " + work_timer.Elapsed);
}
}
}
---------------------------------------------------------
working on thread group 1 thread 1
working on thread group 1 thread 2
working on thread group 1 thread 3
working on thread group 1 thread 4
working on thread group 1 thread 5
done thread group 1 thread 2 in 00:00:01.0023935
done thread group 1 thread 1 in 00:00:01.0038165
done thread group 1 thread 3 in 00:00:01.0021814
done thread group 1 thread 4 in 00:00:01.0235408
done thread group 1 thread 5 in 00:00:01.0079507
done group 1 in 00:00:01.0527982
working on thread group 2 thread 1
working on thread group 2 thread 2
working on thread group 2 thread 3
working on thread group 2 thread 4
working on thread group 2 thread 5
done thread group 2 thread 1 in 00:00:01.0005360
done thread group 2 thread 2 in 00:00:01.0018397
done thread group 2 thread 3 in 00:00:01.0019317
done thread group 2 thread 4 in 00:00:01.0023157
done thread group 2 thread 5 in 00:00:01.0029776
done all in 00:00:02.1052437
---------------------------------------------------
//when threads are being processed, start new thread
//new thread starts before previous thread finish
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
my_timer = Stopwatch.StartNew();
Thread[] thread_array = new Thread[5];
for(int i=0; i<thread_array.Length;i++)
{
thread_array[i] = new Thread(new ThreadStart(work));
thread_array[i].Name = (i + 1).ToString();
thread_array[i].Start();
Thread.Sleep(1000);
}
for(int i=0;i<thread_array.Length;i++)
{
thread_array[i].Join();
}
my_timer.Stop();
Console.WriteLine("done all in " + my_timer.Elapsed);
Console.Read();
}
private static void work()
{
Console.WriteLine("working on thread " + Thread.CurrentThread.Name + " at "+my_timer.Elapsed);
Thread.Sleep(3000);
Console.WriteLine("done thread " + Thread.CurrentThread.Name + " at " + my_timer.Elapsed);
}
private static Stopwatch my_timer { get; set; }
}
}
------------------------------------------------------
working on thread 1 at 00:00:00.0041648
working on thread 2 at 00:00:01.0049986
working on thread 3 at 00:00:02.0054949
done thread 1 at 00:00:03.0048240
working on thread 4 at 00:00:03.0079768
done thread 2 at 00:00:04.0060148
working on thread 5 at 00:00:04.0132561
done thread 3 at 00:00:05.0065144
done thread 4 at 00:00:06.0083626
done thread 5 at 00:00:07.0141073
done all in 00:00:07.0154234
------------------------------------------------------
No comments:
Post a Comment