Buffer缓冲区

  1. Buffer用起来与Array数组类似
  2. Buffer主要用于存储二进制数据,是以十六进制显示的
  3. Buffer.length:占用内存的大小(单位:byte)
  4. 一个字母占用1byte,一个汉字占用3byte
  5. 将字符串转为Buffer:Buffer.from('hello world');
  6. 创建指定大小Buffer:let buffer = Buffer.alloc(1024);这是一个1024byte(1kb)大小的Buffer
  7. Buffer大小一旦确定不可修改,它是对底层的直接操作
  8. 控制台输出buffer中的内容,是以十进制输出的
  9. Buffer.toString():将Buffer中的数据转换为字符串
请帮我为以下代码添加注释,并排版package T14; //Buffer.java public class Buffer { private int buffer = -1; // buffer缓冲区被producer 和 consumer 线程共享 private int occupiedBufferCount = 0; // 控制缓冲区buffers读写的条件边量 public synchronized void set( int value ) //place value into buffer { String name = Thread.currentThread().getName();//get name of thread that called this method // while there are no empty locations, place thread in waiting state while ( occupiedBufferCount == 1 ) { // output thread information and buffer information, then wait try { //System.err.println("Buffer full. "+ name + " waits." ); wait(); } // if waiting thread interrupted, print stack trace catch ( InterruptedException exception ) { exception.printStackTrace(); } } // end while buffer = value; // set new buffer value ++occupiedBufferCount; System.err.println(name + " writes " + buffer); notify(); // tell waiting thread to enter ready state } // end method set; releases lock on SynchronizedBuffer public synchronized int get() // return value from buffer { // for output purposes, get name of thread that called this method String name = Thread.currentThread().getName(); // while no data to read, place thread in waiting state while ( occupiedBufferCount == 0 ) { // output thread information and buffer information, then wait try { //System.err.println("Buffer empty. "+name + " waits." ); wait(); } catch ( InterruptedException exception ) { exception.printStackTrace(); } } // end while // indicate that producer can store another value , because consumer just retrieved buffer value --occupiedBufferCount; System.err.println( name + " reads " + buffer ); notify(); // tell waiting thread to become ready to execute return buffer; } // end method get; releases lock on SynchronizedBuffer }
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值