静态类&穿线 [英] static classes & threading
本文介绍了静态类&穿线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我有一个包含多个
线程调用的所有静态方法的类。我想知道这对竞争线程有什么影响。
Thread2是否必须等到Thread1完成后才能使用
StaticClass.Method1才能使用它? />
如果我删除静态方法并使所有线程实例化
它自己的类副本怎么办?这会消除等待的争用吗?
谢谢。
Hello,
I have a class with all static methods that is called by multiple
threads. I was wondering what effect that has on the competing threads.
Does Thread2 have to wait until Thread1 is done with the
StaticClass.Method1 before it can use it?
What if I removed static methods and made all the threads instantiate
its own copy of the class? Would that remove the waiting contention?
Thanks.
推荐答案
这就是多线程:)所以除非在你的方法中有一个
互斥锁,否则第二个线程将不会等待
第一个线程来完成静态方法的执行。 />
Gabriel Lozano-Morán
软件工程师
Sogeti
" Frank Rizzo" <无** @ none.com>在消息中写道
news:eW ************** @ TK2MSFTNGP15.phx.gbl ...
That''s the whole point of multithreading :) So unless there is a
mutual-exclusion lock in your method the second thread will not wait for the
first thread to finish executing the static method.
Gabriel Lozano-Morán
Software Engineer
Sogeti
"Frank Rizzo" <no**@none.com> wrote in message
news:eW**************@TK2MSFTNGP15.phx.gbl...
你好,>
我有一个包含所有静态方法的类,由多个线程调用。
我想知道它对竞争线程有什么影响。
Thread2是否必须等到Thread1完成
StaticClass.Method1才可以使用它?
如果我删除静态方法并让所有线程实例化它自己的类副本怎么办?这会消除等待的争用吗?
谢谢。
Hello,
I have a class with all static methods that is called by multiple threads.
I was wondering what effect that has on the competing threads.
Does Thread2 have to wait until Thread1 is done with the
StaticClass.Method1 before it can use it?
What if I removed static methods and made all the threads instantiate its
own copy of the class? Would that remove the waiting contention?
Thanks.
我还不清楚。假设静态方法DoSomeTask创建一个
连接对象并获取一些数据,然后销毁连接。
然后让我们说2个线程调用DoSomeTask。第二个帖子是否会等待第一个帖子完成?
问候
Gabriel Lozano- Morán写道:
I am still not clear. Let''s say the static method DoSomeTask creates a
connection object and gets some data, then destroys the connection.
Then let''s say 2 threads call DoSomeTask. Will the second thread have
to wait for the first thread to complete?
Regards
Gabriel Lozano-Morán wrote:
这就是多线程的全部要点:)所以除非你的方法中有一个
互斥锁,否则第二个线程不会等待
第一个完成执行静态方法的线程。
Gabriel Lozano-Morán
软件工程师
Sogeti
Frank Rizzo <无** @ none.com>在消息中写道
新闻:eW ************** @ TK2MSFTNGP15.phx.gbl ...
That''s the whole point of multithreading :) So unless there is a
mutual-exclusion lock in your method the second thread will not wait for the
first thread to finish executing the static method.
Gabriel Lozano-Morán
Software Engineer
Sogeti
"Frank Rizzo" <no**@none.com> wrote in message
news:eW**************@TK2MSFTNGP15.phx.gbl...
你好,>
我有一个包含所有静态方法的类,由多个线程调用。
我想知道它对竞争线程有什么影响。
Thread2是否必须等到Thread1完成
StaticClass.Method1才可以使用它?
如果我删除静态方法并让所有线程实例化它自己的类副本怎么办?这会消除等待的争用吗?
谢谢。
Hello,
I have a class with all static methods that is called by multiple threads.
I was wondering what effect that has on the competing threads.
Does Thread2 have to wait until Thread1 is done with the
StaticClass.Method1 before it can use it?
What if I removed static methods and made all the threads instantiate its
own copy of the class? Would that remove the waiting contention?
Thanks.
没有第二个线程也会创建一个连接对象并获取一些数据
,而第一个线程仍在获取一些数据。
静态方法和非静态方法之间的区别在于静态方法属于
非静态方法属于类实例的类型。
Gabriel Lozano-Morán
软件工程师
Sogeti
" Frank Rizzo <无** @ none.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...
No the second thread will also create a connection object and get some data
while the first thread is still getting some data. The difference between a
static method and a non-static method is that the static method belongs to
the type where the non-static method belongs to an instance of a class.
Gabriel Lozano-Morán
Software Engineer
Sogeti
"Frank Rizzo" <no**@none.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
我是还不清楚。假设静态方法DoSomeTask创建一个
连接对象并获取一些数据,然后破坏连接。然后
让我们说2个线程调用DoSomeTask。第二个线程是否必须等待第一个线程完成?
Gabriel Lozano-Morán写道:
I am still not clear. Let''s say the static method DoSomeTask creates a
connection object and gets some data, then destroys the connection. Then
let''s say 2 threads call DoSomeTask. Will the second thread have to wait
for the first thread to complete?
Regards
Gabriel Lozano-Morán wrote:
那个多线程的全部意义:)所以除非你的方法中有一个
互斥锁,否则第二个线程不会等待第一个线程完成静态方法的执行。
Gabriel Lozano-Morán
软件工程师
Sogeti
Frank Rizzo <无** @ none.com>在消息中写道
新闻:eW ************** @ TK2MSFTNGP15.phx.gbl ...
That''s the whole point of multithreading :) So unless there is a
mutual-exclusion lock in your method the second thread will not wait for
the first thread to finish executing the static method.
Gabriel Lozano-Morán
Software Engineer
Sogeti
"Frank Rizzo" <no**@none.com> wrote in message
news:eW**************@TK2MSFTNGP15.phx.gbl...
你好,>
我有一个包含多个
线程调用的所有静态方法的类。我想知道这对竞争线程有什么影响。
Thread2是否必须等到Thread1完成后才能使用
StaticClass.Method1才能使用它?
如果我删除了静态方法并使所有线程实例化了它自己的类副本?这会消除等待的争用吗?
谢谢。
Hello,
I have a class with all static methods that is called by multiple
threads. I was wondering what effect that has on the competing threads.
Does Thread2 have to wait until Thread1 is done with the
StaticClass.Method1 before it can use it?
What if I removed static methods and made all the threads instantiate its
own copy of the class? Would that remove the waiting contention?
Thanks.
这篇关于静态类&穿线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文