当前位置:网站首页>C# 线程锁和单多线程简单使用
C# 线程锁和单多线程简单使用
2022-07-23 09:20:00 【InfoQ】
前言:
每日一遍,心情愉悦

1.首先看看我们的问题

private static object lockObj = new object();//定义线程锁
private int num = 0;
private void Test()
{
while (true)
{
lock (lockObj)//锁住这个代码块
{
num++;
string name = Thread.CurrentThread.Name;//获取线程名
textBox1.AppendText(name+"测试:"+num);//这个是TextBox1的追加
textBox1.AppendText(System.Environment.NewLine);
Thread.Sleep(2000);//将线程挂起,相当于停止2秒
if (num>=10)//让这个线程执行10次就退出
{
break;
}
}
}
}

//单线程
Thread thred1 = new Thread(Test);//定义一个线程
thred1.Name = "thred1";//线程名
thred1.IsBackground = true;//设置为后台线程就是True
thred1.Start();//开始执行这个线程
int n = thred1.ManagedThreadId;//这个线程ID的标识
Console.WriteLine(n);


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;
using System.Windows.Forms;
namespace IC00
{
public partial class Form1 : Form
{
public Form1()
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//抛 textBox1.AppendText的异常
InitializeComponent();
}
private static object lockObj = new object();//定义线程锁
private int num = 0;
private void Test()
{
while (true)
{
lock (lockObj)
{
num++;
string name = Thread.CurrentThread.Name;//获取线程名
textBox1.AppendText(name+"测试:"+num);//这个是TextBox1的追加
textBox1.AppendText(System.Environment.NewLine);
Thread.Sleep(2000);//将线程挂起,相当于停止2秒
if (num>=10)//让这个线程执行10次就退出
{
break;
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
//单线程
Thread thred1 = new Thread(Test);//定义一个线程,运行Test
thred1.Name = "thred1";//线程名
thred1.IsBackground = true;//设置为后台线程就是True
thred1.Start();//开始执行这个线程
int n = thred1.ManagedThreadId;//这个线程ID的标识
Console.WriteLine(n);//在控制器打印线程ID
//多个线程
Thread thred2 = new Thread(Test);
thred2.Name = "thred2";
thred2.IsBackground = true;
thred2.Start();
int m = thred2.ManagedThreadId;
Console.WriteLine(m);//在控制器打印线程ID,看是否是一个线程
}
}
}
自我总结:

边栏推荐
- [applet automation minium] i. framework introduction and environment construction
- Cool code rain dynamic background registration page
- Several hole filling methods of point cloud (mesh) (1)
- Tell you Web3.0 I understand
- Summary of JS data type judgment methods
- Using JS to parse and execute XSS automatically
- 生成订单号
- C language introduction practice (11): enter a group of positive integers and sum the numbers in reverse order
- Which is a good fixed asset management system? What are the fixed asset management platforms?
- Yunna - how to strengthen fixed asset management? How to strengthen the management of fixed assets?
猜你喜欢
随机推荐
(heavy chain dissection) Magic Tree
C language implementation of classroom random roll call system
Several hole filling methods of point cloud (mesh) (1)
Okrk3399 Development Board Reserved i2c4 Mounting EEPROM
Dynamic programming -- knapsack problem
CPU, memory, disk speed comparison
Aruba学习笔记05-配置架构- WLAN配置架构
What is per title encoding?
OKRK3399開發板預留I2C4掛載EEPROM
Qt文档阅读笔记-Audio Example解析
正则表达式常用语法解析
Vk36n5d anti power interference / mobile phone interference 5-key 5-channel touch detection chip anti freeze function ponding in the touch area can still be operated
【面试高频】cookie、session、token?看完再也不担心被问了
Summary of different circulation modes and precautions in JS
利用js自动解析执行xss
Quanzhi f1c100s/f1c200s learning notes (13) -- lvgl transplantation
什么是Per-Title编码?
Yunna | how to manage fixed assets? How to manage fixed assets?
ArcGIS使用DEM数据划定汇水区具体步骤过程
Design and implementation of websocket universal packaging









