From a93cd9b486f6cad667c8c1ecb394fee7c1f294ac Mon Sep 17 00:00:00 2001 From: Shanghe Chen Date: Tue, 27 Sep 2016 19:54:54 +0800 Subject: [PATCH 1/4] add .gitignore --- .gitignore | 226 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de7b19a --- /dev/null +++ b/.gitignore @@ -0,0 +1,226 @@ + +## Ignore Python compiled files +*.pyc + +## Ignore vim temporary files +*.swp +*~ + +## Ignore JetBrains temporary files +.idea/ +target/ +project/ +out/ + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ + +# Visual Studio 2015 cache/options directory +.vs/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +## TODO: Comment the next line if you want to checkin your +## web deploy settings but do note that will include unencrypted +## passwords +#*.pubxml + +*.publishproj + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# LightSwitch generated files +GeneratedArtifacts/ +_Pvt_Extensions/ +ModelManifest.xml From e50ab1ea9a30b8959e765f2af8b3d0e2345d8047 Mon Sep 17 00:00:00 2001 From: Shanghe Chen Date: Tue, 27 Sep 2016 19:55:10 +0800 Subject: [PATCH 2/4] Core java iml --- CoreJava.iml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 CoreJava.iml diff --git a/CoreJava.iml b/CoreJava.iml new file mode 100644 index 0000000..15179ff --- /dev/null +++ b/CoreJava.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file From 1e7cd074663aa320234d99b39b83947e1bc1b888 Mon Sep 17 00:00:00 2001 From: Shanghe Chen Date: Mon, 31 Oct 2016 10:47:44 +0800 Subject: [PATCH 3/4] learning --- CoreJava.iml | 3 + .../blockingQueue/BlockingQueueTest.java | 309 ++++++++++-------- CoreJava/v1ch14/future/FutureTest.java | 244 +++++++------- CoreJava/v1ch14/sec14_2/Test1.java | 38 +++ 4 files changed, 330 insertions(+), 264 deletions(-) create mode 100644 CoreJava/v1ch14/sec14_2/Test1.java diff --git a/CoreJava.iml b/CoreJava.iml index 15179ff..01f1442 100644 --- a/CoreJava.iml +++ b/CoreJava.iml @@ -7,6 +7,9 @@ + + + diff --git a/CoreJava/v1ch14/blockingQueue/BlockingQueueTest.java b/CoreJava/v1ch14/blockingQueue/BlockingQueueTest.java index bd9f9a8..b58935d 100644 --- a/CoreJava/v1ch14/blockingQueue/BlockingQueueTest.java +++ b/CoreJava/v1ch14/blockingQueue/BlockingQueueTest.java @@ -1,142 +1,167 @@ -package blockingQueue; - -import java.io.*; -import java.util.*; -import java.util.concurrent.*; - -/** - * @version 1.01 2012-01-26 - * @author Cay Horstmann - */ -public class BlockingQueueTest -{ - public static void main(String[] args) - { - Scanner in = new Scanner(System.in); - System.out.print("Enter base directory (e.g. /usr/local/jdk1.6.0/src): "); - String directory = in.nextLine(); - System.out.print("Enter keyword (e.g. volatile): "); - String keyword = in.nextLine(); - - final int FILE_QUEUE_SIZE = 10; - final int SEARCH_THREADS = 100; - - BlockingQueue queue = new ArrayBlockingQueue<>(FILE_QUEUE_SIZE); - - FileEnumerationTask enumerator = new FileEnumerationTask(queue, new File(directory)); - new Thread(enumerator).start(); - for (int i = 1; i <= SEARCH_THREADS; i++) - new Thread(new SearchTask(queue, keyword)).start(); - } -} - -/** - * This task enumerates all files in a directory and its subdirectories. - */ -class FileEnumerationTask implements Runnable -{ - public static File DUMMY = new File(""); - private BlockingQueue queue; - private File startingDirectory; - - /** - * Constructs a FileEnumerationTask. - * @param queue the blocking queue to which the enumerated files are added - * @param startingDirectory the directory in which to start the enumeration - */ - public FileEnumerationTask(BlockingQueue queue, File startingDirectory) - { - this.queue = queue; - this.startingDirectory = startingDirectory; - } - - public void run() - { - try - { - enumerate(startingDirectory); - queue.put(DUMMY); - } - catch (InterruptedException e) - { - } - } - - /** - * Recursively enumerates all files in a given directory and its subdirectories. - * @param directory the directory in which to start - */ - public void enumerate(File directory) throws InterruptedException - { - File[] files = directory.listFiles(); - for (File file : files) - { - if (file.isDirectory()) enumerate(file); - else queue.put(file); - } - } -} - -/** - * This task searches files for a given keyword. - */ -class SearchTask implements Runnable -{ - private BlockingQueue queue; - private String keyword; - - /** - * Constructs a SearchTask. - * @param queue the queue from which to take files - * @param keyword the keyword to look for - */ - public SearchTask(BlockingQueue queue, String keyword) - { - this.queue = queue; - this.keyword = keyword; - } - - public void run() - { - try - { - boolean done = false; - while (!done) - { - File file = queue.take(); - if (file == FileEnumerationTask.DUMMY) - { - queue.put(file); - done = true; - } - else search(file); - } - } - catch (IOException e) - { - e.printStackTrace(); - } - catch (InterruptedException e) - { - } - } - - /** - * Searches a file for a given keyword and prints all matching lines. - * @param file the file to search - */ - public void search(File file) throws IOException - { - try (Scanner in = new Scanner(file)) - { - int lineNumber = 0; - while (in.hasNextLine()) - { - lineNumber++; - String line = in.nextLine(); - if (line.contains(keyword)) - System.out.printf("%s:%d:%s%n", file.getPath(), lineNumber, line); - } - } - } -} +package blockingQueue; + +import java.io.*; +import java.util.*; +import java.util.concurrent.*; + +/** + * @version 1.01 2012-01-26 + * @author Cay Horstmann + */ +public class BlockingQueueTest +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + System.out.print("Enter base directory (e.g. /usr/local/jdk1.6.0/src): "); + String directory = in.nextLine(); + System.out.print("Enter keyword (e.g. volatile): "); + String keyword = in.nextLine(); + + final int FILE_QUEUE_SIZE = 10; + final int SEARCH_THREADS = 100; + + BlockingQueue queue = new ArrayBlockingQueue<>(FILE_QUEUE_SIZE); + + FileEnumerationTask enumerator = new FileEnumerationTask(queue, new File(directory)); + new Thread(enumerator).start(); + + /* + for (int i = 1; i <= SEARCH_THREADS; i++) + new Thread(new SearchTask(queue, keyword)).start(); + */ + + + boolean done = false; + while (!done) + { + File file = null; + try { + file = queue.take(); + System.out.println(file.toString()); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (file == FileEnumerationTask.DUMMY) + { + try { + queue.put(file); + } catch (InterruptedException e) { + e.printStackTrace(); + } + done = true; + } + } + } +} + +/** + * This task enumerates all files in a directory and its subdirectories. + */ +class FileEnumerationTask implements Runnable +{ + public static File DUMMY = new File(""); + private BlockingQueue queue; + private File startingDirectory; + + /** + * Constructs a FileEnumerationTask. + * @param queue the blocking queue to which the enumerated files are added + * @param startingDirectory the directory in which to start the enumeration + */ + public FileEnumerationTask(BlockingQueue queue, File startingDirectory) + { + this.queue = queue; + this.startingDirectory = startingDirectory; + } + + public void run() + { + try + { + enumerate(startingDirectory); + queue.put(DUMMY); + } + catch (InterruptedException e) + { + } + } + + /** + * Recursively enumerates all files in a given directory and its subdirectories. + * @param directory the directory in which to start + */ + public void enumerate(File directory) throws InterruptedException + { + File[] files = directory.listFiles(); + for (File file : files) + { + if (file.isDirectory()) enumerate(file); + else queue.put(file); + } + } +} + +/** + * This task searches files for a given keyword. + */ +class SearchTask implements Runnable +{ + private BlockingQueue queue; + private String keyword; + + /** + * Constructs a SearchTask. + * @param queue the queue from which to take files + * @param keyword the keyword to look for + */ + public SearchTask(BlockingQueue queue, String keyword) + { + this.queue = queue; + this.keyword = keyword; + } + + public void run() + { + try + { + boolean done = false; + while (!done) + { + File file = queue.take(); + if (file == FileEnumerationTask.DUMMY) + { + queue.put(file); + done = true; + } + else search(file); + } + } + catch (IOException e) + { + e.printStackTrace(); + } + catch (InterruptedException e) + { + } + } + + /** + * Searches a file for a given keyword and prints all matching lines. + * @param file the file to search + */ + public void search(File file) throws IOException + { + try (Scanner in = new Scanner(file)) + { + int lineNumber = 0; + while (in.hasNextLine()) + { + lineNumber++; + String line = in.nextLine(); + if (line.contains(keyword)) + System.out.printf("%s:%d:%s%n", file.getPath(), lineNumber, line); + } + } + } +} diff --git a/CoreJava/v1ch14/future/FutureTest.java b/CoreJava/v1ch14/future/FutureTest.java index e151536..e870cec 100644 --- a/CoreJava/v1ch14/future/FutureTest.java +++ b/CoreJava/v1ch14/future/FutureTest.java @@ -1,122 +1,122 @@ -package future; - -import java.io.*; -import java.util.*; -import java.util.concurrent.*; - -/** - * @version 1.01 2012-01-26 - * @author Cay Horstmann - */ -public class FutureTest -{ - public static void main(String[] args) - { - Scanner in = new Scanner(System.in); - System.out.print("Enter base directory (e.g. /usr/local/jdk5.0/src): "); - String directory = in.nextLine(); - System.out.print("Enter keyword (e.g. volatile): "); - String keyword = in.nextLine(); - - MatchCounter counter = new MatchCounter(new File(directory), keyword); - FutureTask task = new FutureTask<>(counter); - Thread t = new Thread(task); - t.start(); - try - { - System.out.println(task.get() + " matching files."); - } - catch (ExecutionException e) - { - e.printStackTrace(); - } - catch (InterruptedException e) - { - } - } -} - -/** - * This task counts the files in a directory and its subdirectories that contain a given keyword. - */ -class MatchCounter implements Callable -{ - private File directory; - private String keyword; - private int count; - - /** - * Constructs a MatchCounter. - * @param directory the directory in which to start the search - * @param keyword the keyword to look for - */ - public MatchCounter(File directory, String keyword) - { - this.directory = directory; - this.keyword = keyword; - } - - public Integer call() - { - count = 0; - try - { - File[] files = directory.listFiles(); - List> results = new ArrayList<>(); - - for (File file : files) - if (file.isDirectory()) - { - MatchCounter counter = new MatchCounter(file, keyword); - FutureTask task = new FutureTask<>(counter); - results.add(task); - Thread t = new Thread(task); - t.start(); - } - else - { - if (search(file)) count++; - } - - for (Future result : results) - try - { - count += result.get(); - } - catch (ExecutionException e) - { - e.printStackTrace(); - } - } - catch (InterruptedException e) - { - } - return count; - } - - /** - * Searches a file for a given keyword. - * @param file the file to search - * @return true if the keyword is contained in the file - */ - public boolean search(File file) - { - try - { - try (Scanner in = new Scanner(file)) - { - boolean found = false; - while (!found && in.hasNextLine()) - { - String line = in.nextLine(); - if (line.contains(keyword)) found = true; - } - return found; - } - } - catch (IOException e) - { - return false; - } - } -} +package future; + +import java.io.*; +import java.util.*; +import java.util.concurrent.*; + +/** + * @version 1.01 2012-01-26 + * @author Cay Horstmann + */ +public class FutureTest +{ + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + System.out.print("Enter base directory (e.g. /usr/local/jdk5.0/src): "); + String directory = in.nextLine(); + System.out.print("Enter keyword (e.g. volatile): "); + String keyword = in.nextLine(); + + MatchCounter counter = new MatchCounter(new File(directory), keyword); + FutureTask task = new FutureTask<>(counter); + Thread t = new Thread(task); // used as runnable + t.start(); + try + { + System.out.println(task.get() + " matching files."); // used as future + } + catch (ExecutionException e) + { + e.printStackTrace(); + } + catch (InterruptedException e) + { + } + } +} + +/** + * This task counts the files in a directory and its subdirectories that contain a given keyword. + */ +class MatchCounter implements Callable +{ + private File directory; + private String keyword; + private int count; + + /** + * Constructs a MatchCounter. + * @param directory the directory in which to start the search + * @param keyword the keyword to look for + */ + public MatchCounter(File directory, String keyword) + { + this.directory = directory; + this.keyword = keyword; + } + + public Integer call() + { + count = 0; + try + { + File[] files = directory.listFiles(); + List> results = new ArrayList<>(); + + for (File file : files) + if (file.isDirectory()) + { + MatchCounter counter = new MatchCounter(file, keyword); + FutureTask task = new FutureTask<>(counter); + results.add(task); // used as future + Thread t = new Thread(task); // used as runnable + t.start(); + } + else + { + if (search(file)) count++; + } + + for (Future result : results) + try + { + count += result.get(); + } + catch (ExecutionException e) + { + e.printStackTrace(); + } + } + catch (InterruptedException e) + { + } + return count; + } + + /** + * Searches a file for a given keyword. + * @param file the file to search + * @return true if the keyword is contained in the file + */ + public boolean search(File file) + { + try + { + try (Scanner in = new Scanner(file)) + { + boolean found = false; + while (!found && in.hasNextLine()) + { + String line = in.nextLine(); + if (line.contains(keyword)) found = true; + } + return found; + } + } + catch (IOException e) + { + return false; + } + } +} diff --git a/CoreJava/v1ch14/sec14_2/Test1.java b/CoreJava/v1ch14/sec14_2/Test1.java new file mode 100644 index 0000000..b2416f2 --- /dev/null +++ b/CoreJava/v1ch14/sec14_2/Test1.java @@ -0,0 +1,38 @@ +package sec14_2; + +/** + * Created by shanghechen on 2016/10/12. + */ +public class Test1 { + public static void main(String[] args) throws InterruptedException { + // 创建两个线程 + Thread a = new Thread(new MyRunnable("Thread a")); + Thread b = new Thread(new MyRunnable("Thread b")); + System.out.println(a.getState()); + System.out.println(b.getState()); + a.start(); + b.start(); + System.out.println(a.getState()); + System.out.println(b.getState()); + a.join(); + b.join(); + System.out.println(a.getState()); + System.out.println(b.getState()); + System.out.println("never arrive."); + } +} + +class MyRunnable implements Runnable { + + private String name; + public MyRunnable(String name) { + this.name = name; + } + + @Override + public void run() { + while (true){ + + } + } +} From eeb864f49c3e4c60a9a8d45ec8baadd8c47f3b5c Mon Sep 17 00:00:00 2001 From: Shanghe Chen Date: Mon, 14 Nov 2016 13:32:31 +0800 Subject: [PATCH 4/4] add src directories --- CoreJava.iml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CoreJava.iml b/CoreJava.iml index 01f1442..ee2b8aa 100644 --- a/CoreJava.iml +++ b/CoreJava.iml @@ -10,8 +10,12 @@ + + + + \ No newline at end of file