是否可以使静态类的静态方法可以进行多线程? [英] Is it posibile to make a static method of a static class can be make multithreading ?

查看:142
本文介绍了是否可以使静态类的静态方法可以进行多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使静态类的静态方法可以进行多线程?

Is it possible to make a static method of a static class can be make multithreading ?

推荐答案

是 - 如果线程不需要共享相同的数据同时是可能的。您可能需要查看锁定语句 [ ^ ]虽然。
Yes - provided the threads don't need to share the same data simultaneously it is possible. You may need to look at the lock statment[^] though.


很可能。当然这很有可能。您不仅可以使用多个排除(锁定语句等),还可以使用一些静态成员(本质上是属性和字段,因为静态方法被广泛使用,如果它们仅在堆栈上工作则不会出现问题)。



但这是一个非常糟糕的主意。使用堆栈是隔离线程的方法。即使您想在线程之间共享数据,也不需要静态字段和属性。请看我过去的答案:

在启动后更改线程(生产者)的参数 [ ^ ],

使代码线程安全 [ ^ ],

在webservice中运行一个永远不会终止的作业/线程/进程(asp.net) [ ^ ]。



主要思想是:只有在需要通过某些共享数据或数据传送器进行通信时才需要同步线程。在其他情况下,最佳同步不是同步。如果您隔离线程数据并使用更多堆栈,这会自动出现。



-SA
Quite possible. Of course it's quite possible. Not only you can use multiple exclusion (lock statements, and the like), you also can use some static members (essentially, properties and fields, as static methods are widely used and don't present problem if they work only on stack).

But this is a very, vary bad idea. Using stack is the way to isolate threads. Static fields and properties are not needed even if you want to share data between threads. Please see my past answer:
Change parameters of thread (producer) after it is started[^],
Making Code Thread Safe[^],
Running exactly one job/thread/process in webservice that will never get terminated (asp.net)[^].

The main idea is: you need to synchronize threads only if they really need to communicate via some shared data or conveyors of data. In other cases, best synchronization is no synchronization. And this comes automatically if you isolate thread data and use more of the stack.

—SA


这篇关于是否可以使静态类的静态方法可以进行多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆