bytebuffer

Channel

主宰稳场 提交于 2019-11-27 07:18:37
在BIO编程中,每一个客户端连接请求过来,对于输入流,必须有单独的线程监听,看是否有数据到达,对于输出流,可以采用一个线程池管理,这样服务端的线程数量至少为n 下面例子是NIO中采用Channel+线程池方式,有个缺点是不知道SocketChannel是否有数据到达了,必须迭代所有的SocketChannel,如果有数据到达,有就处理,否则就跳过,效率太低 public class TimeServer { private BlockingQueue<SocketChannel> idleQueue =new LinkedBlockingQueue<SocketChannel>(); private BlockingQueue<Future<SocketChannel>> workingQueue=new LinkedBlockingQueue<Future<SocketChannel>>(); private ExecutorService executor = Executors.newSingleThreadExecutor(); { new Thread(){ @Override public void run() { try { while (true) { //task1:迭代当前idleQueue中的SocketChannel,提交到线程池中执行任务

Why the odd performance curve differential between ByteBuffer.allocate() and ByteBuffer.allocateDirect()

浪子不回头ぞ 提交于 2019-11-27 05:01:18
问题 I'm working on some SocketChannel -to- SocketChannel code which will do best with a direct byte buffer--long lived and large (tens to hundreds of megabytes per connection.) While hashing out the exact loop structure with FileChannel s, I ran some micro-benchmarks on ByteBuffer.allocate() vs. ByteBuffer.allocateDirect() performance. There was a surprise in the results that I can't really explain. In the below graph, there is a very pronounced cliff at the 256KB and 512KB for the ByteBuffer

What is the use of ByteBuffer in Java? [closed]

偶尔善良 提交于 2019-11-27 04:09:51
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . What are example applications for a ByteBuffer in Java? Please list any example scenarios where this is used. Thank you! 回答1: This is a good description of its uses and shortcomings. You essentially use it whenever you need to do fast low-level I/O. If you were going to implement

Gets byte array from a ByteBuffer in java

放肆的年华 提交于 2019-11-27 03:37:05
Is this the recommended way to get the bytes from the ByteBuffer ByteBuffer bb =.. byte[] b = new byte[bb.remaining()] bb.get(b, 0, b.length); Depends what you want to do. If what you want is to retrieve the bytes that are remaining (between position and limit), then what you have will work. You could also just do: ByteBuffer bb =.. byte[] b = new byte[bb.remaining()] bb.get(b); which is equivalent as per the ByteBuffer javadocs. R4zorax Note that the bb.array() doesn't honor the byte-buffers position, and might be even worse if the bytebuffer you are working on is a slice of some other buffer

JAVA堆外内存

ぃ、小莉子 提交于 2019-11-27 02:25:22
JVM可以使用的内存分外2种: 堆内存和堆外内存 . 堆内存完全由JVM负责分配和释放,如果程序没有缺陷代码导致内存泄露,那么就不会遇到java.lang.OutOfMemoryError这个错误。 使用堆外内存,就是为了能直接分配和释放内存,提高效率。JDK5.0之后,代码中能直接操作本地内存的方式有2种:使用未公开的Unsafe和NIO包下ByteBuffer。 关于Unsafe对象的简介和获取方式,可以参考:http://blog.csdn.net/aitangyong/article/details/38276681 使用ByteBuffer分配本地内存则非常简单,直接ByteBuffer.allocateDirect(10 * 1024 * 1024)即可。 C语言的内存分配和释放函数malloc/free,必须要一一对应,否则就会出现内存泄露或者是野指针的非法访问。java中我们需要手动释放获取的堆外内存吗? 我们一起来看看NIO中提供的ByteBuffer 我们将最大堆外内存设置成40M,运行这段代码会发现:程序可以一直运行下去,不会报OutOfMemoryError。如果使用了-verbose:gc -XX:+PrintGCDetails,会发现程序频繁的进行垃圾回收活动。那么 DirectByteBuffer究竟是如何释放堆外内存的? 我们修改下JVM的启动参数

多线程(11) — NIO

拥有回忆 提交于 2019-11-27 01:06:22
  Java NIO是new IO的简称,是一种可以替代Java IO的一套新的IO机制。它提供了一套不同于Java标准IO的操作机制,严格来说,NIO与并发并无直接关系,但是使用NIO技术可以大大提高线程的使用效率。Java NIO设计的基础内容有通道(Channel)、缓冲区(Buffer)、Selector(选择器)。下面说说这几个内容 1)通道(Channel)   Channel:Channel是一对象,可以通过它读取和写入数据。可以把它看做是IO中的流,不同的是: Channel是双向的,既可以读又可以写,而流是单向的 Channel可以进行异步的读写 对Channel的读写必须通过buffer对象   正如上面提到的,所有数据都通过Buffer对象处理,所以不会将字节写入到Channel中,而是将数据写入到Buffer中;不会从Channel中读取字节,而是将数据从Channel读入Buffer,再从Buffer获取这个字节。Channel可以比流更好地反映出底层操作系统的真实情况。特别是在Unix模型中,底层操作系统通常都是双向的。在Java NIO中的Channel主要有如下几种类型: FileChannel:从文件读取数据的 DatagramChannel:读写UDP网络协议数据 SocketChannel:读写TCP网络协议数据

Can multiple threads see writes on a direct mapped ByteBuffer in Java?

醉酒当歌 提交于 2019-11-27 00:55:22
问题 I'm working on something that uses ByteBuffers built from memory-mapped files (via FileChannel.map()) as well as in-memory direct ByteBuffers. I am trying to understand the concurrency and memory model constraints. I have read all of the relevant Javadoc (and source) for things like FileChannel, ByteBuffer, MappedByteBuffer, etc. It seems clear that a particular ByteBuffer (and relevant subclasses) has a bunch of fields and the state is not protected from a memory model point of view. So, you

Java NIO Buffer详解

耗尽温柔 提交于 2019-11-27 00:49:46
一、ByteBuffer类型化的put与get方法 /** * ByteBuffer类型化的put与get方法 */ public class NioTest5 { public static void main(String[] args) { ByteBuffer buffer = ByteBuffer.allocate(64); buffer.putInt(5); buffer.putLong(500000000L); buffer.putDouble(13.456); buffer.putChar('你'); buffer.putShort((short) 3); buffer.flip(); System.out.println(buffer.getInt()); System.out.println(buffer.getLong()); System.out.println(buffer.getDouble()); System.out.println(buffer.getChar()); System.out.println(buffer.getShort()); } }   put和get的类型要一致。如第一个是putInt, 输出的使用第一个要用getInt。 来源: https://www.cnblogs.com/linlf03/p/11337082.html

NIO--ByteBuf

北慕城南 提交于 2019-11-27 00:47:20
Nio 的ByteBuffer 和 Netty 的 ByteBuf 的区别:   1、ByteBuf 将 ByteBuffer的position 分解为:readIndex , writeIndex 因此,使用ByteBuf 就不再需要使用 filp 进行读写状态的切换,随时可以进行读写。   2、jdk byteBuffer 不支持自动扩容,Netty ByteBuf 支持自动扩容。 byteBuf的三种缓冲区类型:     1、heap buffer(array)   2、direct buffer(内核内存的地址)   3、composite buffer(复合缓冲区,内部包含1、2,然后使用同一的方式来处理,聚合起来)       来源: https://www.cnblogs.com/chen--biao/p/11337028.html

NIO

£可爱£侵袭症+ 提交于 2019-11-26 22:44:19
概念(NIO vs BIO) NIO, Non-blocking I/O, 非阻塞式 I/O 模型。也可以解释为 New I/O, 区别于旧的阻塞式 I/O 模型(BIO)。 BIO 与 NIO 的区别如下: 类别 I/O 方式 最低可用的 JDK 版本 BIO 流式 JDK 1.0 NIO 块式 JDK 1.4 所谓流式处理,就是单个字节的数据移动,通过一个称为 Stream 的对象一次移动一个字节;而块式处理,就是单个字节数组的数据移动,通过一个叫 Buffer 的对象一次移动一个字节数组。JDK 中的 NIO 库已经集成了原来的标准 I/O 功能。 缓冲区和通道(Buffer & Channel) NIO 中的缓冲区(Buffer)实质是一个数组,通常为字节数组(ByteBuffer),用作读写缓冲,以及对数据的结构化访问,还可以用来跟踪系统的读写进程。 Buffer 类型: ByteBuffer CharBuffer ShortBuffer IntBuffer LongBuffer FloatBuffer DoubleBuffer NIO 中的通道(Channel)类似 BIO 中的流(Stream),但是是双向的,可以用来读、写或者同时读写。流之所以是单向的,是因为一个 Stream 要么是 InputStream,要么是 OutputStream,不能兼有。