多線程之其他輔助類: SpinWait, SpinLock, Volatile, SynchronizationContext, CoreDispatcher, ThreadLocal, ThreadStaticAttribute
介紹
重新想象 Windows 8 Store Apps 之 多線程操作的其 他輔助類
SpinWait - 自旋等待
SpinLock - 自旋鎖
volatile - 必在內存
SynchronizationContext - 在指定的線程上同步數 據
CoreDispatcher - 調度器,用於線程同步
ThreadLocal - 用於保存每 個線程自己的數據
ThreadStaticAttribute - 所指定的靜態變量對每個線程都是唯一的
示例
1、演示 SpinWait 的使用
Thread/Other/SpinWaitDemo.xaml.cs
/*
* SpinWait - 自旋等待,一個低級別的同步類型。它不會放棄任何 cpu 時間,而是讓 cpu 不停的循環等待
*
* 適用場景:多核 cpu ,預期等待時間非常短(幾微秒)
* 本例只是用於描述 SpinWait 的用法,而不代表適用場景
*/
using System;
using System.Threading;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.Thread.Other
{
public sealed partial class SpinWaitDemo : Page
{
public SpinWaitDemo()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
lblMsg.Text = DateTime.Now.ToString("mm:ss.fff");
SpinWait.SpinUntil(
() => // 以下條件成立時,結束等待
{
return false;
}
// 如果此超時時間過後指定的條件還未成立,則強制結束等待
,1000);
lblMsg.Text += Environment.NewLine;
lblMsg.Text += DateTime.Now.ToString("mm:ss.fff");
SpinWait.SpinUntil(
() => // 以下條件成立時,結束等待
{
return DateTime.Now.Second % 2 == 0;
});
lblMsg.Text += Environment.NewLine;
lblMsg.Text += DateTime.Now.ToString("mm:ss.fff");
}
}
}
2、演示 SpinLock 的使用
Thread/Other/SpinLockDemo.xaml.cs
/*
* SpinLock - 自旋鎖,一個低級別的互斥鎖。它不會放棄任何 cpu 時間,而是讓 cpu 不停的循環等待,直至鎖變為可用為止
*
* 適用場景:多核 cpu ,預期等待時間非常短(幾微秒)
* 本例只是用於描述 SpinLock 的用法,而不代表適用場景
*/
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.Thread.Other
{
public sealed partial class SpinLockDemo : Page
{
private static int _count;
public SpinLockDemo()
{
this.InitializeComponent();
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
SpinLock spinLock = new SpinLock();
List<Task> tasks = new List<Task>();
// 一共 100 個任務並行執行,每個任務均累加同一個靜態變量 100000 次,以模擬並發訪問靜態變量的場景
for (int i = 0; i < 100; i++)
{
Task task = Task.Run(
() =>
{
bool lockTaken = false;
try
{
// IsHeld - 鎖當前是否已由任何線程占用
// IsHeldByCurrentThread - 鎖是否由當前線程占用
// 要獲取 IsHeldByCurrentThread 屬性,則IsThreadOwnerTrackingEnabled 必須為 true,可以在構造函數中指定,默認就是 true
// 進入鎖,lockTaken - 是否已獲取到鎖
spinLock.Enter(ref lockTaken);
for (int j = 0; j < 100000; j++)
{
_count++;
}
}
finally
{
// 釋放鎖
if (lockTaken)
spinLock.Exit();
}
});
tasks.Add(task);
}
// 等待所有任務執行完畢
await Task.WhenAll(tasks);
lblMsg.Text = "count: " + _count.ToString();
}
}
}
3、演示 volatile 的使用
Thread/Other/VolatileDemo.xaml
<Page
x:Class="XamlDemo.Thread.Other.VolatileDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Thread.Other"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Transparent">
<StackPanel Margin="120 0 0 0">
<TextBlock FontSize="14.667" LineHeight="20">
<Run>如果編譯器認為某字段無外部修改,則為了優化會將其放入寄存器</Run>
<LineBreak />
<Run>標記為 volatile 的字段,則必然會被放進內存</Run>
<LineBreak />
<Run>編寫 Windows Store Apps 後台任務類的時候,如果某個字段會被後台任務的調用者修改的話,就要將其標記為 volatile,因為這種情況下編譯器會認為此字段無外部修改</Run>
</TextBlock>
</StackPanel>
</Grid>
</Page>
4、演示 SynchronizationContext 的使用
Thread/Other/SynchronizationContextDemo.xaml.cs
/*
* SynchronizationContext - 在指定的線程上同步數據
* Current - 獲取當前線程的 SynchronizationContext 對象
* Post(SendOrPostCallback d, object state) - 同步數據到此 SynchronizationContext 所關聯的線
程上
*/
using System;
using Windows.System.Threading;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.Thread.Other
{
public sealed partial class SynchronizationContextDemo : Page
{
System.Threading.SynchronizationContext _syncContext;
public SynchronizationContextDemo()
{
this.InitializeComponent();
// 獲取當前線程,即 UI 線程
_syncContext = System.Threading.SynchronizationContext.Current;
ThreadPoolTimer.CreatePeriodicTimer(
(timer) =>
{
// 在指定的線程(UI 線程)上同步數據
_syncContext.Post(
(ctx) =>
{
lblMsg.Text = DateTime.Now.ToString("mm:ss.fff");
},
null);
},
TimeSpan.FromMilliseconds(100));
}
}
}
5、演示 CoreDispatcher 的使用
Thread/Other/CoreDispatcherDemo.xaml.cs
/*
* CoreDispatcher - 調度器,用於線程同步
*/
using System;
using Windows.System.Threading;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.Thread.Other
{
public sealed partial class CoreDispatcherDemo : Page
{
public CoreDispatcherDemo()
{
this.InitializeComponent();
// 獲取 UI 線程的 CoreDispatcher
CoreDispatcher dispatcher = Window.Current.Dispatcher;
ThreadPoolTimer.CreatePeriodicTimer(
(timer) =>
{
// 通過 CoreDispatcher 同步數據
// var ignored = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
var ignored = dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
lblMsg.Text = DateTime.Now.ToString("mm:ss.fff");
});
},
TimeSpan.FromMilliseconds(100));
}
}
}
6、演示 ThreadLocal 的使用
Thread/Other/ThreadLocalDemo.xaml.cs
/*
* ThreadLocal<T> - 用於保存每個線程自己的數據,T - 需要保存的數據的數據類型
* ThreadLocal(Func<T> valueFactory, bool trackAllValues) - 構造函數
* valueFactory - 指定當前線程個性化數據的初始值
* trackAllValues - 是否需要獲取所有線程的個性化數據
* T Value - 當前線程的個性化數據
* IList<T> Values - 獲取所有線程的個性化數據(trackAllValues == true 才能獲取)
*
*
* 注:ThreadStaticAttribute 與 ThreadLocal<T> 的作用差不多
*/
using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.Thread.Other
{
public sealed partial class ThreadLocalDemo : Page
{
System.Threading.SynchronizationContext _syncContext;
public ThreadLocalDemo()
{
this.InitializeComponent();
// 獲取當前線程,即 UI 線程
_syncContext = System.Threading.SynchronizationContext.Current;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ThreadLocal<string> localThread = new ThreadLocal<string>(
() => "ui thread webabcd", // ui 線程的個性化數據
false);
Task.Run(() =>
{
// 此任務的個性化數據
localThread.Value = "thread 1 webabcd";
_syncContext.Post((ctx) =>
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += ctx.ToString();
},
localThread.Value);
});
Task.Run(() =>
{
// 此任務的個性化數據
localThread.Value = "thread 2 webabcd";
_syncContext.Post((ctx) =>
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += ctx.ToString();
},
localThread.Value);
});
lblMsg.Text += localThread.Value;
}
}
}
7、演示 ThreadStaticAttribute 的使用
Thread/Other/ThreadStaticAttributeDemo.xaml.cs
/*
* ThreadStaticAttribute - 所指定的靜態變量對每個線程都是唯一的
*
*
* 注:ThreadStaticAttribute 與 ThreadLocal<T> 的作用差不多
*/
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.Thread.Other
{
public sealed partial class ThreadStaticAttributeDemo : Page
{
// 此靜態變量對每個線程都是唯一的
[ThreadStatic]
private static int _testValue = 0;
System.Threading.SynchronizationContext _syncContext;
public ThreadStaticAttributeDemo()
{
this.InitializeComponent();
// 獲取當前線程,即 UI 線程
_syncContext = System.Threading.SynchronizationContext.Current;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Task.Run(() =>
{
_testValue = 10;
_syncContext.Post((testValue) =>
{
lblMsg.Text += Environment.NewLine;
// 此 Task 上的 _testValue 的值
lblMsg.Text += "thread 1 testValue: " + testValue.ToString();
},
_testValue);
});
Task.Run(() =>
{
_testValue = 100;
_syncContext.Post((testValue) =>
{
lblMsg.Text += Environment.NewLine;
// 此 Task 上的 _testValue 的值
lblMsg.Text += "thread 2 testValue: " + testValue.ToString();
},
_testValue);
});
// ui 線程上的 _testValue 的值
lblMsg.Text = "ui thread testValue: " + _testValue.ToString();
}
}
}
OK
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar