首页 > 编程知识 正文

线程与进程的比较,cachedthreadpool

时间:2023-05-05 10:20:13 阅读:58895 作者:101

. NET提供了多种创建和启动线程的方法。 那么有两种方法(基本上)。

(1)使用System.Threading.Thread类。

thread curr=new thread (my function; curr.start (新对象) ); )2)使用System.Threading.ThreadPool类。

1 .无参数

thread pool.queueuserworkitem (delegate (postdatawhenloaddata ); );

私有语音下载数据(

{

behaviorreturnbr=_ re service.searchprintreport (currentso,isCount );

if(br.Success ) )。

{

sysnassignwhenload (br.object list;

if(br.objectlist.count==0) )。

{

var Sr msg=new服务器消息

{

代码=' no data ',

消息cn='没有符合查询条件的数据'

(;

br.messagelist.add(Srmsg;

sysnshowerror(br;

}

}

else

{

sysnshowerror(br;

}

}

2 .带参数

string ti=' ';

thread pool.queueuserworkitem (newwaitcallback ) threadproc ),ti ); (参数ti可以是任意类型)

私有语音(对象ti )//回调函数的参数类型必须写为对象

{

}

总结:

创建线程需要很大的开销。 Thread创建新线程,ThreadPool通过从线程池中重用挂起的空闲线程来减少线程创建开销。 使用ThreadPool线程时,这些线程由系统管理。 如果要多次执行小任务,线程池更适合。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。