Skip to content

Commit 559faa3

Browse files
author
yongjie.zhang@ucarinc.com
committed
nio
1 parent fb2c29a commit 559faa3

File tree

12 files changed

+430
-4
lines changed

12 files changed

+430
-4
lines changed

java-nio/4.jpg

14.7 KB
Loading

java-nio/java-nio.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

java-nio/src/1.jpg

14.7 KB
Loading
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.asher.nio;
2+
3+
4+
import java.nio.ByteBuffer;
5+
import java.util.HashMap;
6+
import java.util.Set;
7+
8+
/**
9+
* @author : 张勇杰
10+
* @date : 2019/8/26 13:45
11+
* @Version : v1.0
12+
* @description
13+
**/
14+
public class ByteBufferTest {
15+
public static void main(String[] args) {
16+
String a = "abcde";
17+
ByteBuffer buff = ByteBuffer.allocate(1024);
18+
System.out.println("------------beforePut-----------");
19+
System.out.println(buff.position());
20+
System.out.println(buff.limit());
21+
System.out.println(buff.capacity());
22+
23+
buff.put(a.getBytes());
24+
System.out.println("------------afterPut-----------");
25+
System.out.println(buff.position());
26+
System.out.println(buff.limit());
27+
System.out.println(buff.capacity());
28+
29+
//切换到读模式
30+
buff.flip();
31+
System.out.println("------------beforeGet-----------");
32+
System.out.println(buff.position());
33+
System.out.println(buff.limit());
34+
System.out.println(buff.capacity());
35+
byte[] dist = new byte[buff.limit()];
36+
buff.get(dist);
37+
//读取之后
38+
System.out.println("------------afterGet-----------");
39+
System.out.println(buff.position());
40+
System.out.println(buff.limit());
41+
System.out.println(buff.capacity());
42+
System.out.println(new String(dist));
43+
44+
//可重复读 把position重置
45+
buff.rewind();
46+
System.out.println("------------afterRewind-----------");
47+
System.out.println(buff.position());
48+
System.out.println(buff.limit());
49+
System.out.println(buff.capacity());
50+
// System.out.println(new String(dist));
51+
52+
byte[] dit = new byte[buff.limit()];
53+
buff.get(dit,0,2);
54+
System.out.println(buff.position());
55+
System.out.println(new String(dit,0,2));
56+
buff.get(dit,2,2);
57+
System.out.println(buff.position());
58+
System.out.println(new String(dit,2,2));
59+
System.out.println(new String(dit));
60+
//缓冲区状态重置 数据不清空
61+
buff.clear();
62+
System.out.println("------------afterClear-----------");
63+
System.out.println(buff.position());
64+
System.out.println(buff.limit());
65+
System.out.println(buff.capacity());
66+
67+
char c = (char) buff.get();
68+
System.out.println(c);
69+
70+
buff.flip();
71+
72+
System.out.println(buff.position());
73+
System.out.println(buff.limit());
74+
System.out.println(buff.capacity());
75+
System.out.println(buff.remaining());
76+
//缓冲区位置标记 mark 与 reset
77+
}
78+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.asher.nio;
2+
3+
import sun.misc.URLClassPath;
4+
5+
import java.io.*;
6+
import java.nio.ByteBuffer;
7+
import java.nio.MappedByteBuffer;
8+
import java.nio.channels.FileChannel;
9+
import java.nio.file.Paths;
10+
import java.nio.file.StandardOpenOption;
11+
12+
/**
13+
* @author : 张勇杰
14+
* @date : 2019/8/27 15:10
15+
* @Version : v1.0
16+
* @description
17+
*
18+
* 一、io过程
19+
* 1.应用程序发起读写请求时,要通过用户地址空间->内核地址空间,然后才能操作CPU的IO接口
20+
* 2.早期的IO是由CPU负责,发生IO时,CPU被占用,无法进行其他操作
21+
* 3.后来出现DMA(直接内存存取)技术,由他向CPU申请IO权限,申请成功后则由DMA全权负责
22+
* 4.之后出现channel(通道),是完全独立的处理器,专门负责IO操作
23+
* 二、通道:用于源节点和目标节点的连接,在javaNIO中负责缓冲区中数据的传输,Channel本身不存储数据,因此需要配合缓冲区进行传输
24+
*
25+
* 三、通道的主要实现类
26+
* java.nio.channels.Channel
27+
* |-- FileChannel
28+
* |--SocketChannel
29+
* |--ServerSocketChannel
30+
* |--DatagramChannel
31+
*
32+
* 四、获取通道
33+
* 1.java 针对支持通道的类提供了getChannel()方法
34+
* 本地IO:FileInputStream/FileOutStream
35+
* RandomAccessFile
36+
*
37+
* 网络IO:
38+
* Socket
39+
* ServerSocket
40+
* DatagramSocket
41+
*
42+
* 2.在jdk1.7中的NIO .2 中针对各个通道提供了静态方法open()
43+
* 3.在jdk1.7中的NIO.2的Files工具类中的newByteChannel方法也能获取
44+
*
45+
* 五、通道之间的数据传输
46+
* transferFrom
47+
* transferTo
48+
**/
49+
public class ChannelTest {
50+
public static void main(String[] args) throws IOException {
51+
// test1();
52+
// test2();
53+
test3();
54+
}
55+
56+
/**
57+
* 通道之间的数据传输
58+
*/
59+
public static void test3() throws IOException {
60+
FileChannel inChannel = FileChannel.open(Paths.get("C://Ideaprojects/java-base/java-nio/src", "/1.jpg"), StandardOpenOption.READ);
61+
62+
FileChannel outChannel = FileChannel.open(Paths.get("C://Ideaprojects/java-base/java-nio/src","/4.jpg"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);
63+
64+
inChannel.transferTo(0,inChannel.size(),outChannel);
65+
outChannel.close();
66+
inChannel.close();
67+
}
68+
69+
/**
70+
* 使用直接缓冲区完成文件复制(内存映射文件)
71+
*/
72+
public static void test2() throws IOException {
73+
FileChannel inChannel = FileChannel.open(Paths.get("C://Ideaprojects/java-base/java-nio/src", "/1.jpg"), StandardOpenOption.READ);
74+
75+
FileChannel outChannel = FileChannel.open(Paths.get("C://Ideaprojects/java-base/java-nio/src","/3.jpg"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);
76+
77+
//内存映射文件,直接物理内存,只有byteBuffer支持
78+
MappedByteBuffer inByteBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
79+
80+
MappedByteBuffer outByteBuffer = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
81+
82+
//直接对缓冲区进行数据的读写操作
83+
byte[] dst = new byte[inByteBuffer.limit()];
84+
inByteBuffer.get(dst);
85+
outByteBuffer.put(dst);
86+
87+
}
88+
89+
90+
91+
/**
92+
* 利用通道完成文件复制
93+
*/
94+
public static void test1() throws IOException {
95+
// File file = new File("4.jpg");
96+
// FileInputStream fis = new FileInputStream(file);
97+
FileInputStream fis = new FileInputStream(Paths.get("C://Ideaprojects/java-base/java-nio/src","/1.jpg").toString());
98+
FileOutputStream fos = new FileOutputStream(Paths.get("C://Ideaprojects/java-base/java-nio/src","/2.jpg").toString());
99+
100+
FileChannel inChannel = fis.getChannel();
101+
FileChannel outChannel = fos.getChannel();
102+
103+
ByteBuffer buf = ByteBuffer.allocate(1024);
104+
while(inChannel.read(buf) != -1){
105+
buf.flip();
106+
outChannel.write(buf);
107+
buf.clear();
108+
}
109+
outChannel.close();
110+
inChannel.close();
111+
112+
}
113+
}

java-queue/java.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.asher.synchornizequeue;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author : 张勇杰
8+
* @date : 2019/8/13 11:49
9+
* @Version : v1.0
10+
* @description
11+
**/
12+
public class ListTest {
13+
List<Integer> list;
14+
15+
public static void main(String[] args) {
16+
// ListTest test = new ListTest();
17+
// test.dosomething();
18+
// int i = 2;
19+
//// i = ++i;
20+
//// System.out.println(i);
21+
int a=1;
22+
switch (a){
23+
case 1:
24+
System.out.println("1");
25+
break;
26+
case 2:
27+
System.out.println("2");
28+
}
29+
30+
}
31+
void dosomething(){
32+
List<String> stringList = new ArrayList<>();
33+
stringList.add("1");
34+
stringList.add("2");
35+
stringList.add("3");
36+
stringList.add("4");
37+
stringList.add("5");
38+
this.list = new ArrayList<>(stringList.size());
39+
for(String a:stringList){
40+
this.list.add(Integer.valueOf(a));
41+
}
42+
System.out.println(this.list);
43+
}
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.asher.synchornizequeue;
2+
3+
import java.util.concurrent.SynchronousQueue;
4+
5+
/**
6+
* @author : 张勇杰
7+
* @date : 2019/8/13 10:50
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class SynchornizedQueuePuter<T> implements Runnable{
12+
T value;
13+
SynchronousQueue<T> queue;
14+
15+
public SynchornizedQueuePuter(T value, SynchronousQueue<T> queue) {
16+
this.value = value;
17+
this.queue = queue;
18+
}
19+
20+
@Override
21+
public void run() {
22+
try {
23+
System.out.println(Thread.currentThread().getName()+"--beforeput--"+value);
24+
queue.put(value);
25+
System.out.println(Thread.currentThread().getName()+"--afterput--"+value);
26+
} catch (InterruptedException e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.asher.synchornizequeue;
2+
3+
import java.util.concurrent.SynchronousQueue;
4+
5+
/**
6+
* @author : 张勇杰
7+
* @date : 2019/8/13 10:57
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class SynchornizedQueueTaker<T> implements Runnable{
12+
T value;
13+
SynchronousQueue<T> queue;
14+
15+
public SynchornizedQueueTaker(SynchronousQueue<T> queue) {
16+
this.queue = queue;
17+
}
18+
19+
@Override
20+
public void run() {
21+
22+
try {
23+
System.out.println(Thread.currentThread().getName()+"--beginTaker--");
24+
value = queue.take();
25+
System.out.println(Thread.currentThread().getName()+"--afterTaker--"+value);
26+
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.asher.synchornizequeue;
2+
3+
import java.util.ArrayList;
4+
import java.util.concurrent.SynchronousQueue;
5+
6+
/**
7+
* @author : 张勇杰
8+
* @date : 2019/8/13 10:53
9+
* @Version : v1.0
10+
* @description
11+
**/
12+
public class SynchronousTest {
13+
public static void main(String[] args) throws InterruptedException {
14+
SynchronousQueue<String> queue = new SynchronousQueue<>();
15+
Thread thread1 = new Thread(new SynchornizedQueuePuter<String>("one", queue), "线程1");
16+
thread1.start();
17+
Thread thread2 = new Thread(new SynchornizedQueuePuter<String>("two", queue), "线程2");
18+
thread2.start();
19+
Thread thread3 = new Thread(new SynchornizedQueuePuter<String>("three", queue), "线程3");
20+
thread3.start();
21+
Thread thread4 = new Thread(new SynchornizedQueuePuter<String>("four", queue), "线程4");
22+
thread4.start();
23+
Thread thread5 = new Thread(new SynchornizedQueuePuter<String>("five", queue), "线程5");
24+
thread5.start();
25+
26+
27+
Thread.sleep(5000);
28+
Thread taker = new Thread(new SynchornizedQueueTaker<String>(queue),"取线程1");
29+
taker.start();
30+
}
31+
}

javasimple-RPC/javasimple-RPC.iml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.12" level="project" />
15+
<orderEntry type="library" name="Maven: org.apache.tomcat:tomcat-annotations-api:9.0.12" level="project" />
16+
<orderEntry type="library" name="Maven: org.apache.commons:commons.io:1.3.2" level="project" />
17+
<orderEntry type="library" name="Maven: io.netty:netty-all:4.1.16Final" level="project" />
18+
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.15" level="project" />
19+
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.6.1" level="project" />
20+
</component>
21+
</module>

0 commit comments

Comments
 (0)