给出以下代码而不考虑两个类之间的友谊:
class OutSideClass
{
...
public:
int i_pub;
protected:
int i_pro;
private:
int i_pri;
class InSideClass
{
...
public:
int j_pub;
protected:
int j_pro;
private:
int j_pri;
};
};
Run Code Online (Sandbox Code Playgroud)
问题1> OutSideClass是否只能访问InSideClass的公共成员
问题2> InSideClass是否可以访问OutSideClass的所有成员
如果我的理解不正确,请纠正我.
我的问题:
JVM中的大量线程是否会消耗大量资源(内存,CPU),当线程处于TIMED_WAIT状态(未休眠)时> 99.9%的时间?当线程在等待时,如果需要任何线程,维护它们需要多少CPU开销?
答案是否也适用于非JVM相关环境(如Linux内核)?
语境:
我的程序收到大量空间消耗包.它在不同的包中存储类似属性的计数.在收到包裹后的一段时间(可能是几小时或几天)之后,该特定包裹到期并且包裹所贡献的任何计数应该减少.
目前,我通过将所有包存储在内存或磁盘中来实现这些功能.每隔5分钟,我从存储中删除过期的包,并扫描剩余的包以计算属性.这种方法耗费了大量内存,并且时间复杂度很低(O(n)对于时间和内存,其中n是未到期的包的数量).这使程序的可扩展性变得可怕.
解决此问题的另一种方法是每次打包时递增属性计数,并启动一个Timer()在程序包到期后递减属性计数的线程.这消除了存储所有庞大的包装并减少时间复杂性的需要O(1).但是,这会产生另一个问题,因为我的程序将开始拥有O(n)多个线程,这可能会降低性能.由于大多数线程将在其生命周期的绝大部分时间处于TIMED_WAIT状态(Java Timer()调用Object.wait(long)方法),它是否仍会以非常大的方式影响CPU?
题:
我有一个服务,它接受一个JSON字符串作为输入.该JSON模式的每一个地方有些领域并不总是存在的时间是不同的.如何在使用Jayway JsonPath时查询这些字段的值?
我尝试过的:
我使用了Option.DEFAULT_PATH_LEAF_TO_NULLJayway的自述页面解释
Configuration config = Configuration.defaultConfiguration()
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
if (JsonPath.isPathDefinite(attribute.jsonPath))
{
String value = JsonPath.using(config).parse(currentTx).read(attribute.jsonPath);
if (value != null)
{
attributeValues.add(value);
}
}
else
{
List<String> attributeValuesArray = JsonPath.using(config).parse(currentTx).read(attribute.jsonPath);
for (String value : attributeValuesArray)
{
if (value != null)
{
attributeValues.add(value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果找不到路径,这应该JsonPath.read()返回null,但是我的代码仍会抛出:
com.jayway.jsonpath.PathNotFoundException:路径$ ['somepath']中缺少属性
当我给它一条不存在的道路时.有谁知道这可能导致什么?
参考以下代码
test_linker.cpp
int main() {
srand(time(0));
for (int i = 0; i < 10; ++i) {
cout << rand() % 10 << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
urandom.cpp
#include <iostream>
using std::cout;
using std::endl;
#include <dlfcn.h>
int rand() throw() {
// get the original rand() function
static auto original_rand = (decltype(&rand)) dlsym(RTLD_NEXT,"rand");
cout << "Call made to rand()" << endl;
return original_rand();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下命令编译代码时
g++ -std=c++11 -Wall -Werror -Wextra -Wvla -pedantic -O3 urandom.cpp -c
g++ -std=c++11 -Wall -O3 test_linker.cpp urandom.o …Run Code Online (Sandbox Code Playgroud) 我在mongo中有以下文档:
> { "_id": ObjectId("569afce4b932c542500143ec"),
> "date": "2016-1-17T2:31:0Z",
> "day": NumberInt(17),
> "model1": {
> "date": "2016-01-17T02:31+0000",
> "MondayModel": {
> "gtxdotdot": {
> "xdotdot": 0,
> "xdot": 0
> },
> "lsxdotdot": {
> "xdotdot": 0,
> "xdot": 0
> },
> "gtxdot": {
> "xdotdot": 0,
> "xdot": 0
> },
> "lsxdot": {
> "xdotdot": 0,
> "xdot": 0
> },
> "modeldotdot": {
> "mean": 0,
> "sdvar": 0
> },
> "modeldot": {
> "mean": …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用分析日期SimpleDateFormat.由于我的服务采用多种日期格式,我采用了这种方法:
String[] formats = {
"yyyy-MM-dd'T'HH:mm:ss.SSSZ",
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd'T'HH:mm:ss.SSS-HH:mm",
"EEE MMM dd HH:mm:ss Z yyyy"};
for (String format : formats)
{
try
{
return new SimpleDateFormat(format).parse(dateString);
}
catch (ParseException e) {}
}
return null;
Run Code Online (Sandbox Code Playgroud)
其背后的基本原理try-catch是,如果当前日期格式无法解析dateString,Exception则会抛出一个并且代码将继续循环,直到找到合适的日期格式或返回null.
该catch块简直就是有那么该try块将具有以下一些东西(如果没有遵循可能发生编译错误try).
我可以保持代码原样,但空catch块是不好的做法.将来维持其他人也会感到困惑.它简直不优雅.
我可以在catch块中添加以下内容:
catch (Exception e)
{
if (!(e instanceof ParseException))
{
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
但同样,内部的代码没有任何意义,因为没有Exception其他的ParseException可以被try块抛出(我NullPointerException …
我在其中一个.h文件中设置了一个断点,它有一个小方法的实现,
(gdb) break SmallVector.h:141
Run Code Online (Sandbox Code Playgroud)
这是我从gdb得到的:
Breakpoint 5 at 0x416312: SmallVector.h:141. (38 locations)
Run Code Online (Sandbox Code Playgroud)
为什么断点设置在38个位置而不是单个位置?
我不是新手调试和C++,但不幸的是我从来没有像我现在工作的那样复杂(编译器).所以我以前从未遇到过这样的事情.
任何帮助表示赞赏.
我在Idris中玩一些形式化的游戏,并且有一些奇怪的行为:函数的编译时间和CPU使用率很高。
该代码是一个正则表达式模式匹配算法。首先是正则表达式定义:
data RegExp : Type where
Zero : RegExp
Eps : RegExp
Chr : Char -> RegExp
Cat : RegExp -> RegExp -> RegExp
Alt : RegExp -> RegExp -> RegExp
Star : RegExp -> RegExp
Comp : RegExp -> RegExp
Run Code Online (Sandbox Code Playgroud)
正则表达式的成员资格和非成员资格定义为以下相互递归的数据类型:
mutual
data NotInRegExp : List Char -> RegExp -> Type where
NotInZero : NotInRegExp xs Zero
NotInEps : Not (xs = []) -> NotInRegExp xs Eps
NotInChr : Not (xs = [ c ]) -> NotInRegExp xs …Run Code Online (Sandbox Code Playgroud) 问题)
在Lua:
local a = b or 0
local a = b and 1 or 0
Run Code Online (Sandbox Code Playgroud)
其中 b 可以是任何类型。
代码行之间有什么区别?在什么情况下我会使用其中之一?
语境
我必须将现有Lua代码移植到另一项服务,并且遇到了一个问题,无法理解为什么在代码的某些部分(我不是开发Lua人员),一个变量被分配给一个服务,而在代码的其他部分,一个变量被分配到另一个。右侧的变量是输入参数,我无法知道预期的类型。
我尝试过的
我在网上查找了有关此问题的 Lua 文档,但找不到任何明确的答案。我进行了自己的测试:
local a1;
print(type(a1)) -- nil
local b1 = a1 or 0
print(b1 .. " " .. type(b1)) -- 0 number
local c1 = a1 and 1 or 0
print(c1 .. " " .. type(c1)) -- 0 number
local a2 = 5
print(type(a2)) -- number
local b2 = a2 …Run Code Online (Sandbox Code Playgroud) c++ ×4
java ×3
c++11 ×1
command-line ×1
control-flow ×1
date-format ×1
debugging ×1
exception ×1
ffmpeg ×1
gdb ×1
idris ×1
jpeg ×1
json ×1
jsonpath ×1
linux ×1
lua ×1
mongo-java ×1
mongodb ×1
null ×1
oop ×1
performance ×1
png ×1
timer ×1
try-catch ×1