From 31b4dcbc6f9bcbcf9a86dbd059d03c8c6fbae001 Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 12:54:38 +0800 Subject: [PATCH 001/136] io --- LearnJava.iws | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/LearnJava.iws b/LearnJava.iws index e52c1ee..28ddfd5 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -72,8 +72,8 @@ - - + + @@ -611,7 +611,11 @@ 1398138817501 1398138817501 - - - + + From f65b993af56b7bea110be9b5802f1ffa009d67e3 Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 13:08:04 +0800 Subject: [PATCH 002/136] io --- LearnJava.iws | 106 +++++++++++++----- .../com/example/section2/OpenFileExample.java | 3 +- 2 files changed, 82 insertions(+), 27 deletions(-) diff --git a/LearnJava.iws b/LearnJava.iws index 28ddfd5..f96bfd0 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -2,8 +2,8 @@ - + @@ -69,11 +69,11 @@ - + - - + + @@ -81,11 +81,11 @@ - + - - + + @@ -124,8 +124,8 @@ - - + + @@ -182,8 +182,8 @@ @@ -261,6 +261,58 @@ - - - - - - - - - - + + - - + + + + + + + + + + diff --git a/NIO/src/main/java/com/example/section2/OpenFileExample.java b/NIO/src/main/java/com/example/section2/OpenFileExample.java index c4fdbcd..a4ade73 100644 --- a/NIO/src/main/java/com/example/section2/OpenFileExample.java +++ b/NIO/src/main/java/com/example/section2/OpenFileExample.java @@ -29,7 +29,6 @@ public static void main(String[] args) throws URISyntaxException, IOException { openFileForURL(url); openFileForURL(OpenFileExample.class.getResource("/nio-data.txt")); - openFileForPath(""); - + openFileForPath(OpenFileExample.class.getResource("/nio-data.txt").getFile()); } } From 5a5a91ef471ebcf0aae6209180045cd62101c5e5 Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 13:13:35 +0800 Subject: [PATCH 003/136] io --- LearnJava.iws | 16 ++++++++++------ .../com/example/section2/OpenFileExample.java | 12 +++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/LearnJava.iws b/LearnJava.iws index f96bfd0..33701ad 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -84,8 +84,8 @@ - - + + @@ -671,7 +671,11 @@ 1398142478198 1398142478198 - @@ -698,7 +702,7 @@ - + @@ -1058,8 +1062,8 @@ - - + + diff --git a/NIO/src/main/java/com/example/section2/OpenFileExample.java b/NIO/src/main/java/com/example/section2/OpenFileExample.java index a4ade73..24a513c 100644 --- a/NIO/src/main/java/com/example/section2/OpenFileExample.java +++ b/NIO/src/main/java/com/example/section2/OpenFileExample.java @@ -12,12 +12,22 @@ public class OpenFileExample { - + /** + * @param url 文件Url + * @throws URISyntaxException + * @throws IOException + */ public static void openFileForURL(URL url) throws URISyntaxException, IOException { File file = new File(url.toURI()); System.out.println(Files.readFirstLine(file, Charsets.UTF_8)); } + /** + * + * @param path 文件路径 + * @throws URISyntaxException + * @throws IOException + */ public static void openFileForPath(String path) throws URISyntaxException, IOException { File file = new File(path); System.out.println(Files.readFirstLine(file, Charsets.UTF_8)); From a6c88e5e819db3851c5b7be91fad2fa6aa1c907e Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 13:13:35 +0800 Subject: [PATCH 004/136] io --- LearnJava.iws | 147 +++++++++++------- .../com/example/section2/BufferExample.java | 7 + .../example/section2/FileChannelExample.java | 105 ++++++++++--- .../com/example/section2/OpenFileExample.java | 17 +- 4 files changed, 192 insertions(+), 84 deletions(-) create mode 100644 NIO/src/main/java/com/example/section2/BufferExample.java diff --git a/LearnJava.iws b/LearnJava.iws index f96bfd0..96de388 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -2,6 +2,8 @@ + + @@ -69,11 +71,11 @@ - + - - + + @@ -81,11 +83,21 @@ - + + + + + + + + + + + - - + + @@ -121,21 +133,21 @@ - - + + - - + + - - + + - - + + @@ -182,8 +194,9 @@ @@ -394,7 +407,7 @@ - + @@ -682,7 +703,7 @@ - + @@ -697,8 +718,8 @@ - - + + @@ -711,31 +732,31 @@ - - - - + + + - + + - - - + + + - + + - - + - + @@ -969,14 +990,6 @@ - - - - - - - - @@ -985,14 +998,6 @@ - - - - - - - - @@ -1041,29 +1046,53 @@ - + - + - - - - - + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + diff --git a/NIO/src/main/java/com/example/section2/BufferExample.java b/NIO/src/main/java/com/example/section2/BufferExample.java new file mode 100644 index 0000000..430ac68 --- /dev/null +++ b/NIO/src/main/java/com/example/section2/BufferExample.java @@ -0,0 +1,7 @@ +package com.example.section2; + +/** + * Created by Loon on 2014/4/22. + */ +public class BufferExample { +} diff --git a/NIO/src/main/java/com/example/section2/FileChannelExample.java b/NIO/src/main/java/com/example/section2/FileChannelExample.java index c5c2680..f5ff9c8 100644 --- a/NIO/src/main/java/com/example/section2/FileChannelExample.java +++ b/NIO/src/main/java/com/example/section2/FileChannelExample.java @@ -15,34 +15,91 @@ import java.nio.channels.FileChannel; import java.util.List; +/** + * FileChannel 写入,读取,写入特定位置,截取 + */ public class FileChannelExample { public static void main(String[] args) throws IOException, URISyntaxException { - URL url = Resources.getResource("nio-data.txt"); - - File file = new File(url.toURI()); - - -// RandomAccessFile aFile = new RandomAccessFile(url.toURI(), "rw"); -// FileChannel inChannel = aFile.getChannel(); -// -// ByteBuffer buf = ByteBuffer.allocate(48); -// -// int bytesRead = inChannel.read(buf); -// while (bytesRead != -1) { -// -// System.out.println("Read " + bytesRead); -// buf.flip(); -// -// while(buf.hasRemaining()){ -// System.out.print((char) buf.get()); -// } -// -// buf.clear(); -// bytesRead = inChannel.read(buf); -// } -// aFile.close(); + // Opening a FileChannel,Before you can use a FileChannel you must open it. + // You cannot open a FileChannel directly. You need to obtain a FileChannel via an InputStream, + // OutputStream, or a RandomAccessFile. Here is how you open a FileChannel via a RandomAccessFile: + RandomAccessFile aFile = new RandomAccessFile(FileChannelExample.class.getResource("/nio-data.txt").getFile(), "rw"); + FileChannel inChannel = aFile.getChannel(); + + // 以下无法同时执行 + writeDataToFileChannel(inChannel, "test"); + readDataToFileChannel(inChannel); + writeDataToFileChannel(inChannel, "1", 2); + + + // 截取文件流? +// inChannel.truncate(2); + + // When you are done using a FileChannel you must close it. + inChannel.close(); + aFile.close(); + + } + + // 写入数据 + public static void writeDataToFileChannel(FileChannel fileChannel, String context) throws IOException { + + ByteBuffer buf = ByteBuffer.allocate(48); + buf.clear(); + buf.put(context.getBytes()); + + buf.flip(); + + while (buf.hasRemaining()) { + fileChannel.write(buf); + } + } + + // 读取数据 + public static void readDataToFileChannel(FileChannel fileChannel) throws IOException { + + // First a Buffer is allocated. The data read from the FileChannel is read into the Buffer. + ByteBuffer buf = ByteBuffer.allocate(48); + + // Second the FileChannel.read() method is called. This method reads data from the FileChannel into theBuffer. + // The int returned by the read() method tells how many bytes were witten into the Buffer. If -1 is returned, + // the end-of-file is reached. + int bytesRead = fileChannel.read(buf); + while (bytesRead != -1) { + + System.out.println("Read " + bytesRead); + // 反转Buffer + buf.flip(); + + while (buf.hasRemaining()) { + System.out.print((char) buf.get()); + } + + buf.clear(); + bytesRead = fileChannel.read(buf); + } + + } + + + // 写入数据到指定位置 + public static void writeDataToFileChannel(FileChannel fileChannel, String context, long position) throws IOException { + + long pos = fileChannel.position(); + + fileChannel.position(position); + + ByteBuffer buf = ByteBuffer.allocate(48); + buf.clear(); + buf.put(context.getBytes()); + + buf.flip(); + + while (buf.hasRemaining()) { + fileChannel.write(buf); + } } } diff --git a/NIO/src/main/java/com/example/section2/OpenFileExample.java b/NIO/src/main/java/com/example/section2/OpenFileExample.java index a4ade73..afad8f3 100644 --- a/NIO/src/main/java/com/example/section2/OpenFileExample.java +++ b/NIO/src/main/java/com/example/section2/OpenFileExample.java @@ -10,14 +10,27 @@ import java.net.URISyntaxException; import java.net.URL; +/** + * + */ public class OpenFileExample { - + /** + * @param url 文件Url + * @throws URISyntaxException + * @throws IOException + */ public static void openFileForURL(URL url) throws URISyntaxException, IOException { File file = new File(url.toURI()); System.out.println(Files.readFirstLine(file, Charsets.UTF_8)); } + /** + * + * @param path 文件路径 + * @throws URISyntaxException + * @throws IOException + */ public static void openFileForPath(String path) throws URISyntaxException, IOException { File file = new File(path); System.out.println(Files.readFirstLine(file, Charsets.UTF_8)); @@ -31,4 +44,6 @@ public static void main(String[] args) throws URISyntaxException, IOException { openFileForPath(OpenFileExample.class.getResource("/nio-data.txt").getFile()); } + + } From 73b45b0e52b27a6e0cbcfd7eb127ca742311f1de Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 14:10:11 +0800 Subject: [PATCH 005/136] io --- LearnJava.iws | 43 +++++++------------ .../example/section2/FileChannelExample.java | 18 ++++---- 2 files changed, 24 insertions(+), 37 deletions(-) diff --git a/LearnJava.iws b/LearnJava.iws index 96de388..be2388a 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -2,10 +2,8 @@ - - @@ -74,8 +72,8 @@ - - + + @@ -96,8 +94,8 @@ - - + + @@ -643,23 +641,8 @@ - - @@ -785,9 +791,7 @@ - - - + @@ -1055,11 +1059,9 @@ - - - - - + + + @@ -1089,8 +1091,18 @@ - - + + + + + + + + + + + + diff --git a/NIO/src/main/java/com/example/section2/SocketChannelExample.java b/NIO/src/main/java/com/example/section2/SocketChannelExample.java new file mode 100644 index 0000000..3a9eb5f --- /dev/null +++ b/NIO/src/main/java/com/example/section2/SocketChannelExample.java @@ -0,0 +1,47 @@ +package com.example.section2; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; + +/** + * Created by Loon on 2014/4/22. + */ +public class SocketChannelExample { + + public static void main(String[] args) throws IOException { + SocketChannel socketChannel = SocketChannel.open(); + socketChannel.connect(new InetSocketAddress("localhost", 8080)); + + String newData = "New String to write to file..." + System.currentTimeMillis(); + + ByteBuffer buf = ByteBuffer.allocate(48); + buf.clear(); + buf.put(newData.getBytes()); + + buf.flip(); + + while (buf.hasRemaining()) { + socketChannel.write(buf); + } + + while (!socketChannel.finishConnect()) { + //wait, or do something else... + } + socketChannel.close(); + + } + + public void nonBlockingMode() throws IOException { + SocketChannel socketChannel = SocketChannel.open(); + socketChannel.configureBlocking(false); + socketChannel.connect(new InetSocketAddress("localhost", 8080)); + + + while (!socketChannel.finishConnect()) { + //wait, or do something else... + } + socketChannel.close(); + } +} From e8e0f66ce63f733472745304b4460f0479ecf4ba Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 15:01:15 +0800 Subject: [PATCH 008/136] io --- LearnJava.iws | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/LearnJava.iws b/LearnJava.iws index 0542914..a140bec 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -2,7 +2,6 @@ - @@ -690,7 +689,11 @@ 1398149498951 1398149498951 - From 483576954a0c1d7518e41f30d07f0128850c65fa Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 16:02:30 +0800 Subject: [PATCH 009/136] io --- .../java/com/example/section1/Calculator.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Concurrency/src/main/java/com/example/section1/Calculator.java diff --git a/Concurrency/src/main/java/com/example/section1/Calculator.java b/Concurrency/src/main/java/com/example/section1/Calculator.java new file mode 100644 index 0000000..1c626fe --- /dev/null +++ b/Concurrency/src/main/java/com/example/section1/Calculator.java @@ -0,0 +1,27 @@ +package com.example.section1; + + +public class Calculator implements Runnable { + + private int number; + + public Calculator(int number) { + this.number = number; + } + + public static void main(String[] args) { + for (int i = 1; i <= 10; i++) { + Calculator calculator = new Calculator(i); + Thread thread = new Thread(calculator); + thread.start(); + } + } + + @Override + public void run() { + for (int i = 0; i < 10; i++) { + System.out.printf("%s: %d * %d = %d\n", Thread.currentThread().getName(), number, i, i * number); + } + } +} + From 03ff3c7925ac5d91fbeb247fa4663eec74e08fb4 Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 17:03:22 +0800 Subject: [PATCH 010/136] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Concurrency/Concurrency.iml | 5 +- Concurrency/pom.xml | 21 +- .../com/example/section1/ThreadStatus.java | 83 ++++ Concurrency/src/main/resources/nio-data.txt | 0 LearnJava.iws | 412 ++++++++++++++---- 5 files changed, 431 insertions(+), 90 deletions(-) create mode 100644 Concurrency/src/main/java/com/example/section1/ThreadStatus.java create mode 100644 Concurrency/src/main/resources/nio-data.txt diff --git a/Concurrency/Concurrency.iml b/Concurrency/Concurrency.iml index 1f02819..8462873 100644 --- a/Concurrency/Concurrency.iml +++ b/Concurrency/Concurrency.iml @@ -5,12 +5,15 @@ - + + + + diff --git a/Concurrency/pom.xml b/Concurrency/pom.xml index 6d7caf3..d225d1a 100644 --- a/Concurrency/pom.xml +++ b/Concurrency/pom.xml @@ -1,6 +1,6 @@ - 4.0.0 @@ -8,5 +8,20 @@ Concurrency 1.0-SNAPSHOT - + + + com.google.guava + guava + 16.0.1 + + + + junit + junit + 4.11 + test + + + + \ No newline at end of file diff --git a/Concurrency/src/main/java/com/example/section1/ThreadStatus.java b/Concurrency/src/main/java/com/example/section1/ThreadStatus.java new file mode 100644 index 0000000..1c703c5 --- /dev/null +++ b/Concurrency/src/main/java/com/example/section1/ThreadStatus.java @@ -0,0 +1,83 @@ +package com.example.section1; + +import com.google.common.base.Charsets; +import com.google.common.io.Files; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; + +/** + * Created by Loon on 2014/4/22. + */ +public class ThreadStatus implements Runnable { + + private int number; + + public ThreadStatus(int number) { + this.number = number; + } + + public static void main(String[] args) throws URISyntaxException, IOException { + + Thread threads[] = new Thread[10]; + Thread.State status[] = new Thread.State[10]; + +// URL url = Resources.getResource("nio-data.txt"); + File file = new File("D:\\other\\LearnJava\\Concurrency\\src\\main\\resources\\nio-data.txt"); + + for (int i = 0; i < 10; i++) { + threads[i] = new Thread(new ThreadStatus(i)); + if ((i % 2) == 0) { + threads[i].setPriority(Thread.MAX_PRIORITY); + } else { + threads[i].setPriority(Thread.MIN_PRIORITY); + } + threads[i].setName("Thread " + i); + fileAppend(file, "Main : Status of Thread " + i + " : " + threads[i].getState() + System.getProperty("line.separator")); + status[i] = threads[i].getState(); + } + + for (int i = 0; i < 10; i++) { + threads[i].start(); + } + + + boolean finish = false; + while (!finish) { + for (int i = 0; i < 10; i++) { + if (threads[i].getState() != status[i]) { + writeThreadInfo(file, threads[i], status[i]); + status[i] = threads[i].getState(); + } + } + + finish = true; + for (int i = 0; i < 10; i++) { + finish = finish && (threads[i].getState() == Thread.State.TERMINATED); + } + } + + } + + private static void writeThreadInfo(File file, Thread thread, Thread.State state) throws IOException { + + // 使用 StandardSystemProperty.LINE_SEPARATOR 出错 + fileAppend(file, String.format("Main : Id %d - %s", thread.getId(), thread.getName()) + System.getProperty("line.separator")); + fileAppend(file, String.format("Main : Priority: %d", thread.getPriority()) + System.getProperty("line.separator")); + fileAppend(file, String.format("Main : Old State: %s", state) + System.getProperty("line.separator")); + fileAppend(file, String.format("Main : New State: %s", thread.getState()) + System.getProperty("line.separator")); + fileAppend(file, "Main : ************************************" + System.getProperty("line.separator")); + } + + private static void fileAppend(File file, String context) throws IOException { + Files.append(context, file, Charsets.UTF_8); + } + + @Override + public void run() { + for (int i = 0; i <= 10; i++) { + System.out.printf("%s: %d * %d = %d\n", Thread.currentThread().getName(), number, i, i * number); + } + } +} diff --git a/Concurrency/src/main/resources/nio-data.txt b/Concurrency/src/main/resources/nio-data.txt new file mode 100644 index 0000000..e69de29 diff --git a/LearnJava.iws b/LearnJava.iws index a140bec..17ecb07 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -2,7 +2,11 @@ + + + + @@ -71,18 +75,8 @@ - + - - - - - - - - - - @@ -90,83 +84,87 @@ - - + + - - - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - + + + + + + + + + - - + + - - + + - - + + - - + + @@ -197,6 +195,10 @@ @@ -327,6 +329,136 @@ + + - + + + + - + + - - - - - + + + + + + @@ -693,7 +846,15 @@ 1398150072198 1398150072198 - @@ -713,7 +874,7 @@ - + @@ -743,7 +904,7 @@ - + @@ -794,7 +955,9 @@ - + + + @@ -874,6 +1037,7 @@ + @@ -897,6 +1061,7 @@ + @@ -928,6 +1093,7 @@ + @@ -951,6 +1117,7 @@ + @@ -984,13 +1151,6 @@ - - - - - - - @@ -1052,10 +1212,54 @@ - + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1064,50 +1268,86 @@ + + + + + + + + + + - + - - + + - + - - + + - + - - + + - + - - - - - + + + - + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + From 6166cb009c4abbcfe1066dbea892963075db99b2 Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 17:15:07 +0800 Subject: [PATCH 011/136] =?UTF-8?q?=E4=B8=AD=E6=96=AD=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../section1/ThreadInterruptedExample.java | 20 ++++ LearnJava.iws | 110 +++++++++++------- 2 files changed, 91 insertions(+), 39 deletions(-) create mode 100644 Concurrency/src/main/java/com/example/section1/ThreadInterruptedExample.java diff --git a/Concurrency/src/main/java/com/example/section1/ThreadInterruptedExample.java b/Concurrency/src/main/java/com/example/section1/ThreadInterruptedExample.java new file mode 100644 index 0000000..b8eb06f --- /dev/null +++ b/Concurrency/src/main/java/com/example/section1/ThreadInterruptedExample.java @@ -0,0 +1,20 @@ +package com.example.section1; + +/** + * Created by Loon on 2014/4/22. + */ +public class ThreadInterruptedExample extends Thread { + + public static void main(String[] args) { + Thread thread = new ThreadInterruptedExample(); + thread.start(); + System.out.println(thread.isInterrupted()); + thread.interrupt(); + System.out.println(thread.isInterrupted()); + } + + @Override + public void run() { + System.out.println(System.currentTimeMillis()); + } +} diff --git a/LearnJava.iws b/LearnJava.iws index 17ecb07..ce3cca8 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,12 +1,9 @@ - - - - + + - @@ -134,10 +131,10 @@ - + - + @@ -150,6 +147,19 @@ + + + + + + + + + + + + + @@ -199,6 +209,8 @@ @@ -530,6 +542,8 @@ + + @@ -541,24 +555,7 @@ - - - - + @@ -642,6 +639,23 @@ + + + - - - - - + + + + + - - - - - + + + + + @@ -854,7 +868,11 @@ 1398153750351 1398153750351 - @@ -862,7 +880,7 @@ - + @@ -938,7 +956,10 @@ - - + @@ -1352,6 +1373,17 @@ + + + + + + + + + + + From 7d004ad166ada2cabaf3a9c57d06723cd7260a61 Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 17:27:34 +0800 Subject: [PATCH 012/136] =?UTF-8?q?=E6=8D=95=E8=8E=B7=E4=B8=AD=E6=96=AD?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=8A=A8=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/section1/FileSearch.java | 70 +++++++++ LearnJava.iws | 136 ++++++++---------- 2 files changed, 131 insertions(+), 75 deletions(-) create mode 100644 Concurrency/src/main/java/com/example/section1/FileSearch.java diff --git a/Concurrency/src/main/java/com/example/section1/FileSearch.java b/Concurrency/src/main/java/com/example/section1/FileSearch.java new file mode 100644 index 0000000..3178e3e --- /dev/null +++ b/Concurrency/src/main/java/com/example/section1/FileSearch.java @@ -0,0 +1,70 @@ +package com.example.section1; + +import java.io.File; +import java.util.concurrent.TimeUnit; + +/** + * Created by Loon on 2014/4/22. + */ +public class FileSearch implements Runnable { + + private String initPath; + private String fileName; + + public FileSearch(String initPath, String fileName) { + this.initPath = initPath; + this.fileName = fileName; + } + + public static void main(String[] args) { + FileSearch fileSearch = new FileSearch("D:\\other\\LearnJava\\", "nio-data.txt"); + Thread thread = new Thread(fileSearch); + thread.start(); + + try { + TimeUnit.SECONDS.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + thread.interrupt(); + } + + @Override + public void run() { + File file = new File(initPath); + if (file.isDirectory()) { + try { + directoryProcess(file); + } catch (InterruptedException e) { + System.out.printf("%s: The search has been interrupted", Thread.currentThread().getName()); + } + } + } + + private void directoryProcess(File file) throws InterruptedException { + File list[] = file.listFiles(); + if (list != null) { + for (File aList : list) { + if (aList.isDirectory()) { + directoryProcess(aList); + } else { + fileProcess(aList); + } + } + } + if (Thread.interrupted()) { + throw new InterruptedException(); + } + } + + private void fileProcess(File file) throws InterruptedException { + if (file.getName().equals(fileName)) { + System.out.printf("%s : %s\n", Thread.currentThread().getName(), file.getAbsolutePath()); + } + if (Thread.interrupted()) { + throw new InterruptedException(); + } + } +} + + diff --git a/LearnJava.iws b/LearnJava.iws index ce3cca8..7bef957 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -2,7 +2,7 @@ - + @@ -111,16 +111,6 @@ - - - - - - - - - - @@ -134,7 +124,7 @@ - + @@ -147,11 +137,11 @@ - + - - + + @@ -160,6 +150,18 @@ + + + + + + + + + + + + @@ -211,6 +213,7 @@ @@ -419,32 +422,6 @@ - - + + @@ -1384,6 +1360,16 @@ + + + + + + + + + + From 25415323eccfb6476693421049f68be9a8cd1def Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 17:31:26 +0800 Subject: [PATCH 013/136] =?UTF-8?q?=E6=8D=95=E8=8E=B7=E4=B8=AD=E6=96=AD?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=8A=A8=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/section1/FileSearch.java | 16 ++++++------ LearnJava.iws | 25 +++++++++++-------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Concurrency/src/main/java/com/example/section1/FileSearch.java b/Concurrency/src/main/java/com/example/section1/FileSearch.java index 3178e3e..9338804 100644 --- a/Concurrency/src/main/java/com/example/section1/FileSearch.java +++ b/Concurrency/src/main/java/com/example/section1/FileSearch.java @@ -43,13 +43,15 @@ public void run() { private void directoryProcess(File file) throws InterruptedException { File list[] = file.listFiles(); - if (list != null) { - for (File aList : list) { - if (aList.isDirectory()) { - directoryProcess(aList); - } else { - fileProcess(aList); - } + if (list != null || list.length == 0) { + return; + } + + for (File aList : list) { + if (aList.isDirectory()) { + directoryProcess(aList); + } else { + fileProcess(aList); } } if (Thread.interrupted()) { diff --git a/LearnJava.iws b/LearnJava.iws index 7bef957..1e58380 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,8 +1,8 @@ - - + + @@ -153,8 +153,8 @@ - - + + @@ -848,7 +848,11 @@ 1398158107963 1398158107963 - @@ -935,7 +939,8 @@ - @@ -1240,7 +1245,7 @@ - + @@ -1362,8 +1367,8 @@ - - + + From a084007f76533de09d3510b695c6988ed3eecaf0 Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 17:47:17 +0800 Subject: [PATCH 014/136] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E4=B8=AD=E6=96=AD?= =?UTF-8?q?=E5=92=8C=E6=81=A2=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/section1/FileClock.java | 32 +++++ LearnJava.iws | 122 ++++++++++-------- 2 files changed, 98 insertions(+), 56 deletions(-) create mode 100644 Concurrency/src/main/java/com/example/section1/FileClock.java diff --git a/Concurrency/src/main/java/com/example/section1/FileClock.java b/Concurrency/src/main/java/com/example/section1/FileClock.java new file mode 100644 index 0000000..5457ae6 --- /dev/null +++ b/Concurrency/src/main/java/com/example/section1/FileClock.java @@ -0,0 +1,32 @@ +package com.example.section1; + +import java.util.Date; +import java.util.concurrent.TimeUnit; + +/** + * Created by Loon on 2014/4/22. + */ +public class FileClock implements Runnable { + public static void main(String[] args) throws InterruptedException { + FileClock fileClock = new FileClock(); + Thread thread = new Thread(fileClock); + thread.start(); + + TimeUnit.SECONDS.sleep(5); + // todo:why?为什么当中断后,线程还在继续跑? + thread.interrupt(); + + } + + @Override + public void run() { + for (int i = 0; i < 10; i++) { + System.out.printf("%s\n", new Date()); + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e) { + System.out.printf("The FileClock has been interrupted"); + } + } + } +} diff --git a/LearnJava.iws b/LearnJava.iws index 1e58380..dfc7f99 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -2,7 +2,7 @@ - + @@ -111,16 +111,6 @@ - - - - - - - - - - @@ -150,11 +140,23 @@ - + - - + + + + + + + + + + + + + + @@ -214,6 +216,7 @@ @@ -237,7 +240,7 @@ - + @@ -532,7 +535,7 @@ - + @@ -555,14 +558,9 @@ - - - - - - @@ -868,7 +876,7 @@ - + @@ -878,7 +886,7 @@ - + @@ -946,23 +954,15 @@ - + + - - - - - - - - - - @@ -1367,8 +1367,18 @@ - - + + + + + + + + + + + + From 9dac2e2ff9edd40b2029a77cfbcb860f8dc8a198 Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 17:57:55 +0800 Subject: [PATCH 015/136] =?UTF-8?q?=E7=AD=89=E5=BE=85=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E7=9A=84=E7=BB=88=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/section1/DataSourcesLoader.java | 40 ++++ .../section1/NetworkConnectionsLoader.java | 20 ++ LearnJava.iws | 180 ++++++++++-------- 3 files changed, 156 insertions(+), 84 deletions(-) create mode 100644 Concurrency/src/main/java/com/example/section1/DataSourcesLoader.java create mode 100644 Concurrency/src/main/java/com/example/section1/NetworkConnectionsLoader.java diff --git a/Concurrency/src/main/java/com/example/section1/DataSourcesLoader.java b/Concurrency/src/main/java/com/example/section1/DataSourcesLoader.java new file mode 100644 index 0000000..e030773 --- /dev/null +++ b/Concurrency/src/main/java/com/example/section1/DataSourcesLoader.java @@ -0,0 +1,40 @@ +package com.example.section1; + +import java.util.Date; +import java.util.concurrent.TimeUnit; + +/** + * Created by Loon on 2014/4/22. + */ +public class DataSourcesLoader implements Runnable { + public static void main(String[] args) { + DataSourcesLoader dsLoader = new DataSourcesLoader(); + Thread thread1 = new Thread(dsLoader, "DataSourceThread"); + + NetworkConnectionsLoader ncLoader = new NetworkConnectionsLoader(); + Thread thread2 = new Thread(ncLoader, "NetworkConnectionLoad er"); + + thread1.start(); + thread2.start(); + + try { + thread1.join(); + thread2.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + System.out.printf("Main: Configuration has been loaded: %s\n", new Date()); + } + + @Override + public void run() { + System.out.printf("Beginning data sources loading: %s\n", new Date()); + try { + TimeUnit.SECONDS.sleep(4); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.printf("Data sources loading has finished:%s\n", new Date()); + } +} diff --git a/Concurrency/src/main/java/com/example/section1/NetworkConnectionsLoader.java b/Concurrency/src/main/java/com/example/section1/NetworkConnectionsLoader.java new file mode 100644 index 0000000..9670a8c --- /dev/null +++ b/Concurrency/src/main/java/com/example/section1/NetworkConnectionsLoader.java @@ -0,0 +1,20 @@ +package com.example.section1; + +import java.util.Date; +import java.util.concurrent.TimeUnit; + +/** + * Created by Loon on 2014/4/22. + */ +public class NetworkConnectionsLoader implements Runnable { + @Override + public void run() { + System.out.printf("Beginning netWork Connect loading: %s\n", new Date()); + try { + TimeUnit.SECONDS.sleep(6); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.printf("netWork Connect loading has finished:%s\n", new Date()); + } +} diff --git a/LearnJava.iws b/LearnJava.iws index dfc7f99..896a7ba 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,8 +1,9 @@ - - + + + @@ -69,28 +70,6 @@ - - - - - - - - - - - - - - - - - - - - - - @@ -152,11 +131,35 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -217,6 +220,8 @@ @@ -531,33 +536,14 @@ + + + - - - - - - - + + + + - - - - - + + + + + - - - - - + + + + + @@ -860,7 +863,11 @@ 1398159086467 1398159086467 - @@ -876,7 +883,7 @@ - + @@ -887,7 +894,7 @@ - + @@ -910,7 +917,7 @@ - + @@ -919,7 +926,7 @@ - + @@ -948,7 +955,8 @@ - - - - - - - - - - - - - - - - - @@ -1377,8 +1369,28 @@ - - + + + + + + + + + + + + + + + + + + + + + + From 93c1664afe81fa0c49feb51a7ee91a8bd5719c0f Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 17:59:03 +0800 Subject: [PATCH 016/136] =?UTF-8?q?=E7=AD=89=E5=BE=85=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E7=9A=84=E7=BB=88=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/section1/DataSourcesLoader.java | 1 + LearnJava.iws | 24 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Concurrency/src/main/java/com/example/section1/DataSourcesLoader.java b/Concurrency/src/main/java/com/example/section1/DataSourcesLoader.java index e030773..249cd74 100644 --- a/Concurrency/src/main/java/com/example/section1/DataSourcesLoader.java +++ b/Concurrency/src/main/java/com/example/section1/DataSourcesLoader.java @@ -5,6 +5,7 @@ /** * Created by Loon on 2014/4/22. + * source:http://ifeve.com/thread-management-7/ */ public class DataSourcesLoader implements Runnable { public static void main(String[] args) { diff --git a/LearnJava.iws b/LearnJava.iws index 896a7ba..d8d8430 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,9 +1,8 @@ - - - + + @@ -146,8 +145,8 @@ - - + + @@ -220,8 +219,8 @@ @@ -867,7 +866,11 @@ 1398160037745 1398160037745 - @@ -956,7 +959,8 @@ - - - + + From 710216ccc383b851ed0e8e29e9d118f590b00a40 Mon Sep 17 00:00:00 2001 From: Loon Date: Tue, 22 Apr 2014 18:01:14 +0800 Subject: [PATCH 017/136] =?UTF-8?q?=E6=BA=90=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Concurrency/src/main/java/package-info.java | 4 ++ LearnJava.iws | 51 +++++++++++---------- 2 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 Concurrency/src/main/java/package-info.java diff --git a/Concurrency/src/main/java/package-info.java b/Concurrency/src/main/java/package-info.java new file mode 100644 index 0000000..9c32670 --- /dev/null +++ b/Concurrency/src/main/java/package-info.java @@ -0,0 +1,4 @@ +/** + * Created by Loon on 2014/4/22. + * http://ifeve.com/java-7-concurrency-cookbook/ + */ diff --git a/LearnJava.iws b/LearnJava.iws index d8d8430..c171b89 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -2,7 +2,7 @@ - + @@ -79,16 +79,6 @@ - - - - - - - - - - @@ -142,10 +132,10 @@ - + - + @@ -154,6 +144,16 @@ + + + + + + + + + + @@ -221,6 +221,7 @@ @@ -870,7 +871,11 @@ 1398160675824 1398160675824 - @@ -1048,7 +1102,6 @@ - @@ -1064,7 +1117,6 @@ - @@ -1072,7 +1124,6 @@ - @@ -1096,7 +1147,6 @@ - @@ -1120,7 +1170,6 @@ - @@ -1128,7 +1177,6 @@ - @@ -1150,7 +1198,6 @@ - @@ -1173,7 +1220,6 @@ - @@ -1181,7 +1227,6 @@ - @@ -1203,7 +1248,6 @@ - @@ -1211,7 +1255,6 @@ - @@ -1219,7 +1262,6 @@ - @@ -1227,9 +1269,6 @@ - - - @@ -1237,9 +1276,6 @@ - - - @@ -1247,7 +1283,6 @@ - @@ -1255,7 +1290,6 @@ - @@ -1263,9 +1297,6 @@ - - - @@ -1297,7 +1328,6 @@ - @@ -1305,7 +1335,6 @@ - @@ -1313,7 +1342,6 @@ - @@ -1337,13 +1365,7 @@ - - - - - - - + @@ -1351,10 +1373,31 @@ - - - - + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1362,46 +1405,57 @@ - - - + - + - - + + - + + + + + + + - + - + - + + - + - - - - - + + + - + + + + + + + + + - - + + From 2d05aa474aff6ce75260dfeed3cc96b48beaf38b Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 10:56:46 +0800 Subject: [PATCH 019/136] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E4=B8=AD=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Collection/Collection.iml | 2 +- .../section1/Interrupt/TestInterrupt2.java | 29 ++ .../section1/Interrupt/TestInterrupt3.java | 44 +++ DesignPatterns/DesignPatterns.iml | 2 +- Enum/Enum.iml | 2 +- LearnJava.ipr | 5 + LearnJava.iws | 295 +++++------------- Reflection/Reflection.iml | 2 +- Util/Util.iml | 2 +- 9 files changed, 167 insertions(+), 216 deletions(-) create mode 100644 Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt3.java diff --git a/Collection/Collection.iml b/Collection/Collection.iml index c46d21f..638896b 100644 --- a/Collection/Collection.iml +++ b/Collection/Collection.iml @@ -1,5 +1,5 @@ - + diff --git a/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt2.java b/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt2.java index fb526f8..9c83b72 100644 --- a/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt2.java +++ b/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt2.java @@ -1,7 +1,36 @@ package com.example.section1.Interrupt; +import java.util.concurrent.TimeUnit; + /** * Created by Loon on 2014/4/23. */ public class TestInterrupt2 { + public static void main(String[] args) { + Thread t = new MyThread(); + t.start(); + t.interrupt(); + System.out.println("已调用线程的interrupt方法"); + } + + static class MyThread extends Thread { + private static int longTimeRunningInterruptMethod(int count, int initNum) throws InterruptedException { + for (int i = 0; i < count; i++) { + TimeUnit.SECONDS.sleep(5); + } + return initNum; + } + + public void run() { + int num = -1; + try { + num = longTimeRunningInterruptMethod(2, 0); + } catch (InterruptedException e) { + System.out.println("线程被中断"); + throw new RuntimeException(e); + } + System.out.println("长时间任务运行结束,num=" + num); + System.out.println("线程的中断状态:" + Thread.interrupted()); + } + } } diff --git a/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt3.java b/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt3.java new file mode 100644 index 0000000..4dea7f6 --- /dev/null +++ b/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt3.java @@ -0,0 +1,44 @@ +package com.example.section1.Interrupt; + +/** + * Created by Loon on 2014/4/23. + */ +public class TestInterrupt3 { + public static void main(String[] args) throws Exception { + Thread t = new MyThread(); + t.start(); +// TimeUnit.SECONDS.sleep(1);//如果不能看到处理过程中被中断的情形,可以启用这句再看看效果 + t.interrupt(); + System.out.println("已调用线程的interrupt方法"); + } + + static class MyThread extends Thread { + private static int longTimeRunningNonInterruptMethod(int count, int initNum) throws InterruptedException { + if (interrupted()) { + throw new InterruptedException("正式处理前线程已经被请求中断"); + } + for (int i = 0; i < count; i++) { + for (int j = 0; j < Integer.MAX_VALUE; j++) { + initNum++; + } + //假如这就是一个合适的地方 + if (interrupted()) { + //回滚数据,清理操作等 + throw new InterruptedException("线程正在处理过程中被中断"); + } + } + return initNum; + } + + public void run() { + int num; + try { + num = longTimeRunningNonInterruptMethod(2, 0); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println("长时间任务运行结束,num=" + num); + System.out.println("线程的中断状态:" + Thread.interrupted()); + } + } +} diff --git a/DesignPatterns/DesignPatterns.iml b/DesignPatterns/DesignPatterns.iml index c46d21f..638896b 100644 --- a/DesignPatterns/DesignPatterns.iml +++ b/DesignPatterns/DesignPatterns.iml @@ -1,5 +1,5 @@ - + diff --git a/Enum/Enum.iml b/Enum/Enum.iml index c46d21f..638896b 100644 --- a/Enum/Enum.iml +++ b/Enum/Enum.iml @@ -1,5 +1,5 @@ - + diff --git a/LearnJava.ipr b/LearnJava.ipr index 8e4bf3f..ba087ca 100644 --- a/LearnJava.ipr +++ b/LearnJava.ipr @@ -171,10 +171,15 @@ + + + + + diff --git a/LearnJava.iws b/LearnJava.iws index 287d758..9571694 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,13 +1,16 @@ - - - - - - + + + + + + + + + @@ -73,16 +76,6 @@ - - - - - - - - - - @@ -99,13 +92,13 @@ - - - - - - - + + + + + + + @@ -144,11 +137,21 @@ - + - - + + + + + + + + + + + + @@ -224,6 +227,8 @@ @@ -322,110 +327,6 @@ - - + + - - + - - + - - + - - + - - - - - - + + + + + - - - - - + + + + + @@ -942,7 +809,11 @@ 1398160874596 1398160874596 - @@ -989,7 +860,7 @@ - + @@ -1053,7 +924,8 @@ - - - - - - - - @@ -1414,13 +1279,13 @@ - - - - - - - + + + + + + + @@ -1454,8 +1319,16 @@ - - + + + + + + + + + + diff --git a/Reflection/Reflection.iml b/Reflection/Reflection.iml index c46d21f..638896b 100644 --- a/Reflection/Reflection.iml +++ b/Reflection/Reflection.iml @@ -1,5 +1,5 @@ - + diff --git a/Util/Util.iml b/Util/Util.iml index c46d21f..638896b 100644 --- a/Util/Util.iml +++ b/Util/Util.iml @@ -1,5 +1,5 @@ - + From 9c909681bcd7af9314bb2af3638631d0262c6feb Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 13:16:46 +0800 Subject: [PATCH 020/136] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E4=B8=AD=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/example/deque/Deque1.java | 49 +++ .../example/section1/Daemon/CleanerTask.java | 46 +++ .../com/example/section1/Daemon/Main.java | 21 ++ .../example/section1/Daemon/WriterTask.java | 14 +- LearnJava.iws | 330 ++++++++++++------ 5 files changed, 345 insertions(+), 115 deletions(-) create mode 100644 Collection/src/main/java/com/example/deque/Deque1.java create mode 100644 Concurrency/src/main/java/com/example/section1/Daemon/CleanerTask.java create mode 100644 Concurrency/src/main/java/com/example/section1/Daemon/Main.java diff --git a/Collection/src/main/java/com/example/deque/Deque1.java b/Collection/src/main/java/com/example/deque/Deque1.java new file mode 100644 index 0000000..387be7b --- /dev/null +++ b/Collection/src/main/java/com/example/deque/Deque1.java @@ -0,0 +1,49 @@ +package com.example.deque; + +import java.util.*; + +/** + * Created by Loon on 2014/4/23. + */ +public class Deque1 { + + public static void main(String[] args) { + Deque deque = new ArrayDeque(); + deque.addLast("bat"); + deque.add("cat"); + deque.addFirst("ape"); + System.out.println(deque); + System.out.println(); + + System.out.println("Enumerate the Deque"); + Enumeration e = Collections.enumeration(deque); + while (e.hasMoreElements()) + System.out.println(e.nextElement()); + System.out.println(); + + System.out.println("Iterate through the Deque"); + for (Iterator iter = deque.descendingIterator(); iter.hasNext(); ) { + System.out.println(iter.next()); + } + System.out.println(); + + + System.out.println("Demonstrate access"); +// System.out.println("deque.at( 0 ) = " + deque.at(0)); + System.out.println("deque.getFirst() = " + deque.getFirst()); + System.out.println("deque.element() = " + deque.element()); +// System.out.println("deque.at( 2 ) = " + deque.at(2)); + System.out.println("deque.getLast() = " + deque.getLast()); + System.out.println(); + + System.out.println("Demonstrate modification"); + deque.push("fox"); + System.out.println(deque); + + deque.removeFirst(); + System.out.println("After popFront() = " + deque); + + deque.removeLast(); + System.out.println("After popBack() = " + deque); + } +} diff --git a/Concurrency/src/main/java/com/example/section1/Daemon/CleanerTask.java b/Concurrency/src/main/java/com/example/section1/Daemon/CleanerTask.java new file mode 100644 index 0000000..812fed9 --- /dev/null +++ b/Concurrency/src/main/java/com/example/section1/Daemon/CleanerTask.java @@ -0,0 +1,46 @@ +package com.example.section1.Daemon; + +import java.util.Date; +import java.util.Deque; + +/** + * Created by Loon on 2014/4/23. + */ +public class CleanerTask extends Thread { + + private Deque deque; + + public CleanerTask(Deque deque) { + this.deque = deque; + setDaemon(true); + } + + @Override + public void run() { + while (true) { + Date date = new Date(); + clean(date); + } + } + + private void clean(Date date) { + long difference; + boolean delete; + if (deque.size() == 0) { + return; + } + delete = false; + do { + Event e = deque.getLast(); + difference = date.getTime() - e.getDate().getTime(); + if (difference > 10000) { + System.out.printf("Cleaner: %s\n", e.getEvent()); + deque.removeLast(); + delete = true; + } + } while (difference > 10000); + if (delete) { + System.out.printf("Cleaner: Size of the queue: %d\n", deque.size()); + } + } +} diff --git a/Concurrency/src/main/java/com/example/section1/Daemon/Main.java b/Concurrency/src/main/java/com/example/section1/Daemon/Main.java new file mode 100644 index 0000000..720e116 --- /dev/null +++ b/Concurrency/src/main/java/com/example/section1/Daemon/Main.java @@ -0,0 +1,21 @@ +package com.example.section1.Daemon; + +import java.util.ArrayDeque; +import java.util.Deque; + +/** + * Created by Loon on 2014/4/23. + */ +public class Main { + + public static void main(String[] args) { + Deque deque = new ArrayDeque(); + WriterTask writer = new WriterTask(deque); + for (int i = 0; i < 3; i++) { + Thread thread = new Thread(writer); + thread.start(); + } + CleanerTask cleaner = new CleanerTask(deque); + cleaner.start(); + } +} diff --git a/Concurrency/src/main/java/com/example/section1/Daemon/WriterTask.java b/Concurrency/src/main/java/com/example/section1/Daemon/WriterTask.java index e0d3528..31a5d8d 100644 --- a/Concurrency/src/main/java/com/example/section1/Daemon/WriterTask.java +++ b/Concurrency/src/main/java/com/example/section1/Daemon/WriterTask.java @@ -1,6 +1,8 @@ package com.example.section1.Daemon; +import java.util.Date; import java.util.Deque; +import java.util.concurrent.TimeUnit; /** * Created by Loon on 2014/4/23. @@ -15,6 +17,16 @@ public WriterTask(Deque deque) { @Override public void run() { - + for (int i = 1; i < 100; i++) { + Event event = new Event(); + event.setDate(new Date()); + event.setEvent(String.format("The thread %s has generated an event", Thread.currentThread().getId())); + deque.addFirst(event); + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } } } diff --git a/LearnJava.iws b/LearnJava.iws index 9571694..cd792ba 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -2,15 +2,11 @@ - - - - - + + + - - - + @@ -107,81 +103,89 @@ - - + + - - + + + - - + + - - - + + + + + - - + + - - - + + + + + + - - + + - - - + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + @@ -223,12 +227,15 @@ @@ -433,6 +440,94 @@ - - + + - - + - - + - - + - - + - - - - - - + + + + + - - - - - + + + + + @@ -813,7 +902,11 @@ 1398220304119 1398220304119 - @@ -851,7 +944,7 @@ - + @@ -876,14 +969,14 @@ - + - + @@ -940,29 +1033,6 @@ - - - - - - - - - - - - - - - - - - - - - - - @@ -1290,17 +1360,6 @@ - - - - - - - - - - - @@ -1319,20 +1378,63 @@ - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 95ddca67763c25f8d98ba4ba4b7233fa1da39ced Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 13:30:47 +0800 Subject: [PATCH 021/136] =?UTF-8?q?=E5=8F=8C=E5=90=91=E9=98=9F=E5=88=97?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/example/deque/Deque1.java | 6 +- LearnJava.iws | 268 +++--------------- references.txt | 4 + 3 files changed, 47 insertions(+), 231 deletions(-) create mode 100644 references.txt diff --git a/Collection/src/main/java/com/example/deque/Deque1.java b/Collection/src/main/java/com/example/deque/Deque1.java index 387be7b..3cfbf7c 100644 --- a/Collection/src/main/java/com/example/deque/Deque1.java +++ b/Collection/src/main/java/com/example/deque/Deque1.java @@ -4,6 +4,8 @@ /** * Created by Loon on 2014/4/23. + * 双向队列 + * references:http://www.stanford.edu/group/coursework/docsTech/jgl/api/com.objectspace.jgl.examples.DequeExamples.html */ public class Deque1 { @@ -29,10 +31,10 @@ public static void main(String[] args) { System.out.println("Demonstrate access"); -// System.out.println("deque.at( 0 ) = " + deque.at(0)); System.out.println("deque.getFirst() = " + deque.getFirst()); + System.out.println("find if element exists in deque " + deque.contains("ape")); + System.out.println("find element in deque " + deque.contains("ape1")); System.out.println("deque.element() = " + deque.element()); -// System.out.println("deque.at( 2 ) = " + deque.at(2)); System.out.println("deque.getLast() = " + deque.getLast()); System.out.println(); diff --git a/LearnJava.iws b/LearnJava.iws index cd792ba..d620ca2 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -2,11 +2,9 @@ - - - + + - @@ -72,16 +70,6 @@ - - - - - - - - - - @@ -117,8 +105,8 @@ - - + + @@ -133,7 +121,7 @@ - + @@ -142,15 +130,25 @@ - - + + - + + + + + + + + + + + @@ -235,6 +233,7 @@ @@ -334,200 +333,6 @@ @@ -953,7 +762,7 @@ - + @@ -1033,13 +842,6 @@ - - - - - - - @@ -1394,8 +1196,8 @@ - - + + @@ -1420,17 +1222,25 @@ - + + + + + + + + + - - + + - + diff --git a/references.txt b/references.txt new file mode 100644 index 0000000..9e1ba53 --- /dev/null +++ b/references.txt @@ -0,0 +1,4 @@ +http://www.cs.duke.edu/csl/docs/jgl/api/ +http://ifeve.com/java-7-concurrency-cookbook/ +http://www.programcreek.com/ +http://blog.csdn.net/column/details/multithreading.html \ No newline at end of file From 5a2beb0812f3db6d36939e86325460655fdf68fd Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 13:31:58 +0800 Subject: [PATCH 022/136] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8F=82=E8=80=83?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LearnJava.iws | 81 +++++++++++++++++++++++++-------------------------- README.md | 6 ++++ 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/LearnJava.iws b/LearnJava.iws index d620ca2..e3868b0 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,10 +1,9 @@ - - - + + @@ -70,24 +69,6 @@ - - - - - - - - - - - - - - - - - - @@ -127,11 +108,11 @@ - + - - + + @@ -139,11 +120,21 @@ + + + + + + + + + + - + @@ -235,6 +226,7 @@ @@ -715,7 +707,11 @@ 1398230206753 1398230206753 - @@ -827,7 +823,8 @@ - - - - - - - - - @@ -1227,21 +1216,29 @@ + + + + + + + + + + - + - + - - - - - + + + diff --git a/README.md b/README.md index 594fa02..ef0b582 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ LearnJava ========= Nio + +references: +http://www.cs.duke.edu/csl/docs/jgl/api/ +http://ifeve.com/java-7-concurrency-cookbook/ +http://www.programcreek.com/ +http://blog.csdn.net/column/details/multithreading.html From 23af4dc826ae35a27564a620a9315803edd1c625 Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 13:33:29 +0800 Subject: [PATCH 023/136] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8F=82=E8=80=83?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LearnJava.iws | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/LearnJava.iws b/LearnJava.iws index e3868b0..35c359c 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,7 +1,7 @@ - + @@ -123,8 +123,8 @@ - - + + @@ -711,7 +711,11 @@ 1398231047332 1398231047332 - @@ -824,7 +828,8 @@ - - - + + From bc2d3123510b4ab8c1d335d3e7f089f90ae96d44 Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 13:34:28 +0800 Subject: [PATCH 024/136] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8F=82=E8=80=83?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LearnJava.iws | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/LearnJava.iws b/LearnJava.iws index 35c359c..8c531c8 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -123,8 +123,8 @@ - - + + @@ -715,7 +715,11 @@ 1398231118785 1398231118785 - @@ -1241,8 +1245,8 @@ - - + + From fc7e643a456c7dbb3b0c7c8d0610db40d716b9b4 Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 13:43:47 +0800 Subject: [PATCH 025/136] =?UTF-8?q?=E6=8E=92=E9=99=A4=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.gitignore b/.gitignore index b9d6bd9..9b0c12b 100644 --- a/.gitignore +++ b/.gitignore @@ -213,3 +213,12 @@ pip-log.txt #Mr Developer .mr.developer.cfg + + +################# +## IntelliJ IDEA +################# + +*.ipr +*.iml +*.iws From 97fe15d468c3434948322f1a404d6121fffc11e9 Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 14:07:08 +0800 Subject: [PATCH 026/136] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/example/deque/Deque1.java | 2 +- .../section1/Interrupt/TestInterrupt2.java | 1 + .../section1/Interrupt/TestInterrupt3.java | 1 + .../example/singleton/AmericaPresident.java | 19 + .../example/singleton/SingletonExample.java | 20 + LearnJava.iws | 615 +++++++++++++----- 6 files changed, 502 insertions(+), 156 deletions(-) create mode 100644 DesignPatterns/src/main/java/com/example/singleton/AmericaPresident.java create mode 100644 DesignPatterns/src/main/java/com/example/singleton/SingletonExample.java diff --git a/Collection/src/main/java/com/example/deque/Deque1.java b/Collection/src/main/java/com/example/deque/Deque1.java index 3cfbf7c..df83aba 100644 --- a/Collection/src/main/java/com/example/deque/Deque1.java +++ b/Collection/src/main/java/com/example/deque/Deque1.java @@ -5,7 +5,7 @@ /** * Created by Loon on 2014/4/23. * 双向队列 - * references:http://www.stanford.edu/group/coursework/docsTech/jgl/api/com.objectspace.jgl.examples.DequeExamples.html + * @link http://www.stanford.edu/group/coursework/docsTech/jgl/api/com.objectspace.jgl.examples.DequeExamples.html */ public class Deque1 { diff --git a/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt2.java b/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt2.java index 9c83b72..1b3e782 100644 --- a/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt2.java +++ b/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt2.java @@ -4,6 +4,7 @@ /** * Created by Loon on 2014/4/23. + * @link http://blog.csdn.net/vernonzheng/article/details/8249577 */ public class TestInterrupt2 { public static void main(String[] args) { diff --git a/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt3.java b/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt3.java index 4dea7f6..e567007 100644 --- a/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt3.java +++ b/Concurrency/src/main/java/com/example/section1/Interrupt/TestInterrupt3.java @@ -2,6 +2,7 @@ /** * Created by Loon on 2014/4/23. + * @link http://blog.csdn.net/vernonzheng/article/details/8249577 */ public class TestInterrupt3 { public static void main(String[] args) throws Exception { diff --git a/DesignPatterns/src/main/java/com/example/singleton/AmericaPresident.java b/DesignPatterns/src/main/java/com/example/singleton/AmericaPresident.java new file mode 100644 index 0000000..89986e3 --- /dev/null +++ b/DesignPatterns/src/main/java/com/example/singleton/AmericaPresident.java @@ -0,0 +1,19 @@ +package com.example.singleton; + +/** + * Created by Loon on 2014/4/23. + * + * @link http://www.programcreek.com/2011/07/java-design-pattern-singleton/ + */ +public class AmericaPresident { + private static AmericaPresident thePresident; + + private AmericaPresident() { + } + + public static AmericaPresident getPresident() { + if (thePresident == null) + thePresident = new AmericaPresident(); + return thePresident; + } +} diff --git a/DesignPatterns/src/main/java/com/example/singleton/SingletonExample.java b/DesignPatterns/src/main/java/com/example/singleton/SingletonExample.java new file mode 100644 index 0000000..f29ceaf --- /dev/null +++ b/DesignPatterns/src/main/java/com/example/singleton/SingletonExample.java @@ -0,0 +1,20 @@ +package com.example.singleton; + +/** + * Created by Loon on 2014/4/23. + * + * @link http://www.cnblogs.com/zuoxiaolong/p/3263085.html + */ +public class SingletonExample { + + private SingletonExample() { + } + + public static SingletonExample getInstance() { + return SingletonInstance.instance; + } + + private static class SingletonInstance { + static SingletonExample instance = new SingletonExample(); + } +} diff --git a/LearnJava.iws b/LearnJava.iws index 8c531c8..3188d4d 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,9 +1,13 @@ - + + + + - + + @@ -69,113 +73,107 @@ - - + + - - + + - - - + - - + + - - - - - + + + - - + + - - - - - - + + + - - + + - - - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - + + + + + + @@ -219,14 +217,18 @@ @@ -237,6 +239,40 @@ + + + + + + + + + + + + + + + + + + + + + + @@ -754,16 +1040,16 @@ - + - + - + @@ -779,13 +1065,13 @@ - + - + - + @@ -807,6 +1093,9 @@ + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -905,6 +1160,7 @@ + @@ -912,6 +1168,7 @@ + @@ -933,6 +1190,7 @@ + @@ -958,13 +1216,6 @@ - - - - - - - @@ -983,6 +1234,7 @@ + @@ -1018,6 +1270,7 @@ + @@ -1059,13 +1312,6 @@ - - - - - - - @@ -1168,85 +1414,144 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - + + - + - - + + - + - - - - - + + + - + - - - - - - - + + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - + - + - - + + - + - - + + From 281f2c41c6d77824765cc126a4588ab85f22427b Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 14:56:11 +0800 Subject: [PATCH 027/136] =?UTF-8?q?=E4=BF=AE=E6=94=B9maven=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Collection/Collection.iml | 7 +- Collection/pom.xml | 16 + Concurrency/pom.xml | 23 +- DesignPatterns/DesignPatterns.iml | 7 +- DesignPatterns/pom.xml | 16 + Enum/Enum.iml | 7 +- Enum/pom.xml | 16 + Guava/pom.xml | 28 +- LearnJava.ipr | 12 + LearnJava.iws | 683 +++++++++++------------------- Reflection/Reflection.iml | 7 +- Reflection/pom.xml | 17 + SpringSecurity/SpringSecurity.iml | 3 + SpringSecurity/pom.xml | 11 +- Util/pom.xml | 17 + pom.xml | 40 ++ 16 files changed, 422 insertions(+), 488 deletions(-) create mode 100644 Collection/pom.xml create mode 100644 DesignPatterns/pom.xml create mode 100644 Enum/pom.xml create mode 100644 Reflection/pom.xml create mode 100644 Util/pom.xml create mode 100644 pom.xml diff --git a/Collection/Collection.iml b/Collection/Collection.iml index 638896b..3db36a4 100644 --- a/Collection/Collection.iml +++ b/Collection/Collection.iml @@ -1,16 +1,19 @@ - + - + + + + diff --git a/Collection/pom.xml b/Collection/pom.xml new file mode 100644 index 0000000..439ac89 --- /dev/null +++ b/Collection/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + + LearnJava + LearnJava + 1.0-SNAPSHOT + + + Collection + 1.0-SNAPSHOT + + \ No newline at end of file diff --git a/Concurrency/pom.xml b/Concurrency/pom.xml index d225d1a..20dcd38 100644 --- a/Concurrency/pom.xml +++ b/Concurrency/pom.xml @@ -4,24 +4,13 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - Concurrency + + LearnJava + LearnJava + 1.0-SNAPSHOT + + Concurrency 1.0-SNAPSHOT - - - com.google.guava - guava - 16.0.1 - - - - junit - junit - 4.11 - test - - - - \ No newline at end of file diff --git a/DesignPatterns/DesignPatterns.iml b/DesignPatterns/DesignPatterns.iml index 638896b..3db36a4 100644 --- a/DesignPatterns/DesignPatterns.iml +++ b/DesignPatterns/DesignPatterns.iml @@ -1,16 +1,19 @@ - + - + + + + diff --git a/DesignPatterns/pom.xml b/DesignPatterns/pom.xml new file mode 100644 index 0000000..1f5562b --- /dev/null +++ b/DesignPatterns/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + + LearnJava + LearnJava + 1.0-SNAPSHOT + + + DesignPatterns + 1.0-SNAPSHOT + + \ No newline at end of file diff --git a/Enum/Enum.iml b/Enum/Enum.iml index 638896b..3db36a4 100644 --- a/Enum/Enum.iml +++ b/Enum/Enum.iml @@ -1,16 +1,19 @@ - + - + + + + diff --git a/Enum/pom.xml b/Enum/pom.xml new file mode 100644 index 0000000..7c25618 --- /dev/null +++ b/Enum/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + + LearnJava + LearnJava + 1.0-SNAPSHOT + + + Enum + 1.0-SNAPSHOT + + \ No newline at end of file diff --git a/Guava/pom.xml b/Guava/pom.xml index 7ef80cf..a2c1c82 100644 --- a/Guava/pom.xml +++ b/Guava/pom.xml @@ -1,29 +1,17 @@ - 4.0.0 - Guava + + LearnJava + LearnJava + 1.0-SNAPSHOT + + Guava 1.0-SNAPSHOT - - - com.google.guava - guava - 16.0.1 - - - - junit - junit - 4.11 - test - - - - - \ No newline at end of file diff --git a/LearnJava.ipr b/LearnJava.ipr index ba087ca..ca3a71d 100644 --- a/LearnJava.ipr +++ b/LearnJava.ipr @@ -22,10 +22,15 @@ + + + + + @@ -45,6 +50,12 @@ @@ -176,6 +187,7 @@ + diff --git a/LearnJava.iws b/LearnJava.iws index 3188d4d..328acea 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,13 +1,23 @@ - - - - + + + + + + + + + + + - - + + + + + @@ -73,107 +83,102 @@ - - + + - - - - - + + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - - - - - + + + @@ -194,9 +199,7 @@ @@ -247,30 +258,6 @@ diff --git a/Reflection/Reflection.iml b/Reflection/Reflection.iml index 638896b..3db36a4 100644 --- a/Reflection/Reflection.iml +++ b/Reflection/Reflection.iml @@ -1,16 +1,19 @@ - + - + + + + diff --git a/Reflection/pom.xml b/Reflection/pom.xml new file mode 100644 index 0000000..17887fd --- /dev/null +++ b/Reflection/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + + LearnJava + LearnJava + 1.0-SNAPSHOT + + + Reflection + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/SpringSecurity/SpringSecurity.iml b/SpringSecurity/SpringSecurity.iml index 2ec90b9..20a8a6d 100644 --- a/SpringSecurity/SpringSecurity.iml +++ b/SpringSecurity/SpringSecurity.iml @@ -25,6 +25,9 @@ + + + diff --git a/SpringSecurity/pom.xml b/SpringSecurity/pom.xml index 53d409e..b7bf873 100644 --- a/SpringSecurity/pom.xml +++ b/SpringSecurity/pom.xml @@ -1,10 +1,15 @@ - 4.0.0 - SpringSecurity + + LearnJava + LearnJava + 1.0-SNAPSHOT + + SpringSecurity 1.0-SNAPSHOT diff --git a/Util/pom.xml b/Util/pom.xml new file mode 100644 index 0000000..6e7abe0 --- /dev/null +++ b/Util/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + + LearnJava + LearnJava + 1.0-SNAPSHOT + + + Util + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..fd05186 --- /dev/null +++ b/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + LearnJava + LearnJava + 1.0-SNAPSHOT + pom + + + Collection + Concurrency + DesignPatterns + Enum + Guava + NIO + Reflection + SpringSecurity + Util + + + + + com.google.guava + guava + 16.0.1 + + + + junit + junit + 4.11 + test + + + + + \ No newline at end of file From b3c40b0a4ac236f15a5d47d8d7176af9bb1309fd Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 14:59:51 +0800 Subject: [PATCH 028/136] =?UTF-8?q?=E4=BF=AE=E6=94=B9maven=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Util/Util.iml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Util/Util.iml b/Util/Util.iml index 638896b..3db36a4 100644 --- a/Util/Util.iml +++ b/Util/Util.iml @@ -1,16 +1,19 @@ - + - + + + + From 61c94ed0f1917a499825ef0dedeae1dc03afad4e Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 15:02:46 +0800 Subject: [PATCH 029/136] =?UTF-8?q?=E4=BF=AE=E6=94=B9maven=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LearnJava.iws | 105 +++++++++++++++++--------------------------------- pom.xml | 4 ++ 2 files changed, 40 insertions(+), 69 deletions(-) diff --git a/LearnJava.iws b/LearnJava.iws index 328acea..1d30c4c 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,23 +1,9 @@ - - - - - - - - - - - + - - - - - + @@ -96,8 +82,18 @@ - - + + + + + + + + + + + + @@ -173,16 +169,6 @@ - - - - - - - - - - @@ -223,7 +209,6 @@ @@ -359,34 +345,6 @@ @@ -916,7 +882,8 @@ - - - - - - - - - @@ -1348,10 +1307,18 @@ + + + + + + + + - - + + diff --git a/pom.xml b/pom.xml index fd05186..7bf2722 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,10 @@ Util + + UTF-8 + + com.google.guava From 0e95172882b549ca74da87a9918565150405ad6e Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 15:58:01 +0800 Subject: [PATCH 030/136] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=AE=88=E6=8A=A4?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/example/section1/Daemon/WriterTask.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Concurrency/src/main/java/com/example/section1/Daemon/WriterTask.java b/Concurrency/src/main/java/com/example/section1/Daemon/WriterTask.java index 31a5d8d..3bbfa34 100644 --- a/Concurrency/src/main/java/com/example/section1/Daemon/WriterTask.java +++ b/Concurrency/src/main/java/com/example/section1/Daemon/WriterTask.java @@ -17,10 +17,10 @@ public WriterTask(Deque deque) { @Override public void run() { - for (int i = 1; i < 100; i++) { + for (int i = 1; i < 20; i++) { Event event = new Event(); event.setDate(new Date()); - event.setEvent(String.format("The thread %s has generated an event", Thread.currentThread().getId())); + event.setEvent(String.format("The thread %s has generated an event", Thread.currentThread().getId())); deque.addFirst(event); try { TimeUnit.SECONDS.sleep(1); From 36cab44f42903c11cd1d45b7d0ccf78bc2ddfc47 Mon Sep 17 00:00:00 2001 From: Loon Date: Wed, 23 Apr 2014 16:00:13 +0800 Subject: [PATCH 031/136] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=AE=88=E6=8A=A4?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/section1/Daemon/Main.java | 2 + LearnJava.iws | 367 ++++++++++-------- 2 files changed, 217 insertions(+), 152 deletions(-) diff --git a/Concurrency/src/main/java/com/example/section1/Daemon/Main.java b/Concurrency/src/main/java/com/example/section1/Daemon/Main.java index 720e116..f867ece 100644 --- a/Concurrency/src/main/java/com/example/section1/Daemon/Main.java +++ b/Concurrency/src/main/java/com/example/section1/Daemon/Main.java @@ -5,6 +5,8 @@ /** * Created by Loon on 2014/4/23. + * 当守护线程是程序里唯一在运行的线程时,JVM会结束守护线程并终止程序。 + * @link http://ifeve.com/thread-management-8/ */ public class Main { diff --git a/LearnJava.iws b/LearnJava.iws index 1d30c4c..4200634 100644 --- a/LearnJava.iws +++ b/LearnJava.iws @@ -1,9 +1,9 @@ - + - + @@ -69,101 +69,43 @@ - - + + - - + + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - + + @@ -205,9 +147,7 @@ @@ -345,6 +287,132 @@