//利用ByteBuffer 的duplicate()方法,可以用一个线程写入数据,另一个线程读出刚写入的数据。做到数据的实时要求,如用在视频的读入采集的同时又写入网路流中传递。
//下面的主程序负责写入主bytebuffer,另一线程时刻监控bytebuffer的复刻bytebuffer1,如有数据写如立刻读出数据
import java.nio.ByteBuffer;
public class Khd2 {
static ByteBuffer byteBuffer=ByteBuffer.allocate(1000);
static ByteBuffer byteBuffer1;
static boolean is;
static int z=0;
public static void main(String[] args) throws InterruptedException {
Rd rd=new Rd();
rd.start();
byteBuffer1=byteBuffer.duplicate();
for(int t=0;t<127;t++){
byteBuffer.put((byte)t);
z++;
is=true;
Thread.sleep(1000);
}
}
}
class Rd extends Thread{
public synchronized void run(){
int k=1;
while (Khd2.is){
if(k<=Khd2.z) {
int t = Khd2.byteBuffer1.get();
System.out.println("d=:" + t);
k++;
}
try {
Thread.sleep(1); //这个延时必须加,不知道为啥,如不加,程序不运行
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
准备用这个程序改写前面的利用公网ip写的点对点数据传输程序,再加入摄像头采集程序,做一个内网视频监控