首页 文章 精选 留言 我的

精选列表

搜索[基础软件],共10017篇文章
优秀的个人博客,低调大师

JavaNIO基础02-缓存区基础

1.1、什么是缓存区? 缓冲区(Buffer)就是在内存中预留指定大小的存储空间用来对输入/输出(I/O)的数据作临时存储,这部分预留的内存空间就叫做缓冲区。 Buffer在IO中很重要。在java.io包中的BufferedInputStream、BufferedOutputStream、BufferedReader和BufferedWriter在其实现中都运用了缓冲区。java.nio包公开了Buffer API,使得Java程序可以直接控制和运用缓冲区。 在Java NIO中,缓冲区主要是跟通道(Channel)打交道,数据总是从缓冲区写入到通道中,或者从通道读取数据到缓冲区。 1.2、缓存区的优点? 1、减少实际的物理读写次数 2、缓冲区在创建时就被分配内存,这块内存区域一直被重用,可以减少动态分配和回收内存的次数 1.3、缓存区的存储位置 1、堆内内存 堆内内存是由JVM所管控的Java进程内存,我们平时在Java中创建的对象都处于堆内内存中,并且它们遵循JVM的内存管理机制,JVM会采用垃圾回收机制统一管理它们的内存。 2、堆外内存 堆外内存就是存在于JVM管控之外的一块内存区域,因此它是堆外内存不受JVM的管控。 1.4 缓存区常用类 缓存区类说明 类名称 说明 ByteBuffer 字节缓存区 HeapByteBuffer 堆内字节缓存区 HeapByteBufferR 堆内字节只读缓存区,以R结尾的类表示只读 MappedByteBuffer 文件映射到虚拟内存,读写性极能 DirectByteBuffer 堆外字节缓存区 - - ByteBufferAsCharBufferB 字节缓存区转字符缓存区,大端序列 ByteBufferAsCharBufferRB 字节缓存区转字符缓存区,只读、大端序列 ByteBufferAsCharBufferL 字节缓存区转字符缓存区,小端序列 ByteBufferAsCharBufferLB 字节缓存区转字符缓存区,只读、小端序列 DirectCharBufferS 堆外字符缓冲期,字节序反转 DirectCharBufferRS 堆外字符缓冲期,只读、字节序反转 DirectCharBufferU 堆外字符缓冲期,字节序非反转 2.1 缓存区4个属性 (1)capacity capacity指的是缓冲区能够容纳元素的最大数量,这个值在缓冲区创建时被设定,而且不能够改变。(2)limit limit指的是缓冲区中第一个不能读写的元素的数组下标索引,也可以认为是缓冲区中实际元素的数量。(3)position position指的是下一个要被读写的元素的数组下标索引,该值会随get()和put()的调用自动更新。(4)mark 一个备忘位置,调用mark()方法的话,mark值将存储当前position的值,等下次调用reset()方法时,会设定position的值为之前的标记值。 2.2 缓存区不变等式 0<=标记<=位置<=限制<=容量 2.3 缓存区操作示例 (1)创建缓存区 ByteBuffer buffer = ByteBuffer.allocate(10); //mark = -1; //position= 0; //limit=10; //capacity=10; 初始化状态 (2)添加数据 buffer.put((byte)1); buffer.put((byte)2); buffer.put((byte)3); buffer.put((byte)4); buffer.put((byte)5); //mark = -1; //position= 5; //limit=10; //capacity=10; 添加数据后 (3) 读取数据 读取缓冲区前,一般调用flip(反转) 方法。 buffer.flip(); //当前状态 //mark = -1; //position= 0; //limit=5; //capacity=10; 读取前翻转 buffer.get(); buffer.get(); //当前状态 //mark = -1; //position= 2; //limit=5; //capacity=10; 执行两次get后 (4)标记缓存区 buffer.mark(); //mark = 2; //position= 2; //limit=5; //capacity=10; 标记后 (5)重置缓存区 buffer.get(); buffer.get(); //mark = 2; //position= 4; //limit=5; //capacity=10; buffer.reset(); //mark = 2; //position= 2; //limit=5; //capacity=10; 重置缓存区 3 缓存常用方法 3.1 反转 flip 使缓冲区为一系列新的通道写入或相对获取 操作做好准备:它将限制设置为当前位置,然后将位置设置为 //flip 方法内部实现 limit = position; position = 0; mark = -1; 3.2 清理 clear 使缓冲区为一系列新的通道读取或相对放置 操作做好准备:它将限制设置为容量大小,将位置设置为 0 //clear内部实现 position = 0; limit = capacity; mark = -1; 3.3 重绕rewind 使缓冲区为重新读取已包含的数据做好准备:它使限制保持不变,将位置设置为 0 position = 0; mark = -1; 3.4 remaining 与 hasRemaining 常用用于判断缓存区可读内容的长度。 return limit - position; 3.5 标记mark()与重置reset()方法 mark 把 mark的值设置成position。 reset 把position设置成mark的值,相当于之前做过一个标记,现在要退回到之前标记的地方 3.6 压缩 compact 把从position到limit中的内容移到0到limit-position的区域内,position和limit的取值也分别变成limit-position、capacity。如果先将positon设置到limit,再compact,那么相当于clear()

优秀的个人博客,低调大师

Docker基础之六: Docker基础命令

容器相关的命令 (对Docker感兴趣的朋友可以加我的微信ghostcloud2016,然后我把你加到我们的一个Docker爱好者群组里面。) 在上例中,我们用3种方式运行了容器,在上节中我们学习了: docker ps : 查看容器列表 docker logs:查看容器的日志 docker stop:停止容器 在本节中,我们将通过事例来熟悉一些其他的docker命令,并学习docker client。 1 Docker Client Docker Client和Docker Daemon实际使用的是同一个二进制程序,通过docker --help命令可以直接查看docker的所有命令及选项: 查看docker可执行程序的路径: root@shev:~# which docker /usr/bin/docker docker命令的帮助信息: root@shev:~# docker --help Usage: docker [OPTIONS] COMMAND [arg...] docker daemon [ --help | ... ] docker [ --help | -v | --version ] A self-sufficient runtime for containers. Options: --config=~/.docker Location of client config files -D, --debug=false Enable debug mode --disable-legacy-registry=false Do not contact legacy registries -H, --host=[] Daemon socket(s) to connect to -h, --help=false Print usage -l, --log-level=info Set the logging level --tls=false Use TLS; implied by --tlsverify --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA --tlscert=~/.docker/cert.pem Path to TLS certificate file --tlskey=~/.docker/key.pem Path to TLS key file --tlsverify=false Use TLS and verify the remote -v, --version=false Print version information and quit Commands: attach Attach to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on a container or image kill Kill a running container load Load an image from a tar archive or STDIN login Register or log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container network Manage Docker networks pause Pause all processes within a container port List port mappings or a specific mapping for the CONTAINER ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart a container rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save an image(s) to a tar archive search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop a running container tag Tag an image into a repository top Display the running processes of a container unpause Unpause all processes within a container version Show the Docker version information volume Manage Docker volumes wait Block until a container stops, then print its exit code Run 'docker COMMAND --help' for more information on a command. 具体的含义,我们在日后再一一讲解。我们先关注头几行: Usage: docker [OPTIONS] COMMAND [arg...] docker daemon [ --help | ... ] docker [ --help | -v | --version ] 剖析: 第一行实际是docker client的语法格式,它接收OPTIONS参数,并跟一个COMMAND,之后紧跟该命令对应的参数 第二行实际是docker daemon的语法格式,有兴趣的朋友可以通过docker daemon –help进行查看 第三行就是docker client的帮助,有需要都可以执行并查看。 一个我们经常用的查看docker版本信息的命令: root@shev:~# docker version Client: Version: 1.9.0 API version: 1.21 Go version: go1.4.2 Git commit: 76d6bc9 Built: Tue Nov 3 17:43:42 UTC 2015 OS/Arch: linux/amd64 Server: Version: 1.9.0 API version: 1.21 Go version: go1.4.2 Git commit: 76d6bc9 Built: Tue Nov 3 17:43:42 UTC 2015 OS/Arch: linux/amd64 这个命令会列出当前docker的client和server的基本信息. 2 通过容器来运行web应用 容器可以运行任何程序,那么一个主要的用途就是运行web应用,并对外提供服务,下面我们就用官方的一个例子来进行演示: 2.1 启动容器 root@shev:~# docker run -d -P training/webapp python app.py Unable to find image 'training/webapp:latest' locally latest: Pulling from training/webapp 2880a3395ede: Pull complete 515565c29c94: Pulling fs layer 98b15185dba7: Pulling fs layer 2ce633e3e9c9: Verifying Checksum 2ee0b8f351f7: Download complete 2505b734adda: Downloading [=====================> ] 8.732 MB/20.71 MB 20dd0c759013: Downloading [> ] 515.2 kB/50.25 MB f95ebd363bf2: Verifying Checksum 1952e3bf3d7e: Download complete abb991a4ed5e: Download complete 7cbae6914197: Pulling fs layer f74dd040041e: Pulling fs layer 54bb4e8718e8: Download complete Pulling repository docker.io/training/webapp 02a8815912ca: Download complete e9e06b06e14c: Download complete a82efea989f9: Download complete 37bea4ee0c81: Download complete 07f8e8c5e660: Download complete 23f0158a1fbe: Download complete 0a4852b23749: Download complete 7d0ff9745632: Download complete 99b0d955e85d: Download complete 33e109f2ff13: Download complete cc06fd877d54: Download complete b1ae241d644a: Download complete b37deb56df95: Download complete Status: Downloaded newer image for training/webapp:latest docker.io/training/webapp: this image was pulled from a legacy registry. Important: This registry version will not be supported in future versions of docker. 7736775bd89f6d0373b7a6c39a91126d156d9b0ce5467c30f0275506e012be4e 我们查看一下程序是否启动: root@shev:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7736775bd89f training/webapp "python app.py" 9 minutes ago Up 9 minutes 0.0.0.0:32768->5000/tcp agitated_poitras e221abf45a0b ghostcloud.cn:5000/gcali:1.2 "/usr/local/ghostclou" About an hour ago Up About an hour gcsagent 剖析: 我们从Docker Hub拉取了training用户下的webapp的最新版本,然后用python执行app.py 我们的启动参数包括:-d 在后台运行,-P导出容器需要导出的端口,由于Docker Hub在国外,因此拉取的时间可能会有点长,但是一旦本地有了镜像,之后就会很快了。 通过ps 命令,我们可以看到主机的32768端口映射到了容器的5000端口 2.2 访问网页 我们通过主机的32768端口进行访问: 2.3 查看容器的端口导出情况 root@shev:~# docker port 7736 5000/tcp -> 0.0.0.0:32768 2.4 查看容器的运行日志 root@shev:~# docker logs -f 7736 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 192.168.0.103 - - [02/Dec/2015 10:03:07] "GET / HTTP/1.1" 200 - 192.168.0.103 - - [02/Dec/2015 10:03:07] "GET /favicon.ico HTTP/1.1" 404 - 192.168.0.103 - - [02/Dec/2015 10:08:11] "GET / HTTP/1.1" 200 - 剖析: -f会一直绑定到容器的标准输出上,不带-f时,运行后命令立刻就会退出 运行完logs –f后,我们访问网页时,就会在命令行里面显示日志 2.5 查看容器中的进程信息 root@shev:~# docker top 7736 UID PID PPID C STIME TTY TIME CMD root 5115 4051 0 17:53 ? 00:00:00 python app.py 2.6 查看容器的详细信息 root@shev:~# docker inspect 7736 [ { "Id": "7736775bd89f6d0373b7a6c39a91126d156d9b0ce5467c30f0275506e012be4e", "Created": "2015-12-02T09:53:41.35592583Z", "Path": "python", "Args": [ "app.py" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 5115, "ExitCode": 0, "Error": "", "StartedAt": "2015-12-02T09:53:41.62798274Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "02a8815912ca800f99b7d912485e8c618260e27c6de8d7a161b356b322d5c121", "ResolvConfPath": "/var/lib/docker/containers/7736775bd89f6d0373b7a6c39a91126d156d9b0ce5467c30f0275506e012be4e/resolv.conf", "HostnamePath": "/var/lib/docker/containers/7736775bd89f6d0373b7a6c39a91126d156d9b0ce5467c30f0275506e012be4e/hostname", "HostsPath": "/var/lib/docker/containers/7736775bd89f6d0373b7a6c39a91126d156d9b0ce5467c30f0275506e012be4e/hosts", "LogPath": "/var/lib/docker/containers/7736775bd89f6d0373b7a6c39a91126d156d9b0ce5467c30f0275506e012be4e/7736775bd89f6d0373b7a6c39a91126d156d9b0ce5467c30f0275506e012be4e-json.log", "Name": "/agitated_poitras", "RestartCount": 0, "Driver": "aufs", "ExecDriver": "native-0.2", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LxcConf": [], "Memory": 0, "MemoryReservation": 0, "MemorySwap": 0, "KernelMemory": 0, "CpuShares": 0, "CpuPeriod": 0, "CpusetCpus": "", "CpusetMems": "", "CpuQuota": 0, "BlkioWeight": 0, "OomKillDisable": false, "MemorySwappiness": -1, "Privileged": false, "PortBindings": {}, "Links": null, "PublishAllPorts": true, "Dns": null, "DnsOptions": null, "DnsSearch": null, "ExtraHosts": null, "VolumesFrom": null, "Devices": [], "NetworkMode": "default", "IpcMode": "", "PidMode": "", "UTSMode": "", "CapAdd": null, "CapDrop": null, "GroupAdd": null, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "SecurityOpt": null, "ReadonlyRootfs": false, "Ulimits": null, "LogConfig": { "Type": "json-file", "Config": {} }, "CgroupParent": "", "ConsoleSize": [ 0, 0 ], "VolumeDriver": "" }, "GraphDriver": { "Name": "aufs", "Data": null }, "Mounts": [], "Config": { "Hostname": "7736775bd89f", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "5000/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "python", "app.py" ], "Image": "training/webapp", "Volumes": null, "WorkingDir": "/opt/webapp", "Entrypoint": null, "OnBuild": null, "Labels": {}, "StopSignal": "SIGTERM" }, "NetworkSettings": { "Bridge": "", "SandboxID": "02c2de9e7171ed418869e53297b0b1956052be67d2c9c4c75f99829fff646fdd", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "5000/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "32768" } ] }, "SandboxKey": "/var/run/docker/netns/02c2de9e7171", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "f623f99d87ec63cc3bfdc024886d24d40bd1cf1771e249951133a7c66424d6e9", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "EndpointID": "f623f99d87ec63cc3bfdc024886d24d40bd1cf1771e249951133a7c66424d6e9", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02" } } } } ] 2.7 停止并重启容器 root@shev:~# docker stop 7736 7736 root@shev:~# root@shev:~# docker start 7736 7736 root@shev:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7736775bd89f training/webapp "python app.py" 25 minutes ago Up 2 seconds 0.0.0.0:32769->5000/tcp agitated_poitras 注意:容器的在主机上的映射端口变成了32769,原因是我们没有指定主机的端口。 2.8 指定主机和容器的端口映射 root@shev:~# docker run -d -p 80:5000 training/webapp python app.py 54132a474d29d44a7fc6431184ae1c1c77b4c8fb31975d477aab73647a008837 root@shev:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 54132a474d29 training/webapp "python app.py" 3 seconds ago Up 2 seconds 0.0.0.0:80->5000/tcp grave_williams 剖析: 通过-p 参数可以指定端口映射,通过-P则是随机分配 由于我们导出主机的80端口,因此我们再次访问网页的时候,可以不需要指定端口: 是不是眼前一亮,是的,我们可以将我们的网页直接运行在容器里面! 想象一下,你拥有一台云主机……

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册