lock鎖和monitor.enter鎖
Lock(object)鎖的使用
using System;
using System.Threading;
namespace program
{
class wangjun
{
public static string buff = "0";
public const int ab = 1000000;
private object mylock = new object();
static void Main(string[] args)
{
wangjun wj = new wangjun();
Thread th = new Thread(new ThreadStart(wj.xuan1));
th.Start();
Thread th2 = new Thread(new ThreadStart(wj.xuan2));
th2.Start();
th.Join();
th2.Join();
Console.WriteLine("結果是:{0}",buff);
}
public void xuan1()
{
for (int i = 0; i < ab/2; i++)
{
lock (mylock)
{
buff = (long.Parse(buff) + i).ToString();
}
}
}
public void xuan2()
{
for (int i = ab/2; i <= ab; i++)
{
lock (mylock)
{
buff = (long.Parse(buff) + i).ToString();
}
}
}
}
}
Monitor.enter(object)的使用
using System;
using System.Threading;
namespace program
{
class wangjun
{
public static string buff = "0";
public const int ab = 1000000;
private object mylock = new object();
static void Main(string[] args)
{
wangjun wj = new wangjun();
Thread th = new Thread(new ThreadStart(wj.xuan1));
th.Start();
Thread th2 = new Thread(new ThreadStart(wj.xuan2));
th2.Start();
th.Join();
th2.Join();
Console.WriteLine("結果是:{0}",buff);
}
public void xuan1()
{
for (int i = 0; i < ab/2; i++)
{
Monitor.Enter(mylock);
try
{
buff = (long.Parse(buff) + i).ToString();
}
finally
{
Monitor.Exit(mylock);
}
}
}
public void xuan2()
{
for (int i = ab/2; i <= ab; i++)
{
Monitor.Enter(mylock);
try
{
buff = (long.Parse(buff) + i).ToString();
}
finally
{
Monitor.Exit(mylock);
}
}
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22392018/viewspace-772796/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【JavaSE】Lock鎖和synchronized鎖的比較,lock鎖的特性,讀寫鎖的實現。Javasynchronized
- Lock的獨佔鎖和共享鎖的比較分析
- oracle lock鎖_v$lock_轉Oracle
- MySQL 共享鎖 (lock in share mode),排他鎖 (for update)MySql
- 【Mysql】metadata lock鎖MySql
- MYSQL 鎖:metadata lockMySql
- 分散式鎖-Redission-Lock鎖的使用與原理分散式Redis
- mysql innodb lock鎖之record lock之一MySql
- 帶你理解Lock鎖原理
- Lock鎖相關以及AQSAQS
- Lock、Synchronized鎖區別解析synchronized
- TX鎖(Transaction Lock)分析 (zt)
- mysql表鎖與lock tablesMySql
- [CareerCup] 16.4 A Lock Without Deadlocks 無死鎖的鎖
- java裡的鎖總結(synchronized隱式鎖、Lock顯式鎖、volatile、CAS)Javasynchronized
- java的乾兒子鎖LockJava
- MySQL-lock(鎖)-v2.0MySql
- 鎖——Lock、Condition、ReadWriteLock、LockSupport
- ThunderSoft File Lock for Mac檔案鎖Mac
- MySQL 的next-lock 鎖MySql
- 【鎖】Latch、lock、 pin的區別
- Java中syncrhoized ,reentrantLock ,Atomic ,Lock ,ThreadLocal ,transient ,volatile,物件鎖和類鎖JavaZedReentrantLockthread物件
- Lock介面、重入鎖ReentrantLock、讀寫鎖ReentrantReadWriteLockReentrantLock
- mysql metadata lock後設資料鎖之鎖狀態lock_status流轉圖MySql
- Java併發指南4:Java中的鎖 Lock和synchronizedJavasynchronized
- 如何開啟和關閉MySQL 間隙鎖(gap lock)MySql
- 併發程式設計的鎖機制:synchronized和lock程式設計synchronized
- java重入鎖、公平鎖和非公平鎖Java
- mysql觀測METADATA LOCK(MDL)鎖MySql
- oracle breakable parse lock 易碎解析鎖Oracle
- MySQL 死鎖和鎖等待MySql
- 全域性鎖和表鎖
- Mysql鎖之行級鎖和表級意向鎖MySql
- 自旋鎖、阻塞鎖、可重入鎖、悲觀鎖、樂觀鎖、讀寫鎖、偏向所、輕量級鎖、重量級鎖、鎖膨脹、物件鎖和類鎖物件
- 深入學習Lock鎖(2)——LockSupport工具類
- Oracle中的死鎖Dead Lock(一)Oracle
- Oracle中的死鎖Dead Lock(二)Oracle
- MySQL 5.5 -- innodb_lock_wait 鎖 等待MySqlAI