假设我们有这样一个程序:
// imagine the string1 to string1000 are very long strings, which will take a while to be written to file system
var arr = ["string1",...,"string1000"];
for (let i = 1; i < 1000; i++) {
fs.write("./same/path/file.txt", arr[i], {flag: "a"}});
}
Run Code Online (Sandbox Code Playgroud)
我的问题是, will string1 to string1000 be gurantted to append to the same file in order?
由于fs.write是异步函数,我不确定每次调用fs.write()是如何执行的.我假设对每个字符串的函数调用应该放在某处another thread(比如callstack?),一旦完成上一次调用,就可以执行下一个调用.
我不确定我的理解是否准确.
编辑1
在评论和答案中,我看到fs.write多次写入同一文件而不等待是不安全的callback.但是如何写入流?
如果我使用以下代码,它会保证写作的顺序吗?
// imagine the string1 to string1000 are very long strings, …Run Code Online (Sandbox Code Playgroud) 我试图理解java unsafe中的两个方法:
public native short getShortVolatile(Object var1, long var2);
Run Code Online (Sandbox Code Playgroud)
VS
public native short getShort(Object var1, long var2);
Run Code Online (Sandbox Code Playgroud)
这有什么真正的区别?这里的挥发性真正起作用了什么?我在这里找到了API doc:http://www.docjar.com/docs/api/sun/misc/Unsafe.html#getShortVolatile( Object,%20long)
但它并没有真正解释两种功能之间的区别.
我的理解是,对于不稳定的,它只对我们写作时很重要.对我来说,我们调用putShortVolatile然后进行读取应该是有意义的,我们可以简单地调用,getShort()因为volatile写入已经保证新值已经刷新到主存储器中.
如果有任何问题,请帮我纠正.谢谢!
假设我有这样的代码片段:
try {
// code I need to wrap to be a helper
long t0 = System.nanoTime();
obj.doSomething(); // a void function
long t1 = System.nanoTime();
Logger.info("doSomthing takes {} nanoseconds", t1-t0);
} catch (IOException ex) {
Logger.error("something wrong happened");
}
// another code
try {
long t0 = System.nanoTime();
obj.doAnotherThing(); // void function
long t1 = System.nanoTime();
Logger.info("doSomthing takes {} nanoseconds", t1-t0);
} catch (IOException ex) {
Logger.error("something wrong happened");
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题实际上是如何将该void函数作为参数传递给辅助函数,以便我可以避免冗余代码来测量函数的执行时间.
跟进:如果doSomething可以抛出IOException怎么办?
在这种情况下,如果我不想在labmda中捕获异常,我应该如何调用lambda世界中的函数.
谢谢
这让我感到惊讶,我正在玩Java Unsafe.基本上我正在测试的是
Allocate unsafe memory -> free the memory -> Write to the freed memory
Run Code Online (Sandbox Code Playgroud)
当我访问被释放的内存时,我期待看到某种分段错误错误,但令人惊讶的是,没有引发错误/异常.
我的代码是:
protected static final Unsafe UNSAFE;
static {
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
UNSAFE = (Unsafe) field.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void test() {
long ptr = UNSAFE.allocateMemory(1000);
UNSAFE.freeMemory(ptr);
UNSAFE.putOrderedLong(null, ptr, 100L);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果是这样,为什么我们需要freeMemory()不安全的功能?真的是什么?
好吧,我刚刚下载了源代码,我确信它是正确的。但是当我使用 make 命令来制作文件后。出现错误:我的笔记本电脑是Mac-air
/usr/include/c++/4.2.1/cstdarg:50:10: fatal error: 'stdarg.h' file not found
有人可以帮我解决这个问题吗?
谢谢
我有这样一个函数,它创建一个写流,然后将字符串数组写入文件.写完后,我想让它返回一个Promise.但我不知道如何才能做到这一点.
function writeToFile(filePath: string, arr: string[]): Promise<boolean> {
const file = fs.createWriteStream(filePath);
arr.forEach(function(row) {
file.write(row + "\n");
});
file.end();
file.on("finish", ()=>{ /*do something to return a promise but I don't know how*/});
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的评论!
这个问题困扰我好几天了:
假设我有两个在同一台计算机上运行的进程(p_write和p_read)。
过程p_write用于写入/更新mmap文件。
过程p_read是使用mmap文件的过程,换句话说,就是读取mmap文件。
我的假设是,p_write首先需要为mmap文件分配一个内存空间(堆外)(该空间使用Java MappedByteBufferAPI 自动映射到文件)。
我的问题是p_read如何从mmap文件读取?我现在的假设是,p_read还需要为要映射的mmap文件分配另一个相同大小的堆外空间,但这似乎是不正确的,因为在这种情况下,内存量需要加倍。
如果p_read不需要为要映射的mmap文件分配单独的内存空间,那么如何p_read知道文件映射到的正确内存地址p_write?
更新1
我发现有一个更好的问题要问,或者您可以将其视为后续问题:如果FileChannel.map()两次调用,同一文件是否将两次映射到两个不同的内存空间?
// Scenario A: In single process
try (FileChannel fc = FileChannel.open(filePath, openOptions)) {
// First call
fc.map(MapMode.READ_ONLY, 0, SIZE_CONSTANT);
// Second call
fc.map(MapMode.READ_ONLY, 0, SIZE_CONSTANT);
}
Run Code Online (Sandbox Code Playgroud)
和
// Scenario B: In two processes
// in first process
try (FileChannel fc = FileChannel.open(filePath, openOptions)) {
// First …Run Code Online (Sandbox Code Playgroud) java memory filechannel memory-mapped-files mappedbytebuffer
java ×4
fs ×2
javascript ×2
node.js ×2
unsafe ×2
asynchronous ×1
bytebuffer ×1
c++ ×1
filechannel ×1
function ×1
java-8 ×1
lambda ×1
memory ×1
promise ×1
typescript ×1
volatile ×1