Merge branch 'master' of git.oschina.net:eh3/useful-code

dev
lixiangrong 2016-07-24 21:15:36 +08:00
commit 85b3b48696
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package osc.git.eh3.test;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
public class TestCountDownLatch {
private static final int N = 10;
private static Map<String, Object> cache = new ConcurrentHashMap<String, Object>();
public static void main(String[] args) throws InterruptedException {
CountDownLatch doneSignal = new CountDownLatch(N);
for (int i = 1; i <= N; i++) {
new Thread(new Worker(i, doneSignal)).start();// 线程启动了
}
System.out.println("begin------------");
doneSignal.await();// 等待所有的线程执行完毕
System.out.println("Ok");
System.out.println(cache.size());
Set<String> keySet = cache.keySet();
for (String key : keySet) {
System.out.println(key + " = " + cache.get(key));
}
}
static class Worker implements Runnable {
private final CountDownLatch doneSignal;
private int beginIndex;
Worker(int beginIndex, CountDownLatch doneSignal) {
this.beginIndex = beginIndex;
this.doneSignal = doneSignal;
}
public void run() {
beginIndex = (beginIndex - 1) * 10 + 1;
for (int i = beginIndex; i <= beginIndex + 10; i++) {
cache.put(i + "key", i);
}
doneSignal.countDown();
}
}
}

View File

@ -0,0 +1,37 @@
package osc.git.eh3.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class TestFile {
public static void main(String[] args) {
File file = new File("C:\\Users\\lixiangrong\\Desktop\\20160628\\161845");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
String[] tempArr = tempString.split("\\|");
long time = Long.parseLong(tempArr[0]);
long rang = 1467100800000L;
if (time < rang) {
System.out.println(tempString);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
}