我在考虑懒惰单例初始化的经典问题 - 完全无效的问题:
if (instance == null)
{
instance = new Foo();
}
return instance;
Run Code Online (Sandbox Code Playgroud)
任何知道Singleton是什么的人都熟悉这个问题(你只需要if一次).这是微不足道的,但很刺激.
所以,我想到了一个替代解决方案,至少对于.NET来说(尽管它应该可以在任何具有等效功能指针的地方工作:
public class Foo
{
private delegate Foo FooReturner();
private static Foo innerFoo;
private static FooReturner fooReturnHandler = new FooReturner(InitialFooReturner);
public static Foo Instance
{
get
{
return fooReturnHandler();
}
}
private static Foo InitialFooReturner()
{
innerFoo = new Foo();
fooReturnHandler = new FooReturner(NewFooReturner);
return innerFoo;
}
private static Foo NewFooReturner()
{
return innerFoo;
}
}
Run Code Online (Sandbox Code Playgroud)
简而言之 - Instance返回一个委托方法.委托最初设置为初始化实例的方法,然后将委托更改为指向简单的Return方法.
现在,我喜欢认为我的工作并不可怕,但我并不担心自己很棒.我没有在任何地方看到这个代码的例子.
因此,我得出结论,我错过了一些东西.重要的东西.要么整个问题太过微不足道,要么不必考虑太多,或者这会造成毁灭宇宙的可怕事情.或者我在搜索时失败,因此没有看到数百名开发人员使用这种方法.无论如何,还有什么.
我希望Stack Overflow这里的优秀人员可以让我知道什么(除了关于是否应该使用Singleton的争议).
编辑澄清: …
正如问题所述:Mailto对于使用桌面电子邮件客户端的人来说非常有用.我想,这个数字一直在企业内部网之外.因此,使mailto链接给这些人带来烦恼,因为他们必须关闭系统上的默认邮件客户端并复制粘贴链接.
现在,我知道有一些插件来解决这个问题,但让我们面对它 - 大多数人可能都不会使用它们.这几天使用mailto粗鲁还是不假思索?我说是的,你说什么?
问题是这样的.我在Windows 7上使用JDK 6.0,并尝试使用信号量作为解决同步问题的机制.它工作得很好,但我试图避免忙于等待我的问题.
我只想问一下java文档并省去了麻烦,但文档是这样的:
Acquires the given number of permits from this semaphore,
blocking until all are available, or the thread is interrupted.
Acquires the given number of permits, if they are available,
and returns immediately, reducing the number of available permits
by the given amount.
If insufficient permits are available then the current thread
becomes disabled for thread scheduling purposes and lies dormant
Run Code Online (Sandbox Code Playgroud)
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Semaphore.html#acquire(int)
也就是说,文档似乎暗示了两个答案.哪一个是正确的?
(在64位Windows 7上运行MingW,在Kubuntu上运行GCC)
这可能只是一个MingW问题,但至少在安装一个Kubuntu时也失败了,所以我对此表示怀疑。
我有一个简短的简单C程序,应该调用汇编程序。我使用nasm编译汇编程序,并使用MingW的gcc实现编译c程序。两者通过makefile链接在一起-bog-simple。然而,链接失败,声称外部功能是“未定义的引用”
Makefile的相关部分:
assign0: ass0.o main.o
gcc -v -m32 -g -Wall -o assign0 ass0.o main.o
main.o: main.c
gcc -g -c -Wall -m32 -o main.o main.c
ass0.o: ass0.s
nasm -g -f elf -w+all -o ass0.o ass0.s
Run Code Online (Sandbox Code Playgroud)
汇编文件的开头:
section .data ; data section, read-write
an: DD 0 ; this is a temporary var
section .text ; our code is always in the .text section
global do_str ; makes the function appear in global scope
extern printf
do_str: ; functions are …Run Code Online (Sandbox Code Playgroud)