当你锁定一个对象时,该对象是否在整个应用程序中被锁定?
例如,Cuts 3.0中的这个片段在Nutshell Section 19.6.1"Thread Safety and .NET Framework Types"中:
static void AddItems( )
{
for (int i = 0; i < 100; i++)
lock (list)
list.Add ("Item " + list.Count);
string[] items;
lock (list) items = list.ToArray( );
foreach (string s in items) Console.WriteLine (s);
}
Run Code Online (Sandbox Code Playgroud)
第一次锁定:
lock (list)
list.Add ("Item " + list.Count);
Run Code Online (Sandbox Code Playgroud)
防止其他线程访问:
lock (list) items = list.ToArray( );
Run Code Online (Sandbox Code Playgroud)
或者都可以同时执行?
CLR会自动使您的静态方法线程安全吗?或者这取决于开发人员?
谢谢,约翰
我的目标是Properties在我的类中有一个私有静态对象,在创建Properties我的应用程序所需的其他对象时充当默认对象.当前的实现如下所示:
public class MyClass {
private static Properties DEFAULT_PROPERTIES = new Properties();
static {
try {
DEFAULT_PROPERTIES.load(
MyClass.class.getResourceAsStream("myclass.properties"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
看着它,它有效,但感觉不对.
你会怎么做?
我是一名C程序员,但很久以来就学习过C++ @school.现在我试图用C++编写代码,但是编译错误.请检查并告诉我我的代码有什么问题.
typedef class _filter_session
{
private:
static int session_count; /* Number of sessions count -- Static */
public:
_filter_session(); /* Constructor */
~_filter_session(); /* Destructor */
}FILTER_SESSION;
_filter_session::_filter_session(void)
{
(this->session_count)++;
return;
}
_filter_session::~_filter_session(void)
{
(this->session_count)--;
return;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
"错误LNK2001:未解析的外部符号"private:static int _filter_session :: session_count"(?session_count @ _filter_session @@ 0HA)"
我顺便使用Visual Studio 2005.
Plz plz帮帮我.
问候,
微内核
我有一个Car具有类范围变量成本的类,我理解这在实践中是一个坏主意,只是试图理解公共范围类变量的影响.
Car的所有对象,Car.cost的引用,所有类加载器都可以访问和修改成本,还是应该知道Car.cost可能存在多个副本的情况?Car.cost在任何特定情况下都会有一个吗?
public class Car{
public static int cost;
public Car(int cost){
cost = cost;
}
}
public static void main(String args[]){
Car car = new Car(2);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个类,其中包含我们为应用程序制作的各种图形决策的相关数据,并且能够在一个位置进行这些更改.但是,每当我调用该类时,我总是得到相同的UIColor.White返回,而不是我请求的任何颜色.我在Xamarin.iOS库中有这个类,我在主项目中引用它.
这是一个代码示例:
public static class MyColors
{
public static UIColor BackgroundColor
{
get { return ConvertHexToColor("fa0000"); }
}
public static UIColor ConvertHexToColor(string hex)
{
if (hex.Contains('#')) hex.Replace("#", "");
int[] rgba = new int[] { 255, 255, 255, 255 };
if (hex.Length == 6)
{
rgba[0] = Convert.ToInt32(hex.Substring(0, 2), 16);
rgba[1] = Convert.ToInt32(hex.Substring(2, 2), 16);
rgba[2] = Convert.ToInt32(hex.Substring(4, 2), 16);
}
else if (hex.Length == 8)
{
rgba[0] = Convert.ToInt32(hex.Substring(0, 2), 16);
rgba[1] = Convert.ToInt32(hex.Substring(2, 2), 16);
rgba[2] = Convert.ToInt32(hex.Substring(4, 2), …Run Code Online (Sandbox Code Playgroud) 以下是一些引发NameError的代码:
class Foo:
a = [1,2,3,4,5]
b = all(i for i in a) # works fine !
c = [2,3,4]
d = all(i in a for i in c) # raises NameError : name 'a' is not defined
Run Code Online (Sandbox Code Playgroud)
我很困惑 - ;
谢谢 !
我理解静态关键字的目的但遗憾的是我无法弄清楚这个简单的代码.不知道为什么?我期待答案非常简单.
public class VariableScope {
int x=y;
static int y=5;
public static void main(String[] args) {
System.out.println(new VariableScope().x);
}
}
Run Code Online (Sandbox Code Playgroud)
当y在早期分配给它时,x如何打印为5?
我试图弄清楚为什么我在Xcode 6.3.2中对这种类型的构造有持续的编译问题.
class Foo {
static let CONSTANT_NAME = "CONSTANT_STRING"
...
func bar () -> String {
var s = String(format:"%s,%d\n", CONSTANT_NAME, 7)
return s
}
...
}
Run Code Online (Sandbox Code Playgroud)
据我了解这种语言,这应该是完全合法的代码,但是Xcode经常(hah-pun)有问题引发错误
"Foo课程中没有成员CONSTANT_NAME"
如果我很幸运并强迫它清理,然后重建它将有时会自行解决并工作.其他时候,即使这样做,然后尝试打开/关闭项目仍然无法解决问题.
所以,我想我的隐含跟进问题(如果上面的答案是 - 它是合法代码)是:是Xcode Swift编译器,即使像这样的基本事情可能会导致问题吗?如果是这样,斯威夫特似乎处于一种非常糟糕的状态.
通过查看以下代码,我们注意到初始化名为的静态数据成员非常容易cnt:
template<typename T> struct Base { static int cnt; };
template<typename T> int Base<T>::cnt = 0;
int main() { }
Run Code Online (Sandbox Code Playgroud)
无论如何,我正在与variadic_template静态数据成员斗争,因为我无法用它们来制作它.请考虑以下代码:
template<typename...> struct Base;
template<> struct Base<> { static int cnt; };
int main() { }
Run Code Online (Sandbox Code Playgroud)
首先,我尝试了最明显的事情,至少是对我来说最明显的事情:
template<typename... T> int Base<T...>::cnt = 0;
Run Code Online (Sandbox Code Playgroud)
它回来了error: template definition of non-template ‘int Base<T>::cnt’,我立刻意识到我的尝试确实没有意义.那就是说,之后我有点疑惑,因为对我来说这是不对的,这是正确的语法.
我尝试了以下方法:
template<> int Base<>::cnt = 0;
// ... and ...
template<> int Base::cnt = 0;
Run Code Online (Sandbox Code Playgroud)
随着错误error: template definition of non-template ‘unsigned int Base<T>::cnt’ …
在C ++中,union可以包含静态成员,这些静态成员与类一样属于一个类,因此对于所有对象都是公共的。
union U
{
long l;
int i;
static long sl;
static int si;
};
int U::si;
long U::sl;
Run Code Online (Sandbox Code Playgroud)
逻辑上期望所有联合静态成员存储在与非静态成员存储类似的相同地址上。但事实并非如此。一个简单的示例显示静态成员存储在不同的地址下,并且可以包含独立的值。
int main()
{
U u;
u.si = 10;
u.sl = 50;
std::cout << "Non-static members adresses: " << &u.i << " " << &u.l << std::endl;
std::cout << "Static members adresses: " << &u.si << " " << &u.sl << std::endl;
std::cout << "Static members values: " << u.si << " " << u.sl << …Run Code Online (Sandbox Code Playgroud)