update
parent
1a0271adde
commit
b254d582e4
Binary file not shown.
|
@ -0,0 +1,49 @@
|
||||||
|
package osc.git.eh3;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lixiangrong on 2017/3/27.
|
||||||
|
*/
|
||||||
|
public class Rename {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
File dir = new File("/Users/ehlxr/Desktop/_posts/");
|
||||||
|
|
||||||
|
File[] files = dir.listFiles();
|
||||||
|
for (File file : files) {
|
||||||
|
try {
|
||||||
|
String oName = file.getName();
|
||||||
|
|
||||||
|
String date = oName.substring(oName.lastIndexOf("-201") + 1, oName.indexOf(".md"));
|
||||||
|
String title = oName.substring(0, oName.lastIndexOf("-201"));
|
||||||
|
|
||||||
|
String nName = date + "-" + title + ".md";
|
||||||
|
|
||||||
|
|
||||||
|
copyFileUsingFileStreams(file, new File("/Users/ehlxr/Desktop/posts/" + nName));
|
||||||
|
} catch (Exception e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void copyFileUsingFileStreams(File source, File dest)
|
||||||
|
throws IOException {
|
||||||
|
InputStream input = null;
|
||||||
|
OutputStream output = null;
|
||||||
|
try {
|
||||||
|
input = new FileInputStream(source);
|
||||||
|
output = new FileOutputStream(dest);
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int bytesRead;
|
||||||
|
while ((bytesRead = input.read(buf)) > 0) {
|
||||||
|
output.write(buf, 0, bytesRead);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
input.close();
|
||||||
|
output.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,96 @@
|
||||||
|
package osc.git.eh3.cache;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.FutureTask;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lixiangrong on 2017/4/11.
|
||||||
|
*/
|
||||||
|
public class TestFutureCahe<K, V> {
|
||||||
|
private final ConcurrentHashMap<K, Future<V>> cacheMap = new ConcurrentHashMap<K, Future<V>>();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final TestFutureCahe<String, String> TestGuaVA = new TestFutureCahe<String, String>();
|
||||||
|
|
||||||
|
Thread t1 = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
System.out.println("T1======start========");
|
||||||
|
Object value = TestGuaVA.getCache("key", "T1");
|
||||||
|
System.out.println("T1 value==============" + value);
|
||||||
|
System.out.println("T1======end========");
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Thread t2 = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println("T2======start========");
|
||||||
|
Object value = TestGuaVA.getCache("key", "T2");
|
||||||
|
System.out.println("T2 value==============" + value);
|
||||||
|
System.out.println("T2======end========");
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Thread t3 = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println("T3======start========");
|
||||||
|
Object value = TestGuaVA.getCache("key", "T3");
|
||||||
|
System.out.println("T3 value==============" + value);
|
||||||
|
System.out.println("T3======end========");
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
t1.start();
|
||||||
|
t2.start();
|
||||||
|
t3.start();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getCache(K keyValue, String ThreadName) {
|
||||||
|
Future<V> value = null;
|
||||||
|
try {
|
||||||
|
System.out.println("ThreadName getCache==============" + ThreadName);
|
||||||
|
//从缓存获取数据
|
||||||
|
value = cacheMap.get(keyValue);
|
||||||
|
//如果没有的话,把数据放到缓存
|
||||||
|
if (value == null) {
|
||||||
|
value = putCache(keyValue, ThreadName);
|
||||||
|
return value.get();
|
||||||
|
}
|
||||||
|
return value.get();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Future<V> putCache(K keyValue, final String ThreadName) {
|
||||||
|
// //把数据放到缓存
|
||||||
|
Future<V> value = null;
|
||||||
|
Callable<V> callable = new Callable<V>() {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public V call() throws Exception {
|
||||||
|
//可以根据业务从数据库获取等取得数据,这边就模拟已经获取数据了
|
||||||
|
System.out.println("ThreadName 执行业务数据并返回处理结果的数据(访问数据库等)==============" + ThreadName);
|
||||||
|
return (V) "dataValue";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
FutureTask<V> futureTask = new FutureTask<V>(callable);
|
||||||
|
value = cacheMap.putIfAbsent(keyValue, futureTask);
|
||||||
|
if (value == null) {
|
||||||
|
value = futureTask;
|
||||||
|
futureTask.run();
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package osc.git.eh3.test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lixiangrong on 2017/4/14.
|
||||||
|
*/
|
||||||
|
public class TestReference {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Person person = new Person();
|
||||||
|
|
||||||
|
person.age = 20;
|
||||||
|
person.name = "test20";
|
||||||
|
System.out.println(person);
|
||||||
|
TestReference testReference = new TestReference();
|
||||||
|
testReference.test(person);
|
||||||
|
System.out.println(person);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void test(Person person) {
|
||||||
|
person = new Person();
|
||||||
|
person.age = 21;
|
||||||
|
person.name = "test21";
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Person {
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Person{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", age=" + age +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue