Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
personal-project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
xuyang
personal-project
Commits
9d7c22f4
提交
9d7c22f4
authored
6月 18, 2020
作者:
xuyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MemoryBarrier
上级
a890e8b4
隐藏空白字符变更
内嵌
并排
正在显示
9 个修改的文件
包含
266 行增加
和
20 行删除
+266
-20
ArrayTest.java
api/src/main/java/api/array/ArrayTest.java
+8
-8
ExceptionTest.java
api/src/main/java/api/exception/ExceptionTest.java
+3
-4
NioTest.java
api/src/main/java/api/io/nio/NioTest.java
+169
-0
NMsg.java
api/src/main/java/api/io/nio/nsocket/NMsg.java
+1
-1
NSocketClient.java
api/src/main/java/api/io/nio/nsocket/NSocketClient.java
+1
-1
NSocketClient2.java
api/src/main/java/api/io/nio/nsocket/NSocketClient2.java
+1
-1
NSocketServer.java
api/src/main/java/api/io/nio/nsocket/NSocketServer.java
+1
-5
ThreadDemo.java
api/src/main/java/api/thread/memory_barrier/ThreadDemo.java
+72
-0
pom.xml
pom.xml
+10
-0
没有找到文件。
api/src/main/java/api/array/ArrayTest.java
浏览文件 @
9d7c22f4
...
...
@@ -11,15 +11,15 @@ public class ArrayTest {
void
ArrayString
()
{
String
A
=
"Helloworld"
;
char
[]
array
=
A
.
toCharArray
();
//字符串转化为数组
Arrays
.
sort
(
array
);
//排序 从小到大 大写在小写前
//Arrays.sort(array); //排序 从小到大 大写在小写前0~9A~Za~z
for
(
int
i
=
array
.
length
-
1
;
i
>=
0
;
i
--)
{
//倒序输出
System
.
out
.
print
(
array
[
i
]);
}
int
intIndex
=
A
.
indexOf
(
"llo"
);
int
intIndex
=
A
.
indexOf
(
"
;
llo"
);
if
(
intIndex
==
-
1
)
{
System
.
out
.
println
(
"没有找到llo"
);
System
.
out
.
println
(
"
\n"
+
"
没有找到llo"
);
}
else
{
System
.
out
.
println
(
"\n"
+
"llo的位置"
+
intIndex
);
}
...
...
@@ -52,13 +52,13 @@ public class ArrayTest {
}
public
static
void
main
(
String
[]
args
)
{
/*
ArrayTest A = new ArrayTest();
ArrayTest
A
=
new
ArrayTest
();
A
.
ArrayString
();
A.StringBufferTest();
A.BooleanTest();*/
//
A.StringBufferTest();
//A.BooleanTest();
double
num
=
5.09
-
0.08
;
System
.
out
.
println
(
num
);
/*
double num = 5.09-0.08;
System.out.println(num);
*/
}
...
...
api/src/main/java/api/exception/ExceptionTest.java
浏览文件 @
9d7c22f4
...
...
@@ -16,11 +16,9 @@ public class ExceptionTest {
int
c1
=
fileReader
.
read
();
System
.
out
.
println
(
c1
);
}
catch
(
FileNotFound
Exception
e
)
{
//子类异常在父类异常前面
}
catch
(
IO
Exception
e
)
{
//子类异常在父类异常前面
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
}
finally
{
try
{
if
(
fileReader
!=
null
)
fileReader
.
close
();
...
...
@@ -31,6 +29,7 @@ public class ExceptionTest {
}
// java.lang.StackOverflowError
private
static
final
List
<
String
>
list
=
new
ArrayList
<>();
public
static
String
test2
(
String
j
){
int
i
=
1
,
s
=
1
,
f
=
1
,
a
=
1
,
b
=
1
,
c
=
1
,
d
=
1
,
e
=
1
;
...
...
api/src/main/java/api/io/nio/NioTest.java
0 → 100644
浏览文件 @
9d7c22f4
package
api
.
io
.
nio
;
import
cn.hutool.core.util.StrUtil
;
import
java.io.*
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.FileChannel
;
import
java.nio.charset.Charset
;
import
java.nio.charset.CharsetDecoder
;
public
class
NioTest
{
private
String
path
=
"E:\\worksite.json"
;
//读取文件中的内容
public
String
read
()
{
FileInputStream
fin
=
null
;
String
resultStr
=
null
;
ByteBuffer
buffer
=
null
;
try
{
fin
=
new
FileInputStream
(
path
);
// 获取通道
FileChannel
fc
=
fin
.
getChannel
();
// 创建缓冲区
buffer
=
ByteBuffer
.
allocate
(
1024
);
// 读取数据到缓冲区
if
(
fc
.
read
(
buffer
)
>
0
)
{
buffer
.
flip
();
//从读buffer->写buffer
/* while (buffer.remaining() > 0) {
byte b = buffer.get();
System.out.print(((char)b));
}*/
resultStr
=
Charset
.
forName
(
"UTF-8"
).
decode
(
buffer
).
toString
();
System
.
out
.
println
(
resultStr
);
}
fc
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
fin
!=
null
)
{
try
{
fin
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
return
resultStr
;
}
//将String存进文件中
public
void
write
(
String
resultStr
)
{
if
(
StrUtil
.
isBlank
(
resultStr
))
{
System
.
out
.
println
(
"输入参数为空"
);
return
;
}
FileOutputStream
fout
=
null
;
try
{
fout
=
new
FileOutputStream
(
"E:\\worksite.txt"
);
FileChannel
fc
=
fout
.
getChannel
();
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
1024
);
char
[]
resArr
=
resultStr
.
toCharArray
();
for
(
char
c
:
resArr
)
{
buffer
.
put
(
Character
.
valueOf
(
c
).
toString
().
getBytes
());
buffer
.
flip
();
fc
.
write
(
buffer
);
buffer
.
clear
();
}
System
.
out
.
println
(
"\n"
+
Charset
.
forName
(
"UTF-8"
).
decode
(
buffer
).
toString
());
fout
.
close
();
fc
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
fout
!=
null
)
{
try
{
fout
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
void
readNIO
()
{
FileInputStream
fin
=
null
;
try
{
fin
=
new
FileInputStream
(
new
File
(
path
));
FileChannel
channel
=
fin
.
getChannel
();
int
capacity
=
1024
;
// 字节
ByteBuffer
bf
=
ByteBuffer
.
allocate
(
capacity
);
System
.
out
.
println
(
"限制是:"
+
bf
.
limit
()
+
",容量是:"
+
bf
.
capacity
()
+
" ,位置是:"
+
bf
.
position
());
int
length
=
-
1
;
while
((
length
=
channel
.
read
(
bf
))
!=
-
1
)
{
/*
* 注意,读取后,将位置置为0,将limit置为容量, 以备下次读入到字节缓冲中,从0开始存储
* clear并不会实际清除buffer,而是重置position=0,limit=capacity
*/
bf
.
clear
();
byte
[]
bytes
=
bf
.
array
();
System
.
out
.
println
(
"start.............."
);
String
str
=
new
String
(
bytes
,
0
,
length
);
System
.
out
.
println
(
str
);
//System.out.write(bytes, 0, length);
System
.
out
.
println
(
"end................"
);
System
.
out
.
println
(
"限制是:"
+
bf
.
limit
()
+
"容量是:"
+
bf
.
capacity
()
+
"位置是:"
+
bf
.
position
());
}
channel
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
fin
!=
null
)
{
try
{
fin
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
void
writeNIO
()
{
FileOutputStream
fos
=
null
;
try
{
fos
=
new
FileOutputStream
(
new
File
(
path
));
FileChannel
channel
=
fos
.
getChannel
();
ByteBuffer
src
=
Charset
.
forName
(
"utf8"
).
encode
(
"你好你好你好你好你好"
);
// 字节缓冲的容量和limit会随着数据长度变化,不是固定不变的
System
.
out
.
println
(
"初始化容量和limit:"
+
src
.
capacity
()
+
","
+
src
.
limit
());
int
length
=
0
;
while
((
length
=
channel
.
write
(
src
))
!=
0
)
{
/*
* 注意,这里不需要clear,将缓冲中的数据写入到通道中后 第二次接着上一次的顺序往下读
*/
System
.
out
.
println
(
"写入长度:"
+
length
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
fos
!=
null
)
{
try
{
fos
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
static
void
main
(
String
[]
args
)
{
NioTest
nioTest
=
new
NioTest
();
//nioTest.write(nioTest.read());
nioTest
.
readNIO
();
}
}
api/src/main/java/api/io/nio/NMsg.java
→
api/src/main/java/api/io/nio/
nsocket/
NMsg.java
浏览文件 @
9d7c22f4
package
api
.
io
.
nio
;
package
api
.
io
.
nio
.
nsocket
;
import
lombok.Data
;
...
...
api/src/main/java/api/io/nio/NSocketClient.java
→
api/src/main/java/api/io/nio/
nsocket/
NSocketClient.java
浏览文件 @
9d7c22f4
package
api
.
io
.
nio
;
package
api
.
io
.
nio
.
nsocket
;
import
api.io.bio.Msg
;
import
com.alibaba.fastjson.JSON
;
...
...
api/src/main/java/api/io/nio/NSocketClient2.java
→
api/src/main/java/api/io/nio/
nsocket/
NSocketClient2.java
浏览文件 @
9d7c22f4
package
api
.
io
.
nio
;
package
api
.
io
.
nio
.
nsocket
;
import
api.io.bio.Msg
;
import
com.alibaba.fastjson.JSON
;
...
...
api/src/main/java/api/io/nio/NSocketServer.java
→
api/src/main/java/api/io/nio/
nsocket/
NSocketServer.java
浏览文件 @
9d7c22f4
package
api
.
io
.
nio
;
package
api
.
io
.
nio
.
nsocket
;
import
api.io.bio.Msg
;
import
com.alibaba.fastjson.JSON
;
import
java.io.IOException
;
import
java.net.InetSocketAddress
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.ServerSocketChannel
;
import
java.nio.channels.SocketChannel
;
import
java.nio.charset.Charset
;
import
java.nio.charset.StandardCharsets
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
api/src/main/java/api/thread/memory_barrier/ThreadDemo.java
0 → 100644
浏览文件 @
9d7c22f4
package
api
.
thread
.
memory_barrier
;
import
java.util.concurrent.atomic.AtomicBoolean
;
/**
* 测试多线程数据不一致问题
*/
class
ThreadDemo
implements
Runnable
{
//Solution1: volatile
/*private volatile boolean flag = false;*/
private
boolean
flag
=
false
;
//flag初始化为false
//Solution6: CAS
/*private AtomicBoolean flag = new AtomicBoolean(false);*/
@Override
public
void
run
()
{
//将flag设置为true
flag
=
true
;
/*flag.getAndSet(true);*/
System
.
out
.
println
(
"flag="
+
flag
);
}
public
boolean
isFlag
()
{
return
flag
;
}
/*public AtomicBoolean isFlag() {
return flag;
}*/
}
/**
* main
*/
class
Main
{
public
static
void
main
(
String
[]
args
)
{
ThreadDemo
threadDemo
=
new
ThreadDemo
();
new
Thread
(
threadDemo
).
start
();
//启动线程threadDemo,线程中设置flag为true
//Solution3: lock
/*Lock lock = new ReentrantLock();
lock.lock();*/
while
(
true
)
{
//Solution4: sleep()
/*try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
//Solution5: sout
/*System.out.println("output here");*/
//Solution2: synchronized
/*synchronized (threadDemo) {*/
if
(
threadDemo
.
isFlag
())
{
//主线程中判断flag为true/false
/*if (threadDemo.isFlag().get()) {*/
System
.
out
.
println
(
"----------------"
);
break
;
}
/*}*/
}
/*lock.unlock();*/
}
}
\ No newline at end of file
pom.xml
浏览文件 @
9d7c22f4
...
...
@@ -28,6 +28,16 @@
<artifactId>
fastjson
</artifactId>
<version>
1.2.70
</version>
</dependency>
<dependency>
<groupId>
commons-lang
</groupId>
<artifactId>
commons-lang
</artifactId>
<version>
2.6
</version>
</dependency>
<dependency>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-all
</artifactId>
<version>
4.6.7
</version>
</dependency>
</dependencies>
<build>
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论