bytebuffer

从实践角度重新理解BIO和NIO

扶醉桌前 提交于 2019-12-01 22:28:14
前言 这段时间自己在看一些Java中BIO和NIO之类的东西,看了很多博客,发现各种关于NIO的概念说的天花乱坠头头是道,可以说是非常的完整,但是整个看下来之后,自己对NIO还是一知半解的状态,所以这篇文章不会提到很多的概念,而是站在一个实践的角度,写一些我自己关于NIO的见解,站在实践过后的高度下再回去看概念,应该对概念会有一个更好的理解。 实现一个简易单线程服务器 要讲明白BIO和NIO,首先我们应该自己实现一个简易的服务器,不用太复杂,单线程即可。 为什么使用单线程作为演示 因为在单线程环境下可以很好地对比出BIO和NIO的一个区别,当然我也会演示在实际环境中BIO的所谓一个请求对应一个线程的状况。 服务端 public class Server { public static void main(String[] args) { byte[] buffer = new byte[1024]; try { ServerSocket serverSocket = new ServerSocket(8080); System.out.println("服务器已启动并监听8080端口"); while (true) { System.out.println(); System.out.println("服务器正在等待连接..."); Socket socket =

堆内内存与堆外内存

纵然是瞬间 提交于 2019-12-01 22:09:38
最近看Spark的 StorageLevel(存储级别) 源码的时候 看到有 useOffHeap 这个标签, 觉得有必要挖掘一下 堆内内存(on-heap memory) 堆内内存是java程序员在日常工作中解除比较多的, 可以在jvm参数中使用-Xms, -Xmx 等参数来设置堆的大小和最大值 堆内内存 = 年轻代 + 老年代 + 持久代 年轻代 (Young Generation) 存放的是新生成的对象 年轻代的目标是尽可能快速的收集掉那些生命周期短的对象 Eden 大部分对象在Eden区中生成 当Eden区满时, 依然存活的对象将被复制到Survivor区, 当一个Survivor 区满时, 此区的存活对象将被复制到另外一个Survivor区 Survivor(通常2个) 当两个 Survivor 区 都满时, 从第一个Survivor 区 被复制过来 且 依旧存活的 对象会被复制到 老年区(Tenured) Survivor 的两个区是对称的, 没有先后关系, 所有同一个区中可能同时存在从Eden复制过来的对象 和 从前一个 Survivor 复制过来的对象。 Survivor 区可以根据需要配置多个, 从而增加对象在年轻代的存在时间, 减少被放到老年代的可能。 老年代 (Old Generation) 存放了在年轻代中经历了N次垃圾回收后仍存活的对象,

How to serialize ByteBuffer

时光毁灭记忆、已成空白 提交于 2019-12-01 17:16:52
问题 I wish to send a java.nio.ByteBuffer accross a network using RMI, however ByteBuffer isn't serializable. I've tried the following custom class to no avail: public class NetByteBuffer implements java.io.Serializable { ByteBuffer buffer; public NetByteBuffer(ByteBuffer buffer) { this.buffer = buffer; } public ByteBuffer getByteBuffer() { return this.buffer; } } The client still gets a non-serialzable exception. Any ideas? Thanks 回答1: You can't. You'd better obtain the byte[] and send it instead

How to serialize ByteBuffer

回眸只為那壹抹淺笑 提交于 2019-12-01 17:04:02
I wish to send a java.nio.ByteBuffer accross a network using RMI, however ByteBuffer isn't serializable. I've tried the following custom class to no avail: public class NetByteBuffer implements java.io.Serializable { ByteBuffer buffer; public NetByteBuffer(ByteBuffer buffer) { this.buffer = buffer; } public ByteBuffer getByteBuffer() { return this.buffer; } } The client still gets a non-serialzable exception. Any ideas? Thanks You can't. You'd better obtain the byte[] and send it instead and reconstruct the ByteBuffer on the other side. You are of course losing the advantages of it being a

Java NIO:Buffer、Channel 和 Selector

烂漫一生 提交于 2019-12-01 17:03:20
本文将介绍 Java NIO 中三大组件 Buffer、Channel、Selector 的使用。 本来要一起介绍非阻塞 IO 和 JDK7 的异步 IO 的,不过因为之前的文章真的太长了,有点影响读者阅读,所以这里将它们放到另一篇文章中进行介绍。 Buffer 一个 Buffer 本质上是内存中的一块,我们可以将数据写入这块内存,之后从这块内存获取数据。 java.nio 定义了以下几个 Buffer 的实现,这个图读者应该也在不少地方见过了吧。 其实核心是最后的 ByteBuffer,前面的一大串类只是包装了一下它而已,我们使用最多的通常也是 ByteBuffer。 我们应该将 Buffer 理解为一个数组,IntBuffer、CharBuffer、DoubleBuffer 等分别对应 int[]、char[]、double[] 等。 MappedByteBuffer 用于实现内存映射文件,也不是本文关注的重点。 我觉得操作 Buffer 和操作数组、类集差不多,只不过大部分时候我们都把它放到了 NIO 的场景里面来使用而已。下面介绍 Buffer 中的几个重要属性和几个重要方法。 position、limit、capacity 就像数组有数组容量,每次访问元素要指定下标,Buffer 中也有几个重要属性:position、limit、capacity。 最好理解的当然是

Mediacodec, decode byte packet from server and render it on surface

冷暖自知 提交于 2019-12-01 14:37:11
I have some issues with MediaCode. I have 3 components; Decoder, Downloader and Render. And Simple FragmentStreamVideo that initialize the 'SurfaceView' and the 'Downloader'. The other components like the Render and Decoder are initialized in the SurfaceView. Then, a syncronize is done between the Decoder and the Dowloader, implemented by BlockingQueue<String> queue where String = Filename ( Each frame has its file ). Another syncronize between Decode and Render is done by the standard ByteBuffer as stated in documentation. Find below my piece of code. Would be very grateful if you can help.

Java中NIO及基础实现

廉价感情. 提交于 2019-12-01 13:54:29
NIO:同步非阻塞IO 来源:BIO是同步阻塞IO操作,当线程在处理任务时,另一方会阻塞着等待该线程的执行完毕,为了提高效率,,JDK1.4后,引入NIO来提升数据的通讯性能 NIO中采用Reactor设计模式,注册的汇集点为Selector,NIO有三个主要组成部分:Channel(通道)、Buffer(缓冲区)、Selector(选择器) Reactor设计模式:Reactor模式是一种被动事件处理模式,即当某个特定事件发生时触发事件,可参考, https://blog.csdn.net/feimataxue/article/details/7642638 , https://www.cnblogs.com/bitkevin/p/5724410.html NIO采用了轮询的方式来观察事件是否执行完毕,如:A让B打印某个文件,BIO会一直等待着B返回,期间自己不做其他事情,而NIO则会不断的询问B是否完成,未完成则处理自己的时,直至B完成 Channel(通道):Channel是一个对象,可以通过它读取和写入数据 Selector(对象选择器): Selector是一个对象,它可以注册到很多个Channel上,监听各个Channel上发生的事件,并且能够根据事件情况决定Channel读写 代码实现:(此实现参考网络上可用的例子) NIO客户端实现: package com

record voice in a Queue<byte[]> and send it to the server

心已入冬 提交于 2019-12-01 08:29:42
I am developing voice application. I need a buffer queue of some sort so that i record continuosly in a thread , place the buffers full of bytes into the queue and to transmit to the server, and i take the next buffer from the queue. Here is the recording code: Queue<byte[]> qArray = new LinkedList<byte[]>(); recordingThread = new Thread(new Runnable() { @Override public void run() { bData = new byte[BufferElements]; while (isRecording) { recorder.read(bData, 0, BufferElements); qArray.add(bData); if (AudioRecord.ERROR_INVALID_OPERATION != 0) { SendAudio(); } } } }, "AudioRecorder Thread");

OOM常见几种类型

ε祈祈猫儿з 提交于 2019-12-01 07:01:36
Java中的OOM java.lang.StackOverflowError java.lang.OutMemoryError:Java heap space java.lang.OutMemoryError:GC overhead limit exceeded:Gc回收时间过长会发生outofmemoryerror,过长的定义是,如果超过98%的时间来做GC,并且回收了不到2%的堆内存,连续多次回收不到2%的情况会跑出,负责形成恶性循环,gc清理的内存再次被填满,迫使gc再次执行。 /* -Xmx10m -Xms10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m */ public static void main(String[] args){ int i = 0; List<String> list = new ArrayList<>(); try{ while (true){ list.add(String.valueOf(new Random().nextInt(1111111)).intern()); } }catch (Throwable e){ e.printStackTrace(); } } java.lang.OutMemoryError:Direct buffer memory

NIO流的学习

别来无恙 提交于 2019-12-01 07:01:30
NIO的使用 一)、什么叫NIO? 定义:是一套新的Java I/O标准, 在java1.4中被纳入JDK中。 二)、NIO的实现方法 NIO是基于块的, 以块为基本单位处理数据。 标准的I/O是基于流实现的,以字节为单位处理数据。 三)、NIO的特性 1).为所有的原始类型特供Buffer支持 ByteBuffer CharBuffer DoubleBuffer FloatBuffer IntBuffer LongBuffer ShortBuffer 2).字符集编码解码解决方案,使用java.nio.Charset 3) .增加通道(Channel)对象,做为新的原始的I/O抽象 4).支持锁和内存映射文件的文件访问接口 5).提供了基于Selector的异步网络I/O 四)、NIO的两个重要组件 Buffer: 缓冲, 是一块连续的内存块,是NIO中读写数据的中转地。 Channel: 通道, 表示缓冲数据的源头或目的地。 Buffer和Channel的关系: Channel作为数据的源头:从Channel中写数据到Buffer Channel ---------> Buffer Channel作为数据的目的地:从Buffer中写出数据到Channel Channel <--------- Buffer 五)、NIO的Buffer类族和Channel Buffer: