提交 9d7c22f4 authored 作者: xuyang's avatar xuyang

MemoryBarrier

上级 a890e8b4
......@@ -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);*/
}
......
......@@ -16,11 +16,9 @@ public class ExceptionTest {
int c1 = fileReader.read();
System.out.println(c1);
} catch (FileNotFoundException e) { //子类异常在父类异常前面
} catch (IOException 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;
......
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();
}
}
package api.io.nio;
package api.io.nio.nsocket;
import api.io.bio.Msg;
import com.alibaba.fastjson.JSON;
......
package api.io.nio;
package api.io.nio.nsocket;
import api.io.bio.Msg;
import com.alibaba.fastjson.JSON;
......
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;
......
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
......@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论