I have a server program which polls a database for new requests , I want this polling to be done at 1 minute intervals so , I've set up a Thread.sleep() in the program while loop.
The problem is that whenever this program is supposed to "sleep" the CPU consumption goes up drastically (viz. about 25 - 30%).
Paradoxically, when the program is not dormant and is busy processing requests , the CPU consumption drops to 0.4%.
I read online …
我只需要一个可以在字符数组中设置的字符,表示特定位置或一系列位置是空闲的,可用于存储数据.
我需要这个,因为我正在创建一个简单的内存池,需要重置释放的块并将它们指示为空闲.
任何帮助,将不胜感激.
我正在尝试使用在相当远的数据库上运行的预准备语句,用于访问此数据库的网络连接存在相当大的滞后和不可靠性.停机时间长达一分钟很常见.问题是,如果出现这样的故障,如果我的程序试图执行任何准备好的语句,整个线程就会进入无限等待状态.它永远不会超时,只是一直停滞不前,等待数据库的响应.
我尝试使用方法setQueryTimeout()在执行时显式地设置超时,但是,这个方法似乎存在一些问题,如果网络出现故障,它无法正常工作.
这有什么替代方法吗?
我的程序需要将此日期表示为java.sql.date对象,但似乎当我创建一个新日期(使用日历)并将其设置为'9999-12-31'并最终转换此java.util时.将对象更改为java.sql.date对象,此日期将转换为类似'000-01-31'的内容.
Calendar calendar = Calendar.getInstance();
calendar.set(9999, 12, 31);
infinityDate = new java.sql.Date(normalizeDate(calendar.getTime()).getTime());
Run Code Online (Sandbox Code Playgroud)
infinityDate应该是31-12-9999,但是当我的代码到达这里时:
if(otherDate.equals(infinityDate))
{// Do stuff}
Run Code Online (Sandbox Code Playgroud)
它永远不会进入if条件,因为infinityDate由于某种原因被改为31-01-000,即使otherDate实际上是'31 -12-9999'.
otherDate为31-12-9999的事实告诉我java可以表示这个日期,但出于某种原因,当我使用日历构建它时,它会更改日期.(otherDate来自一个从数据库中获取数据的jdbc语句)
此参考日期'31 -12-9999'已由某个客户修复,因此无法更改,我的程序必须能够将某些传入日期值与此进行比较.有谁知道为什么会发生这种情况,我意识到http://en.wikipedia.org/wiki/Year_10,000_problem可能是9999年后的日期问题,但我应该安全一天.
编辑:规范化日期方法仅"将给定日期规范化为当天的午夜"
private static java.util.Date normalizeDate(java.util.Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
date = calendar.getTime();
return date;
}
Run Code Online (Sandbox Code Playgroud)
但是,在我将日期标准化之前出现了这个问题,我将其标准化以试图解决这个问题.
我正在尝试使用rand()%(范围)生成一堆随机数.以下是我的代码设置方式:
srand(time(NULL));
someClass *obj = new someClass(rand()%range1,rand()%range2... etc ); // i.e. a number of random numbers one after the other
Run Code Online (Sandbox Code Playgroud)
每当我运行它时,似乎所有对rand()的调用都会生成相同的数字.我试过没有:(编辑:所有rand()不会生成相同的数字,最后读取编辑)
srand(time(NULL));
Run Code Online (Sandbox Code Playgroud)
然后,程序的每次执行都会产生相同的结果.
此外,由于对rand()的所有调用都在构造函数中,所以我不能一直重新调用它.我想我可以预先创建发送给构造函数的所有对象,然后在其间重新设置随机数生成器,但这似乎是一个不优雅的解决方案.
如何生成一堆不同的随机数?
编辑:看起来因为我在循环中创建了很多对象,所以每次重复循环srand(time(NULL))并重置序列时(因为time(NULL)的分辨率为秒),这就是为什么所有后续对象具有非常相似的属性.
我正在尝试编写一个霍夫曼编码器但是我遇到了一些压缩错误.我将问题识别为put()到ofstream的字符与来自同一文件的字符read()之间的不匹配.
这个问题的一个具体实例:
我认为阅读并写入原始数据(没有字符翻译)我不知道为什么会发生这种情况.有人可以帮我吗?
以下是编写压缩文件的ofstream实例:
std::ofstream compressedFileStream(getCompressedFileName(),std::ios::binary||std::ios::ate);
Run Code Online (Sandbox Code Playgroud)
以及用于读取相同内容的ifstream实例
std::ifstream fileInput(getFileName()+".huf",std::ios::binary);
Run Code Online (Sandbox Code Playgroud)
代码在Windows 7上运行,程序中的所有流都以二进制模式打开.
这个例子很容易解释我的问题:
class Vector3
{
float _x;
float _y;
float _z;
public :
/// constructors and stuff
};
class Point : public Vector3
{
// some BS
Point(float _x):Vector3(float _x)
{}
};
main()
{
Point aPoint(3);
Point anotherPoint(4);
// WHY DOES THIS WORK and copy _x,_y & _z properly
aPoint = anotherPoint;
}
Run Code Online (Sandbox Code Playgroud)
基本上,我无法理解为什么=派生类可以复制_x,_y并且_z,即使它不应该访问它们,因为它们是私有的.
好吧,我一直试图绕过这个问题一段时间了,但是我不明白,有人可以告诉我为什么案例#1会抛出一个断言(BLOCK TYPE IS INVALID)?
情况1
mehodName()
{
// Get all dependents for this resource
boost::shared_ptr<std::set<std::string>> dependents = deactivatedResource->getDependendents();
// Do some stuff
} // Assertion thrown here (heap gets corrupted)
Run Code Online (Sandbox Code Playgroud)
这是这种情况下的getDependents:
boost::shared_ptr<std::set<std::string>> Resource::getDependendents()
{
return boost::shared_ptr<std::set<std::string>>(&dependents);
}
Run Code Online (Sandbox Code Playgroud)
案例#2
mehodName()
{
// Get all dependents for this resource
std::set<std::string>* dependents = deactivatedResource->getDependendents();
} // No problem !! (but an obvious leak , if I try to use delete ,then the same assertion as in case 1)
Run Code Online (Sandbox Code Playgroud)
这是这种情况下的getDependents:
std::set<std::string>* Resource::getDependendents()
{
return …Run Code Online (Sandbox Code Playgroud)