原来没开线程专心读取输入数据,导致reboot时,数据还没读,连接就断开了。
所以,想专门写个线程,专心读取数据。
用ByteBuffer做数据缓存~~
测试程序如下:
import java.nio.ByteBuffer; import java.util.Random;
public class test {
ByteBuffer buffer = ByteBuffer.allocate(1024);
Object lock = new Object();
/** * @param args */ public static void main(String[] args) {
test x = new test(); x.go();
}
private void go() { new PutDataThread().start(); new GetDataThread().start();
}
//放数据的线程 class PutDataThread extends Thread { public void run() { buffer.rewind(); //指针挪到0 int i = 0; while (i < 255) { synchronized (lock) { System.out.println("put " + i); buffer.put((byte) i); }
//sleep一会 try { Thread.sleep(new Random().nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); }
i++; if (i == 254) { i = 0; } } } }
//取数据线程 class GetDataThread extends Thread { public void run() { while (true) { //读数据要同步 synchronized (lock) { byte[] aa = new byte[buffer.position()]; buffer.position(0); //指针挪到0 buffer.get(aa); //数据读入数组 buffer.position(0); //指针挪回0 //打印(实际应把其他工作挪到同步外面进行,这里为了打印顺序不乱,放在了里面) System.out.print("-----get--"); for (int i = 0; i < aa.length; i++) { System.out.print(aa[i] + " "); } System.out.println("-------"); } //sleep一会 try { Thread.sleep(new Random().nextInt(2000)); } catch (InterruptedException e) { e.printStackTrace(); } } } }
}
运行结果如下:
-----get--------- put 0 put 1 put 2 -----get--0 1 2 ------- put 3 put 4 put 5 put 6 -----get--3 4 5 6 ------- put 7 put 8 put 9 put 10 -----get--7 8 9 10 ------- put 11 put 12 put 13 -----get--11 12 13 ------- -----get--------- put 14 -----get--14 ------- put 15 put 16 put 17 -----get--15 16 17 ------- -----get--------- put 18 put 19 put 20 -----get--18 19 20 ------- put 21 -----get--21 ------- put 22 put 23 put 24 -----get--22 23 24 ------- put 25 put 26 put 27 -----get--25 26 27 ------- put 28 put 29 put 30 -----get--28 29 30 ------- put 31 put 32 put 33 -----get--31 32 33 ------- put 34 put 35 -----get--34 35 ------- put 36 put 37 put 38 -----get--36 37 38 ------- put 39 put 40 -----get--39 40 ------- put 41 put 42 put 43 put 44 -----get--41 42 43 44 ------- put 45 -----get--45 ------- -----get--------- put 46 -----get--46 ------- put 47 put 48 -----get--47 48 ------- put 49 put 50 -----get--49 50 ------- put 51 put 52 -----get--51 52 ------- put 53 put 54 put 55 -----get--53 54 55 ------- put 56 put 57 put 58 -----get--56 57 58 ------- -----get--------- put 59 put 60 put 61 -----get--59 60 61 ------- put 62 -----get--62 ------- -----get--------- put 63 put 64 put 65 -----get--63 64 65 ------- put 66 put 67 put 68 -----get--66 67 68 ------- -----get--------- put 69 -----get--69 ------- put 70 put 71 put 72 put 73 -----get--70 71 72 73 ------- -----get--------- put 74 put 75 put 76 put 77 -----get--74 75 76 77 ------- put 78 put 79 -----get--78 79 ------- put 80 -----get--80 ------- put 81 -----get--81 ------- put 82 put 83 put 84 -----get--82 83 84 ------- put 85 put 86 -----get--85 86 ------- -----get--------- put 87 put 88 put 89 put 90 put 91 put 92 put 93 -----get--87 88 89 90 91 92 93 ------- put 94 -----get--94 ------- put 95 -----get--95 ------- put 96 put 97 put 98 -----get--96 97 98 ------- put 99 put 100 put 101 -----get--99 100 101 ------- -----get--------- put 102 put 103 put 104 -----get--102 103 104 ------- put 105 put 106 -----get--105 106 ------- put 107 put 108 put 109 put 110 -----get--107 108 109 110 ------- put 111 put 112 put 113 put 114 put 115 put 116 -----get--111 112 113 114 115 116 ------- put 117 put 118 put 119 put 120 -----get--117 118 119 120 ------- put 121 put 122 put 123 put 124 put 125 put 126 -----get--121 122 123 124 125 126 ------- put 127 put 128 -----get--127 -128 ------- -----get--------- -----get--------- -----get--------- put 129 -----get---127 ------- put 130 put 131 -----get---126 -125 ------- put 132 -----get---124 ------- put 133 put 134 -----get---123 -122 ------- -----get--------- put 135 -----get---121 ------- put 136 put 137 -----get---120 -119 ------- put 138 ....
|