c#之线程总结(一)
在我们做项目的时候会经常用到线程,但线程也不是万能的,用线程需要注意的东西也很多,自己做了一下总结
这次总结主要说三个部分
1 线程之委托方法
2 给线程传参
3 三种方法控制线程同步
我们先看一下小例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadMethod
{
class Program
{
static void Main( string [] args)
{
ThreadStart _ts = new ThreadStart(MyThread);
Thread _thread1 = new Thread(_ts);
_thread1.Start();
Console.WriteLine( "other Metod" );
Console.ReadLine();
}
public static void MyThread()
{
for ( int i = 0; i < 10; i++)
{
Console.WriteLine(i + " by Thread " + Thread.CurrentThread.ManagedThreadId);
}
}
}
}
|
1 线程之委托方法
在方法里我们定义了一个 ThreadStart _ts = new ThreadStart(MyThread);
打开msdn 查看发现这是一个委托 ,表示在 Thread 上执行的方法。
[ComVisible( true )]
public delegate void ThreadStart();
|
那就是说也可以这么写
Thread _thread2 = new Thread( delegate ()
{
for ( int i = 0; i < 5; i++)
{
Console.WriteLine(i + " by Thread " + Thread.CurrentThread.ManagedThreadId);
}
});
//或者
Thread _thread3 = new Thread(()=>
{
for ( int i = 0; i < 5; i++)
{
Console.WriteLine(i + " by Thread " + Thread.CurrentThread.ManagedThreadId);
}
});
_thread2.Start();
_thread3.Start();
|
2为线程传递参数
Thread类有一个带参数的委托类型的重载形式
[ComVisibleAttribute(false)] public delegate void ParameterizedThreadStart (Object obj ) 可以传递一个object参数,因为是个委托我们也可以和上面的那么写
class Program
{
static void Main( string [] args)
{
Thread _thread = new Thread( new ParameterizedThreadStart(MyParametFun));
_thread.Start( "aaa" );
Thread _thread2 = new Thread(x => { Console.WriteLine(x); });
_thread2.Start( "bbb" );
Console.WriteLine( "other Metod" );
Console.ReadLine();
}
public static void MyParametFun( object f_str)
{
Console.WriteLine(f_str);
}
}
|
有时个我们有好几个参数只传一个参数据就不能满足了
我们可以直接用委托要多个参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadMethod
{
class Program
{
static void Main( string [] args)
{
int _x = 1;
string _str = "abc" ;
string _str2 = "aaaaa" ;
Thread _thread= new Thread(()=>{
Console.WriteLine(_x);
Console.WriteLine(_str);
Console.WriteLine(_str2);
});
_thread.Start();
Console.WriteLine( "other Metod" );
Console.ReadLine();
}
}
}
|
也可以自己写一个中间类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadMethod
{
class Program
{
static void Main( string [] args)
{
MyMode _myMode = new MyMode(1, "aa" , "bb" );
Thread _thread = new Thread(_myMode.ConsoFun);
_thread.Start();
Console.WriteLine( "other Metod" );
Console.ReadLine();
}
}
public class MyMode
{
int _x ;
string _str ;
string _str2 ;
public MyMode() { }
public MyMode( int f_x, string f_str1, string f_str2)
{
_x = f_x;
_str = f_str1;
_str2 = f_str2;
}
public void ConsoFun()
{
Console.WriteLine(_x);
Console.WriteLine(_str);
Console.WriteLine(_str2);
}
}
}
|
3同步控件
三种方法lock 和Monitor 还有MethodImpl都会让线程同步下面我们一个一个的来说
多个线程访问同一个方法时有可能会出现一个线程修改了一个参数别一个线程进入也修改了这个参数就会发生
错误
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadMethod
{
class Program
{
static int sum = 200;
static void Main( string [] args)
{
Thread _t1 = new Thread(ConFun);
Thread _t2 = new Thread(ConFun);
_t1.Start();
_t2.Start();
Console.WriteLine( "other Metod" );
Console.ReadLine();
}
public static void ConFun()
{
int i = 0;
while (i <= 10)
{
sum -= i;
Thread.Sleep(10);
++i;
}
Console.WriteLine(sum);
}
}
}
|
结果是
根据机器的配制不同可能结果也不同
在这个方法里我们是想让第一个线程减10结果应该 是145
第二个线程进去后在145基础上再减结果应该是90
现在说三种方法lock 和Monitor 还有MethodImpl 都会让线程同步,只有一个线程执行完后另一个线程才能访问
我们一个一个来说吧
先看一下lock
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.CompilerServices;
namespace ThreadMethod
{
class Program
{
static object obj = new object ();
static int sum = 200;
static void Main( string [] args)
{
Thread _t1 = new Thread(ConFun);
Thread _t2 = new Thread(ConFun);
_t1.Start();
_t2.Start();
Console.WriteLine( "other Metod" );
Console.ReadLine();
}
public static void ConFun()
{
lock (obj)
{
int i = 0;
while (i <= 10)
{
sum -= i;
Thread.Sleep(10);
++i;
}
}
Console.WriteLine(sum);
}
}
}
|
这样结果就对了吧
还有其它的两种形式
用MethodImpl 要加上
using System.Runtime.CompilerServices;
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.CompilerServices;
namespace ThreadMethod
{
class Program
{
static object obj = new object ();
static int sum = 200;
static void Main( string [] args)
{
Thread _t1 = new Thread(ConFun);
Thread _t2 = new Thread(ConFun);
_t1.Start();
_t2.Start();
Console.WriteLine( "other Metod" );
Console.ReadLine();
}
[MethodImpl(MethodImplOptions.Synchronized)]
public static void ConFun()
{
int i = 0;
while (i <= 10)
{
sum -= i;
Thread.Sleep(10);
++i;
}
Console.WriteLine(sum);
}
|
Monitor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadMethod
{
class Program
{
static object obj = new object ();
static int sum = 200;
static void Main( string [] args)
{
Thread _t1 = new Thread(ConFun);
Thread _t2 = new Thread(ConFun);
_t1.Start();
_t2.Start();
Console.WriteLine( "other Metod" );
Console.ReadLine();
}
public static void ConFun()
{
Monitor.Enter(obj);
int i = 0;
while (i <= 10)
{
sum -= i;
Thread.Sleep(10);
++i;
}
Monitor.Exit(obj);
Console.WriteLine(sum);
}
}
}
|
结果都是正确的
本文转自lpxxn博客园博客,原文链接:http://www.cnblogs.com/li-peng/archive/2013/03/15/2961224.html,如需转载请自行联系原作者
c#之线程总结(一)相关推荐
- 多线程编程指南 part 2
多线程编程指南 Sun Microsystems, Inc. 4150 Network Circle Santa Clara, CA95054 U.S.A. 文件号码819–7051–10 2006 ...
- java 手编线程池_死磕 java线程系列之自己动手写一个线程池
欢迎关注我的公众号"彤哥读源码",查看更多源码系列文章, 与彤哥一起畅游源码的海洋. (手机横屏看源码更方便) 问题 (1)自己动手写一个线程池需要考虑哪些因素? (2)自己动手写 ...
- Redis 笔记(12)— 单线程架构(非阻塞 IO、多路复用)和多个异步线程
Redis 使用了单线程架构.非阻塞 I/O .多路复用模型来实现高性能的内存数据库服务.Redis 是单线程的.那么为什么说是单线程呢? Redis 在 Reactor 模型内开发了事件处理器,这个 ...
- Python 多线程总结(2)— 线程锁、线程池、线程数量、互斥锁、死锁、线程同步
主要介绍使用 threading 模块创建线程的 3 种方式,分别为: 创建 Thread 实例函数 创建 Thread 实例可调用的类对象 使用 Thread 派生子类的方式 多线程是提高效率的一种 ...
- 详解 Tomcat 的连接数与线程池
原文出处:编程迷思 前言 在使用tomcat时,经常会遇到连接数.线程数之类的配置问题,要真正理解这些概念,必须先了解Tomcat的连接器(Connector). 在前面的文章 详解Tomcat配置文 ...
- Spring并发访问的线程安全性问题
下面的记录对spring中并发的总结.理论分析参考Spring中Singleton模式的线程安全,建议先看 spring中的并发访问题: 我们知道在一般情况下,只有无状态的Bean才可以在多线程环境下 ...
- 聊一聊Spring中的线程安全性
原文出处:SylvanasSun Spring与线程安全 ThreadLocal ThreadLocal中的内存泄漏 参考文献 Spring与线程安全 Spring作为一个IOC/DI容器,帮助我们管 ...
- 线程的状态、调度、同步
线程的状态 java中的线程共五个状态:新建.就绪.运行.阻塞.死亡: 新建状态(New):处于系统创建线程,但未启动此线程,系统未为其分配资源. 就绪状态(Runnable):线程调用start( ...
- 关于Numba的线程实现的说明
关于Numba的线程实现的说明 由Numbaparallel目标执行的工作由Numba线程层执行.实际上,"线程层"是Numba内置库,可以执行所需的并发执行.在撰写本文时,有三个 ...
- Android线程池简单使用
线程池使用的好处: 1)对多个线程进行统一地管理,避免资源竞争中出现的问题. 2)对线程进行复用,线程在执行完任务后不会立刻销毁,而会等待另外的任务,这样就不会频繁地创建.销毁线程和调用GC. 使用T ...
最新文章
- 软件开发依据的标准或法律法规_第178篇丨直真科技:官宣!定制软件开发不应该采用完工百分比法确认收入...
- AMD CPU真烂!售后服务也很可恶!
- Only the original thread that created a view hierarchy can touch its views——Handler的使用
- 一次恢复outlook express通讯录文件*.wab的经历
- C/C++ Native 包大小测量
- 用unity制作能量护盾(3)
- JAVA垃圾回收器源码_浅谈关于Java的GC垃圾回收器的一些基本概念
- FFmpeg滤镜:使用colorkey抠图
- 《算法导论》第三版第13章 红黑树 练习思考题 个人答案
- 吐槽表情包计算机系,网友用表情包形容自己的专业 分明是场吐槽大会
- Hadoop3.3.1 踩坑笔记
- ClickHouse的核心特性及架构
- 职场技巧:高效实用的四象限法则
- 3月20 Bundle Adjustment光束平差法概述
- 1e9个兵临城下 - 容斥原理
- 【HTML5】Web前端——第三课:HTML5 表单相关元素和属性
- 快速安装AXURE谷歌扩展插件
- CASS3D2.0.3旗靓版更新了,更稳定【下载地址文末】
- 电脑公司GHOST WIN7 SP1 2011装机旗舰版
- [Russell Han] 24 | 数据库基础 | 关系模型
热门文章
- 极域电子教室软件怎么脱离控制_新疆灵感科技技术汇总~LED控制卡常见软、硬件问题...
- mysql 分页_百万数据下mysql分页问题
- java 反射技术实例,什么是反射技术?Java中最常用的反射技术实例
- contenttype文件ajax_jquery ajax contentType设置
- 关于 try catch 捕捉不到异常
- 计算机操作员技术工作总结,计算机操作员工作总结
- mysql安全方面_MySQL数据库在网络安全方面功能有哪些呢?
- mysql 存储过程 大于等于_mysql 存储过程 大于
- 20200721:每日一题之不同的二叉搜索树 II(leetcode95)
- matlabapp窗口图像_如何在一个matlab窗口上合并两个图像?