buffer

[高并发Java 八] NIO和AIO

不问归期 提交于 2019-12-17 16:17:20
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> IO感觉上和多线程并没有多大关系,但是NIO改变了线程在应用层面使用的方式,也解决了一些实际的困难。而AIO是异步IO和前面的系列也有点关系。在此,为了学习和记录,也写一篇文章来介绍NIO和AIO。 1. 什么是NIO NIO是New I/O的简称,与旧式的基于流的I/O方法相对,从名字看,它表示新的一套Java I/O标 准。它是在Java 1.4中被纳入到JDK中的,并具有以下特性: NIO是基于块(Block)的,它以块为基本单位处理数据 (硬盘上存储的单位也是按Block来存储,这样性能上比基于流的方式要好一些) 为所有的原始类型提供(Buffer)缓存支持 增加通道(Channel)对象,作为新的原始 I/O 抽象 支持锁(我们在平时使用时经常能看到会出现一些.lock的文件,这说明有线程正在使用这把锁,当线程释放锁时,会把这个文件删除掉,这样其他线程才能继续拿到这把锁)和内存映射文件的文件访问接口 提供了基于Selector的异步网络I/O 所有的从通道中的读写操作,都要经过Buffer,而通道就是io的抽象,通道的另一端就是操纵的文件。 2. Buffer Java中Buffer的实现。基本的数据类型都有它对应的Buffer Buffer的简单使用例子: package test; import

What goes on behind the curtains during disk I/O?

£可爱£侵袭症+ 提交于 2019-12-17 15:44:33
问题 When I seek to some position in a file and write a small amount of data (20 bytes), what goes on behind the scenes? My understanding To my knowledge, the smallest unit of data that can be written or read from a disk is one sector (traditionally 512 bytes, but that standard is now changing). That means to write 20 bytes I need to read a whole sector, modify some of it in memory and write it back to disk. This is what I expect to be happening in unbuffered I/O. I also expect buffered I/O to do

How to garbage collect a direct buffer in Java

痴心易碎 提交于 2019-12-17 15:36:39
问题 I have a memory leak that I have isolated to incorrectly disposed direct byte buffers. ByteBuffer buff = ByteBuffer.allocateDirect(7777777); The GC collects the objects that harbor these buffers but does not dispose of the buffer itself. If I instantiate enough of the transient objects containing buffers, I get this encouraging message: java.lang.OutOfMemoryError: Direct buffer memory I have been searching up this problem and apparently buff.clear(); and System.gc(); do not work. 回答1: I

Determine if there is Data left on the socket and discard it

不羁的心 提交于 2019-12-17 14:56:55
问题 I'm writing an Interface under Linux which gets Data from a TCP socket. The user provides a Buffer in which the received Data is stored. If the provided Buffer is to small I just want to return an Error. First problem is to determine if the Buffer was to small. The recv() function just returns me the amount of bytes actually written into the Buffer. If I use the MSG_TRUNC flag stated on the recv() manpage it still returns me the same. Second problem is to discard the data still queued in the

When does cout flush?

萝らか妹 提交于 2019-12-17 14:45:48
问题 I know endl or calling flush() will flush it. I also know that when you call cin after cout , it flushes too. And also when the program exit. Are there other situations that cout flushes? I just wrote a simple loop, and I didn't flush it, but I can see it being printed to the screen. Why? Thanks! for (int i =0; i<399999; i++) { cout<<i<<"\n"; } Also the time for it to finish is same as with endl both about 7 seconds. for (int i =0; i<399999; i++) { cout<<i<<endl; } 回答1: There is no strict

C++ cout and cin buffers, and buffers in general

青春壹個敷衍的年華 提交于 2019-12-17 10:42:55
问题 Can someone explain the concept of buffers a bit more explicitly? I understand that buffers are data structures where characters are stored, and the place where the data is to be read from. What is the idea of flushing buffers? When a buffer is flushed, is this referring to the act of writing the characters stored in it? From the text: To avoid the overhead of writing in response to each output request, the library uses the buffer to accumulate the characters to be written, and flushes the

How do you flush a buffered log4j FileAppender?

淺唱寂寞╮ 提交于 2019-12-17 09:33:57
问题 In log4j, when using a FileAppender with BufferedIO=true and BufferSize=xxx properties (i.e. buffering is enabled), I want to be able to flush the log during normal shutdown procedure. Any ideas on how to do this? 回答1: public static void flushAllLogs() { try { Set<FileAppender> flushedFileAppenders = new HashSet<FileAppender>(); Enumeration currentLoggers = LogManager.getLoggerRepository().getCurrentLoggers(); while(currentLoggers.hasMoreElements()) { Object nextLogger = currentLoggers

Does reactive extensions support rolling buffers?

雨燕双飞 提交于 2019-12-17 07:23:27
问题 I'm using reactive extensions to collate data into buffers of 100ms: this.subscription = this.dataService .Where(x => !string.Equals("FOO", x.Key.Source)) .Buffer(TimeSpan.FromMilliseconds(100)) .ObserveOn(this.dispatcherService) .Where(x => x.Count != 0) .Subscribe(this.OnBufferReceived); This works fine. However, I want slightly different behavior than that provided by the Buffer operation. Essentially, I want to reset the timer if another data item is received. Only when no data has been

Does reactive extensions support rolling buffers?

百般思念 提交于 2019-12-17 07:23:08
问题 I'm using reactive extensions to collate data into buffers of 100ms: this.subscription = this.dataService .Where(x => !string.Equals("FOO", x.Key.Source)) .Buffer(TimeSpan.FromMilliseconds(100)) .ObserveOn(this.dispatcherService) .Where(x => x.Count != 0) .Subscribe(this.OnBufferReceived); This works fine. However, I want slightly different behavior than that provided by the Buffer operation. Essentially, I want to reset the timer if another data item is received. Only when no data has been

cin for an int inputing a char causes Loop that is supposed to check input to go wild

ⅰ亾dé卋堺 提交于 2019-12-17 06:49:06
问题 This is a function of my game it will ask for input and cin into "iAuswahl"! Then the while loop is checks if it is one of the Values i want 1-9 if not it activates and is supposed to ask for new input. Witch it does for int. But if i input a char like r it will go crazy and just keep giving me back my cout and skip the cin! My questions are why does it do it and how do i stop it? void zug(string sSpieler, int iDran){ int iAuswahl; char cXO = 'O'; if (iDran == 1) { cXO = 'X'; } cout <<