首页 \ 问答 \ AtomicReference的等价物,但没有不稳定的同步成本(Equivalent of AtomicReference but without the volatile synchronization cost)

AtomicReference的等价物,但没有不稳定的同步成本(Equivalent of AtomicReference but without the volatile synchronization cost)

相当于什么:

AtomicReference<SomeClass> ref = new AtomicReference<SomeClass>( ... );

但没有同步成本。 请注意,我确实想要在另一个对象中包装引用。

我已经看过扩展Reference抽象类的类,但是我在所有的选择中都有点失落。

我需要一些非常简单的,不是虚弱的,也不是幻影,也不需要其他所有参考。 我应该使用哪个课程?


What is the equivalent of:

AtomicReference<SomeClass> ref = new AtomicReference<SomeClass>( ... );

but without the synchronization cost. Note that I do want to wrap a reference inside another object.

I've looked at the classes extending the Reference abstract class but I'm a bit lost amongst all the choices.

I need something really simple, not weak nor phantom nor all the other references besides one. Which class should I use?


原文:https://stackoverflow.com/questions/8344509
更新时间:2023-09-11 09:09

最满意答案

不完全,但这里有5种模式可以让你接近:

namespace My.Namespace
{
    using H = MyHelperClass;

    public class MyHelperClass
    {
        public static void HelperFunc1()
        {
            Console.WriteLine("Here's your help!");
        }
    }

    public class MyHelperClass2
    {
        public static void HelperFunc4()
        {
            Console.WriteLine("Here's your help!");
        }
    }

    public interface IHelper{ }

    public static class HelperExtensions
    {
        public static void HelperFunc3(this IHelper self)
        {
            Console.WriteLine("Here's your help!");
        }
    }

    public class MyClass : MyHelperClass2, IHelper
    {
        private static readonly Action HelperFunc2 = MyHelperClass.HelperFunc1;

        private static void HelperFunc5() 
        {
            Console.WriteLine("Here's your help!");
        }

        public void MyFunction()
        {
            //Method 1 use an alias to make your helper class name shorter
            H.HelperFunc1();
            //Method 2 use a class property
            HelperFunc2();
            //Method 3 extend an interface that has extension methods.
            //Note: you'll have to use the this keyword when calling extension
            this.HelperFunc3();
            //Method 4 you have access to methods on classes that you extend.
            HelperFunc4();
            //Method 5 put the helper method in your class
            HelperFunc5();
        }
    }
}

Not quite, but here are 5 patterns that get you close:

namespace My.Namespace
{
    using H = MyHelperClass;

    public class MyHelperClass
    {
        public static void HelperFunc1()
        {
            Console.WriteLine("Here's your help!");
        }
    }

    public class MyHelperClass2
    {
        public static void HelperFunc4()
        {
            Console.WriteLine("Here's your help!");
        }
    }

    public interface IHelper{ }

    public static class HelperExtensions
    {
        public static void HelperFunc3(this IHelper self)
        {
            Console.WriteLine("Here's your help!");
        }
    }

    public class MyClass : MyHelperClass2, IHelper
    {
        private static readonly Action HelperFunc2 = MyHelperClass.HelperFunc1;

        private static void HelperFunc5() 
        {
            Console.WriteLine("Here's your help!");
        }

        public void MyFunction()
        {
            //Method 1 use an alias to make your helper class name shorter
            H.HelperFunc1();
            //Method 2 use a class property
            HelperFunc2();
            //Method 3 extend an interface that has extension methods.
            //Note: you'll have to use the this keyword when calling extension
            this.HelperFunc3();
            //Method 4 you have access to methods on classes that you extend.
            HelperFunc4();
            //Method 5 put the helper method in your class
            HelperFunc5();
        }
    }
}

相关问答

更多
  • 一种解决方案是在专用单例中存储在引导程序中创建的注入器的引用。 bootstrap(App, [Auth, ROUTER_PROVIDERS]) .then((appRef: ComponentRef) => { // store a reference to the injector appInjector(appRef.injector); }); 单身人士如下: let appInjectorRef: Injector; export const appInjector = (injecto ...
  • 既然你声明你已经明白什么是static ,我会跳过它。 但是,引用关于static关键字的 PHP 文档可能仍然很好。 特别是以下两个警报很重要(真的很难看清楚)。 警告在PHP 5中,静态调用非静态方法会生成E_STRICT级别警告。 而这一个(斜体强调我的)。 警告在PHP 7中,静态调用非静态方法已被废弃,并将生成E_DEPRECATED警告。 支持静态调用非静态方法将来可能会被删除。 所以,简而言之就是:您的示例将运行(暂时),因为PHP解释器会尝试修复您的错误。 但是,你应该永远不要这样做 。 P ...
  • 你有一些选择。 您可以使用NSStrings实际拥有方法的名称,然后执行以下操作: NSString *myMethodName = @"hellWorld"; SEL selector = selectorFromString(myMethodName); 然后,您可以将选择器添加到NSArray 。 您还可以使用NSInvocations并存储方法并在以后定义目标: NSInvocation对象包含Objective-C消息的所有元素:目标,选择器,参数和返回值。 可以直接设置这些元素中的每一个,并在 ...
  • 在这种情况下,对实例方法append未绑定引用具有相同的arity,参数类型甚至返回值作为对静态方法append的引用,所以不,您无法解决方法引用的消歧。 如果您不想重命名其中一个方法,则应使用lambda: collect(StringCollector::new, (sb, s) -> sb.append(s), StringCollector::concat); 或者,如果您确实想使用静态方法: collect(StringCollector::new, (sb, s) -> StringColle ...
  • 真的没有理由为什么你可以这样做。 我唯一的猜测是,它可以让你覆盖静态方法,但你不能 。 如果您尝试以下情形: 香蕉有一个称为'测试'的静态方法(打印'香蕉')苹果延伸香蕉和“覆盖”静态方法称为'测试'(这打印'苹果') 你做这样的事情: public static void main(String[] args) { Apple apple = new Apple(); Banana banana = new Banana(); Banana base = new Apple(); ...
  • 好吧,除了你的ClassA构造函数格式错误(你错过了括号)之外,该代码编译 ......但是在构造ClassA的实例之前, PointerToB和PointerToC都将为null。 如果您只想初始化它们一次,请在静态初始化程序中执行 - 例如使用声明: public static ClassB PointerToB = new ClassB(); public static ClassC PointerToC = new ClassC(); 即便如此,这里有两个令人讨厌的设计(IMO): 公共可变字段几 ...
  • 不完全,但这里有5种模式可以让你接近: namespace My.Namespace { using H = MyHelperClass; public class MyHelperClass { public static void HelperFunc1() { Console.WriteLine("Here's your help!"); } } public class MyHelpe ...
  • 静态方法可以调用实例方法 - 但是你需要有一个实例来调用它们。 这个例子来自哪里并不重要,例如: int a = 10; int b = 100; Program program = new Program(); program.outTestMethod(a,out b); 实例方法与特定类型的实例相关联,而静态方法与整体类型相关 - 而其他类型的成员也是如此。 因此,要调用一个实例方法,您需要知道您感兴趣的实例。例如,具有以下内容是没有意义的: string name = Person.Name; ...
  • 您可以将嵌套类设置为static 。 您将失去具有封闭父类实例的约束,但它将允许您从静态方法操作DoubleNode : // This will compile public class DoublyLinkedList { public static void insertStart(DoublyLinkedList list, DoubleNode first) { // implementation } private st ...
  • PLUS = 0 MINUS = 1 OPERATIONS = [PLUS, MINUS] class Generator: operations = {} def __init__(self, state): self._state = state @classmethod def init_operations(cls): cls.operations = { PLUS: cls.plus, ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。