From f5c197e5d8b09810dbad16d758727bd976b36835 Mon Sep 17 00:00:00 2001 From: lixiangrong Date: Mon, 18 Jul 2016 16:48:45 +0800 Subject: [PATCH] TestFile.java TestCountDownLatch.java --- .../osc/git/eh3/test/TestCountDownLatch.java | 47 +++++++++++++++++++ src/main/java/osc/git/eh3/test/TestFile.java | 37 +++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/osc/git/eh3/test/TestCountDownLatch.java create mode 100644 src/main/java/osc/git/eh3/test/TestFile.java diff --git a/src/main/java/osc/git/eh3/test/TestCountDownLatch.java b/src/main/java/osc/git/eh3/test/TestCountDownLatch.java new file mode 100644 index 0000000..0d0061b --- /dev/null +++ b/src/main/java/osc/git/eh3/test/TestCountDownLatch.java @@ -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 cache = new ConcurrentHashMap(); + + 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 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(); + } + } +} \ No newline at end of file diff --git a/src/main/java/osc/git/eh3/test/TestFile.java b/src/main/java/osc/git/eh3/test/TestFile.java new file mode 100644 index 0000000..4ee4647 --- /dev/null +++ b/src/main/java/osc/git/eh3/test/TestFile.java @@ -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) { + } + } + } + + } +}