首页 技术 正文
技术 2022年11月15日
0 收藏 348 点赞 3,748 浏览 1691 个字

目录

Java申请DirectBuffer

/*-------JAVA直接操作内存-------------   * 申请100m的直接内存,不会申请在java堆上面   * 打开资源管理器可以看到内存占用会增加100m,但是java堆上的内存却没有增加!   */ByteBuffer buffer = ByteBuffer.allocateDirect(100 * 1024 * 1024);System.out.println("------------------start----------------------");Thread.sleep(10 * 1000);// 清理刚刚申请的内存
((DirectBuffer)(buffer)).cleaner().clean(); System.out.println("------------------end----------------------");

ByteBuffer的position,limit,capacity,flip操作之间的关系

以下面的代码为例

/*** 描述:测试ByteBuffer的相关操作* position:写模式下当前写的位置,读模式下当前读的位置* limit:写模式下能写多少数据,读模式下能读多少数据* capacity:即byteBuffer的总大小,不区分读写* flip:将limit置为position,position置为0,一般用于写完之后开始进行读取*/   privatestaticvoid testByteBufferOperation() throws InterruptedException   {
ByteBuffer buffer = ByteBuffer.allocate(20); buffer.putInt(123); //java 1个int 4个字节 //默认limit和capacity的值是相同的,第一次put之后,position移动到当前写的位置
System.out.println("*************initial status***************");
System.out.println("position:" + buffer.position() + "\nlimit:" + buffer.limit() + "\n"
+ "capacity:" + buffer.capacity()); //flip之后,limit变为position,position置为0
buffer.flip();
System.out.println("*************after flip***************");
System.out.println("position:" + buffer.position() + "\nlimit:" + buffer.limit() + "\n"
+ "capacity:" + buffer.capacity()); //get之后,position移动1位,其他不变
buffer.get();
System.out.println("*************after first get***************");
System.out.println("position:" + buffer.position() + "\nlimit:" + buffer.limit() + "\n"
+ "capacity:" + buffer.capacity());
}

枚举实现单例模式

/* * 枚举实现单例,一方面保证了线程安全,另一方面会“免费”得到了序列化的功能
*/publicenum ChildEnum {
//枚举里面默认的实例
INSTANCE;
private Child childInstance; private ChildEnum()
{
childInstance = new Child();
} public Child getInstance()
{
returnchildInstance;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298