C#关于多线程及线程同步 lock锁的应用
Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WinLock { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn_Start_Click(object sender, EventArgs e) { listBox1.Items.Clear(); Thread[] threads = new Thread[10];//使用new关键字在堆里创建对象,其生命期相当于全局变量,但访问的时候与普通变量一样 Account acc = new Account(1000, this); for (int i = 0; i < 10; i++) { Thread t = new Thread(acc.DoTransactions); t.Name = "线程" + i; threads[i] = t; } for (int i = 0; i < 10; i++) { threads[i].Start(); } } delegate void AddListBoxItemDelegate(string str);//在类的内部也能声明代理 public void AddListBoxItem(string str) { if (listBox1.InvokeRequired)//如果是跨线程访问控件则为true { AddListBoxItemDelegate d = AddListBoxItem;//代理 listBox1.Invoke(d, str);//调用代理函数并传递参数 } else { listBox1.Items.Add(str);//不是跨线程访问控件则直接访问该控件 } } } }
account.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace WinLock { public class Account { private Object lockedObj = new Object(); int balance; Random r = new Random(); Form1 form1; public Account(int initial, Form1 form1) { this.form1 = form1; this.balance = initial; } /// <summary> /// 取款 /// </summary> /// <param name="amount">取款金额</param> /// <returns>余额</returns> private int Withdraw(int amount) { if (balance < 0) { form1.AddListBoxItem("余额:" + balance + " 已经为负值了,还想取呵!"); } //将lock (lockedObj)这句注释掉,看看会发生什么情况 lock (lockedObj) { if (balance >= amount) { string str = Thread.CurrentThread.Name + "取款---"; str += string.Format("取款前余额:{0,-6}取款:{1,-6}", balance, amount); balance = balance - amount; str += "取款后余额:" + balance; form1.AddListBoxItem(str); return amount; } else { return 0; } } } /// <summary>自动取款</summary> public void DoTransactions()//Transaction--事务 { for (int i = 0; i < 100; i++) { Withdraw(r.Next(1, 100)); } } } }
不加lock 会出现统一资源被多次利用的情况
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通