JDK 1.6.x G1的经验(“垃圾第一”)(Experience with JDK 1.6.x G1 (“Garbage First”))
我想知道在最新的JDK中使用G1垃圾收集器的经验是什么? 虽然在早期的JDK中代码没有改变并且行为正确,但我在程序中看到了抛出的
NullPointerException
异常。I'd like to know what are the experiences with G1 garbage collector in newest JDK? I see
NullPointerException
thrown in my program, although code didn't change and behave correctly in earlier JDKs.
原文:https://stackoverflow.com/questions/986974
最满意答案
要回答你关于“这样做的标准方法是什么”的问题,我假设你的意思是传递成员函数和/或一般函数指针并用一些数据执行它们。 一些提供此功能的流行实现是:
这真的取决于偏好和图书馆的选择。 就个人而言,我大部分时间都使用过FastDelegate,然后是std :: function。
我发布的所有链接都应该有教程信息,以帮助您启动并运行,并向您展示如何正确地传递和存储成员函数和/或一般函数指针。
以下是使用FastDelegate的示例:
class A { public: // [1] This creates a delegate type. Can used for any function, // class function, static function, that takes one int // and has a return type of an int. typedef FastDelegate1< int, int > Delegate; // [2] Pass the delegate into 'A' and save a copy of it. A( const Delegate& delegate ) : _delegate( delegate ) { }; void execute() { // [7] // Result should be 10! int result = _delegate( 8 ); } private: // [3] Storage to save the Delegate in A. Delegate _delegate; }; class B { public: B() { // [4] Create the delegate A::Delegate bDelegate; bDelegate.bind( this, &B::function ); // [5] Create 'A' passing in the delegate. _aPtr = new A( bDelegate ); // [6] Test it out!! :) // This causes `A` to execute the Delegate which calls B::function. _aPtr->execute(); } ~B() { delete _aPtr; } int function( int n ) { return n+2; } private: A* _aPtr; };
To answer your question regarding "What's the standard method of doing something like this" I'll assume you mean passing member functions and/or general function pointers around and executing them with some data. Some popular implementations which provide this ability are:
It really comes down to preference and library choice. Personally, I've used FastDelegate most of the time and then std::function after that.
All the links I posted should have tutorial information to get you up and running and show you how to properly pass and store member functions and/or general function pointers with ease.
Here's an example of using a FastDelegate with your example:
class A { public: // [1] This creates a delegate type. Can used for any function, // class function, static function, that takes one int // and has a return type of an int. typedef FastDelegate1< int, int > Delegate; // [2] Pass the delegate into 'A' and save a copy of it. A( const Delegate& delegate ) : _delegate( delegate ) { }; void execute() { // [7] // Result should be 10! int result = _delegate( 8 ); } private: // [3] Storage to save the Delegate in A. Delegate _delegate; }; class B { public: B() { // [4] Create the delegate A::Delegate bDelegate; bDelegate.bind( this, &B::function ); // [5] Create 'A' passing in the delegate. _aPtr = new A( bDelegate ); // [6] Test it out!! :) // This causes `A` to execute the Delegate which calls B::function. _aPtr->execute(); } ~B() { delete _aPtr; } int function( int n ) { return n+2; } private: A* _aPtr; };
相关问答
更多-
要回答你关于“这样做的标准方法是什么”的问题,我假设你的意思是传递成员函数和/或一般函数指针并用一些数据执行它们。 一些提供此功能的流行实现是: FastDelegate 的std ::功能 提振::函数 这真的取决于偏好和图书馆的选择。 就个人而言,我大部分时间都使用过FastDelegate,然后是std :: function。 我发布的所有链接都应该有教程信息,以帮助您启动并运行,并向您展示如何正确地传递和存储成员函数和/或一般函数指针。 以下是使用FastDelegate的示例: class A ...
-
是。 只要不直接或间接访问未初始化的成员或虚拟函数 ,因为对象尚未完全构建,就可以使用this指针在初始化列表中 。 对象child可以存储this Parent指针,供以后使用! Yes. It's safe to use this pointer in initialization-list as long as it's not being used to access uninitialized members or virtual functions, directly or indirectly ...
-
A::cbPtr类型需要指向A类的非静态成员函数的指针。 但是您尝试将指向非成员函数的指针分配给静态cbptr变量。 它们是两种不同的类型,这就是代码不能编译的原因。 从您的cbPtr typedef中删除A:: ,例如: #include
//external callback function static void innerFunc(int i, float f) { printf("running inner function : %i %f\n", i, f); } ... -
获取类对象的指针(Get pointer of class object)[2022-04-10]
找到特定类的所有当前对象真的没有好办法,但你可以使用whos来获取关于所有变量的结构,循环遍历这个并确定哪些具有你的属性然后修改 variables = whos; for k = 1:numel(variables) obj = eval(variables(k).name); if isobject(obj) && isprop(obj, 'color') obj.color = 'yellow'; end end 如果您正在寻找特定的类,则可以使用who ... -
函数指针数组初始化(Function Pointer Array Initialization)[2022-10-13]
我不认为你必须初始化数组,但我鼓励你这样做。 在你的情况下数组默认初始化的原因是你提供了一个构造函数,并且没有提到初始值设定项列表中的数据成员(cf. cppreference / default initialization ): 默认初始化在三种情况下执行: ... 3)当一个基类或一个非静态数据成员没有在构造函数初始化列表中被提及并且该构造函数被调用时。 因此,默认初始化将发生。 无论如何,为了表达您依赖数据成员为“零”,而不以其他方式强制未初始化的条目将被访问,我会使初始化显式化,例如 class ... -
如何使用函数指针初始化类中的对象?(How do I use function pointers to initialize an object inside a class?)[2022-11-12]
除非是静态成员函数,否则不能将成员函数作为普通函数指针传递。 static int funct(myType); 当然,静态函数不能访问非静态的成员变量或函数,除非您将它们与另一个对象指针结合使用。 您还可以将传递给构造函数的指针类型更改为成员指针,但这更加复杂,除非您还传递该类型的对象,否则通常不会有用。 Subclass(char* t,Container* cp, int((Container::*)i)(myType),int a,int b,int c,int p) : Superclass ... -
将函数指针返回到main()中初始化的对象(Return a pointer from a function to an object initialized in main())[2021-11-16]
你写了 values &getPointerToStruct(big *con) { values *return_vals; if(con->small_anchor->vals.val==10) return_vals=con->small_anchor->vals; return (&return_vals); } 我认为你想要的地方 values *getPointerToStruct(big *con) { values *return_vals ... -
如果呼叫机构的类型未存储在某个可访问的地方,则不能在呼叫站点调用未知类型的函数对象。 有两种选择: 如果您可以使用C ++ 11或boost,则可以使用std::function resp。 boost::function : class A { public: // ... void doSomeJob(int x) { functor(x); } private: std::function
functor; // or boost::function ... -
如果对象isNew()没有ID,则无法在另一个上设置它的引用。 它的工作原理与此类似,因为数据库中尚不存在新对象。 如果您需要在此保存之前存在对象的信息,请使用afterSave hook和afterSave existed() 。 If the object isNew() is doesn't have an ID and you cannot set it's reference on another. It works like that because new object doesn't exi ...
-
函数F()不是虚函数,这意味着函数调用将静态调度到指针/引用的静态类型中的版本,而不是让它在运行时找到对象的动态类型。 如果您符合您感兴趣的变体,则可以从指向Derived的指针访问相同的函数: pDerived->Base::F(); The function F() is not virtual, which means that the function call will be statically dispatched to the version in the static type of t ...