diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..554ea79 --- /dev/null +++ b/.gitignore @@ -0,0 +1,72 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +.idea/ +src/main/main.iml +src/test/test.iml \ No newline at end of file diff --git a/README.md b/README.md index 49941a0..88bb506 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # JavaTutorial内容说明 该Java教程是学习Java过程中对Java知识点进行梳理。每一个包中的代码都有一个ReadMe来说明该包代码的作用以及知识点。 +src包里面是Java的源码,test包里面是针对源码的测试 -![Alt text](images/JavaTutorial目录.jpg) +![Alt text](images/JavaTutorial目录.png) ## ThreadTutorial内容说明 在源码路径下`cn.byhieg.threadtutorial`中放置的是Java多线程的代码和测试例子,在每一个章的包下,都有一个ReadMe来说明该章的知识点。 @@ -67,9 +68,6 @@ MapTutorial中的知识点有 - TreeMap的使用 - LinkedHashMap的使用 - - - ## AnnotationTutorial内容说明 在源码路径下`cn.byhieg.annotationtutorial`中放置的是Java注解类的代码和测试例子。在每一个包里面,都有一个ReadMe来说明该章的知识点。 @@ -79,7 +77,22 @@ MapTutorial中的知识点有 在源码路径下`cn.byhieg.iotutorial`中放置的是Java io类的代码和测试例子。在每一个包里面,都有一个ReadMe来说明该章的知识点。 +## DesignPattern内容说明 + +在源码路径下`cn.byhieg.designpatterntutorial`中放置的是Java 设计模式的代码和测试的例子。目前有 + +- 代理模式 +- 单例模式 +- builder模式 +## algorithmtutorial内容说明 +在源码路径下`cn.byhieg.algorithmtutorial`中放置的是Java实现的一些算法,内容如下: +- 二叉搜索树 +- 二叉树的常见的算法 +- 查找算法, +- 图的算法(DFS,BFS,Dijkstra) +- 单链表的算法 +- 排序算法。 diff --git "a/images/JavaTutorial\347\233\256\345\275\225.jpg" "b/images/JavaTutorial\347\233\256\345\275\225.jpg" deleted file mode 100644 index a4603a6..0000000 Binary files "a/images/JavaTutorial\347\233\256\345\275\225.jpg" and /dev/null differ diff --git "a/images/JavaTutorial\347\233\256\345\275\225.png" "b/images/JavaTutorial\347\233\256\345\275\225.png" new file mode 100644 index 0000000..f7c0041 Binary files /dev/null and "b/images/JavaTutorial\347\233\256\345\275\225.png" differ diff --git a/javaturorial.iml b/javaturorial.iml new file mode 100644 index 0000000..4e3316b --- /dev/null +++ b/javaturorial.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/cn/byhieg/algorithmtutorial/RedBlackTree.java b/src/main/java/cn/byhieg/algorithmtutorial/RedBlackTree.java new file mode 100644 index 0000000..c895f7b --- /dev/null +++ b/src/main/java/cn/byhieg/algorithmtutorial/RedBlackTree.java @@ -0,0 +1,326 @@ +package cn.byhieg.algorithmtutorial; + +/** + * Created by byhieg on 2017/6/24. + * Mail to byhieg@gmail.com + */ + + +/** + * 红黑树,一种通过红黑两种节点来维持二叉搜索树的一种树 + * 这样树比原先的BST而言,不会出现最坏的查找情况是o(N)的情况 + * 但是对于插入和删除的节点而言,就需要调整树的平衡,也就是维持红黑树的定义 + *

+ * 红黑树的定义如下: + * 1. 任何节点要不是黑色要不是红色 + * 2. 根节点是黑色节点 + * 3. 红节点的两个子节点都是黑色节点 + * 4. 空节点是黑色节点 + * 5. 任何一个节点下面遍历其子孙的叶子节点,经过的黑色节点的个数必须相等。 + *

+ * 红黑树也是通过第5点进行维持平衡的,而为了维持平衡,需要对树进行调整,即进行左旋,右旋。 + + */ +public class RedBlackTree { + + Node root; + + public RedBlackTree() { + } + + public RedBlackTree(int value) { + root = new Node(value); + } + + public Node find(int value) { + if (root == null) { + throw new RuntimeException("树是空的"); + } + + Node currentNode = root; + while (currentNode != null && currentNode.getValue() != value) { + if (currentNode.getValue() < value) { + currentNode = currentNode.getLeft(); + } else { + currentNode = currentNode.getRight(); + } + } + + return currentNode; + } + + + public void insertNode(int value) { + Node node = new Node(value); + insertNode(node); + + } + + /** + * 插入节点 + * 该方法首先找到要插入的位置,然后设置插入的节点为红色节点 + * 然后因为可能会破坏平衡,因此需要进行平衡调整 + * + * @param node + */ + public void insertNode(Node node) { + int cmp; + Node y = null; + Node x = this.root; + + while (x != null) { + y = x; + cmp = node.getValue() - x.getValue(); + if (cmp < 0) { + x = x.left; + } else { + x = x.right; + } + } + + node.parent = y; + if (y != null) { + cmp = node.getValue() - y.getValue(); + if (cmp < 0) { + y.left = node; + } else { + y.right = node; + } + } else { + this.root = node; + } + + node.isRed = true; + insertFixUp(node); + + } + + /** + * 插入修复: 新插入的节点是红色节点,插入修复操作如果遇到父节点的颜色为黑色则修复结束 + * 也就是说只有在父节点为红色节点的时候才需要插入修复操作 + * 插入修复操作分为3种情况, + * 1. 叔叔节点也为红色节点 + * 2. 叔叔节点为空,且祖父节点,父节点与新节点在一个斜线上 + * 3. 叔叔节点为空,且祖父节点,父节点与新节点不在一个斜线上 + *

+ *

+ * 解决办法:对于第一种,只需要将祖父节点与父节点以及叔叔节点的颜色对调即可。 + * 即原祖父节点是黑色,现在变成红色,父节点与叔叔节点都变成黑色。 + * 对于第二种,我们将新插入的节点为C,父节点为B,祖父节点为A. + * 如果BC都是左节点,要现将A右旋,然后调整B与A的颜色,即B变成黑色,A变成红色 + * 如果BC都是右节点,要现将A左旋,然后调整B与A的颜色,即B变成黑色,A变成红色 + * 对于第三种,我们将新插入的节点为C,父节点为B,祖父节点为A. + * 如果C为右节点,B为左节点,要先将B左旋,然后就变成第二种的情况 + * 如果C为左节点,B为右节点,要先B右旋,然后就变成第二种的情况 + * + * @param node + */ + private void insertFixUp(Node node) { + Node parent, grandParent, uncle; + while ((parent = parentOf(node)) != null && parent.isRed()) { + grandParent = parentOf(node); + //如果父节点是祖父节点的左孩子 + if (parent == grandParent.left) { + uncle = grandParent.right; + //第一种情况 + if ((uncle != null) && uncle.isRed()) { + uncle.makeBlack(); + parent.makeBlack(); + grandParent.makeRed(); + node = grandParent; + continue; + } + //将第三种情况变成第二种情况 + if (parent.right == node) { + Node tmp; + rotateLeft(parent); + tmp = parent; + parent = node; + node = tmp; + } + parent.makeBlack(); + grandParent.makeRed(); + rotateRight(grandParent); + } else { + uncle = grandParent.left; + if ((uncle != null) && uncle.isRed()) { + uncle.makeBlack(); + parent.makeBlack(); + grandParent.makeRed(); + node = grandParent; + continue; + } + + if (parent.left == node) { + Node tmp; + rotateRight(parent); + tmp = parent; + parent = node; + node = tmp; + } + + parent.makeBlack(); + grandParent.makeRed(); + rotateLeft(grandParent); + + } + + } + + root.makeBlack(); + } + + + /** + * 对红黑树的节点(y)进行右旋转 + * + * 右旋示意图(对节点y进行左旋): + * py py + * / / + * y x + * / \ --(右旋)-. / \ # + * x ry lx y + * / \ / \ # + * lx rx rx ry + * + * @param y 待旋转的节点 + */ + private void rotateRight(Node y) { + + Node x = y.left; + + // 将 “x的右孩子” 设为 “y的左孩子”; + // 如果"x的右孩子"不为空的话,将 “y” 设为 “x的右孩子的父亲” + y.left = x.right; + if (x.right != null) + x.right.parent = y; + + // 将 “y的父亲” 设为 “x的父亲” + x.parent = y.parent; + + if (y.parent == null) { + this.root = x; // 如果 “y的父亲” 是空节点,则将x设为根节点 + } else { + if (y == y.parent.right) + y.parent.right = x; // 如果 y是它父节点的右孩子,则将x设为“y的父节点的右孩子” + else + y.parent.left = x; // (y是它父节点的左孩子) 将x设为“x的父节点的左孩子” + } + + // 将 “y” 设为 “x的右孩子” + x.right = y; + + // 将 “y的父节点” 设为 “x” + y.parent = x; + } + + /** + * + * 对红黑树的节点(x)进行左旋转 + * + * 左旋示意图(对节点x进行左旋): + * px px + * / / + * x y + * / \ --(左旋)-. / \ # + * lx y x ry + * / \ / \ + * ly ry lx ly + * + * @param x 待旋转的节点 + */ + private void rotateLeft(Node x) { + Node y = x.getRight(); + x.right = y.left; + if (y.left != null) { + y.left.parent = x; + } + y.parent = x.parent; + if (x.parent == null) { + root = y; + }else{ + if (x.parent.left == x) { + x.parent.left = y; + }else{ + x.parent.right = y; + } + } + y.left = x; + x.parent = y; + } + + private Node parentOf(Node node) { + return node != null ? node.parent : null; + } + + + static class Node { + private int value; + private Node parent; + private boolean isRed; + private Node left; + private Node right; + + public Node() { + + } + + public Node(int value) { + this.value = value; + + } + + public Node(int value, boolean isRed) { + this.value = value; + this.isRed = isRed; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public Node getParent() { + return parent; + } + + public void setParent(Node parent) { + this.parent = parent; + } + + public boolean isRed() { + return isRed; + } + + public boolean isBlack() { + return !isRed(); + } + + public Node getLeft() { + return left; + } + + public void setLeft(Node left) { + this.left = left; + } + + public Node getRight() { + return right; + } + + public void setRight(Node right) { + this.right = right; + } + + public void makeRed() { + isRed = true; + } + + public void makeBlack() { + isRed = false; + } + } +} diff --git a/src/main/java/cn/byhieg/algorithmtutorial/SingleLinkList.java b/src/main/java/cn/byhieg/algorithmtutorial/SingleLinkList.java index 28b29ae..1de9643 100644 --- a/src/main/java/cn/byhieg/algorithmtutorial/SingleLinkList.java +++ b/src/main/java/cn/byhieg/algorithmtutorial/SingleLinkList.java @@ -43,7 +43,10 @@ public Node insertFromHead(int data) { return head; } - + /** + * 反转链表的非递归实现 + * @return + */ public Node reverseLinkList() { if (head == null) { return head; @@ -63,6 +66,23 @@ public Node reverseLinkList() { return reverseHead; } + + /** + * 反转链表的递归实现 + * @return + */ + public Node reverseLinkList(Node head){ + if (head == null || head.next == null) { + return head; + } + Node newNode = reverseLinkList(head.next); + head.next.next = head; + head.next = null; + return newNode; + } + + + /** * 打印链表 */ diff --git a/src/main/java/cn/byhieg/algorithmtutorial/Sort.java b/src/main/java/cn/byhieg/algorithmtutorial/Sort.java index 6229a1f..6794e42 100644 --- a/src/main/java/cn/byhieg/algorithmtutorial/Sort.java +++ b/src/main/java/cn/byhieg/algorithmtutorial/Sort.java @@ -128,6 +128,59 @@ public void bubbleSort2(int[] nums) { } } + /** + * 归并排序,将数组一分为二,对于每一子数组继续进行上述步骤,直到子数组只有1个元素,那么自然是有序的。 + * 然后不断合并两个数组,直到合并到整个数组。 + * 时间复杂度o(NlgN) + * 空间复杂度o(N) + * @param nums + */ + public void mergeSort(int[] nums) { + int length = nums.length; + int low = 0; + int high = length - 1; + realSort(nums, low, high); + } + + /** + * 归并排序真正的sort函数 + * @param nums 待排序的数组 + * @param low 最低位 + * @param high 最高位 + */ + private void realSort(int[] nums, int low, int high) { + int mid = (low + high) / 2; + if (low < high) { + realSort(nums, low, mid); + realSort(nums, mid + 1, high); + realMerge(nums, low, mid, high); + } + } + + private void realMerge(int[] nums, int low, int mid, int high) { + int[] tmpNums = new int[high - low + 1]; + int leftPoint = low; + int rightPoint = mid + 1; + int index = 0; + + while (leftPoint <= mid && rightPoint <= high) { + if (nums[leftPoint] < nums[rightPoint]) { + tmpNums[index++] = nums[leftPoint++]; + }else{ + tmpNums[index++] = nums[rightPoint++]; + } + } + + while (leftPoint <= mid) { + tmpNums[index++] = nums[leftPoint++]; + } + while (rightPoint <= high) { + tmpNums[index++] = nums[rightPoint++]; + } + + System.arraycopy(tmpNums, 0, nums, low, tmpNums.length); + } + /** * 快速排序,选定一个切分元素,每一轮排序后,都保证切分元素之前的元素都小于切分元素,切分元素之后的元素都大于切分元素 * 时间复杂度o(NlgN) @@ -263,4 +316,5 @@ public void sink(int [] nums, int i,int n) { } + } diff --git a/src/main/java/cn/byhieg/bitoperatetutorial/BitOperate.java b/src/main/java/cn/byhieg/bitoperatetutorial/BitOperate.java new file mode 100644 index 0000000..680408b --- /dev/null +++ b/src/main/java/cn/byhieg/bitoperatetutorial/BitOperate.java @@ -0,0 +1,14 @@ +package cn.byhieg.bitoperatetutorial; + +/** + * Created by byhieg on 2017/6/27. + * Mail to byhieg@gmail.com + */ +public class BitOperate { + + + public String getRightestOne(int n){ + int res = n & (~n + 1); + return Integer.toBinaryString(res); + } +} diff --git a/src/main/java/cn/byhieg/designpatterntutorial/builder/Person.java b/src/main/java/cn/byhieg/designpatterntutorial/builder/Person.java new file mode 100644 index 0000000..91077fd --- /dev/null +++ b/src/main/java/cn/byhieg/designpatterntutorial/builder/Person.java @@ -0,0 +1,73 @@ +package cn.byhieg.designpatterntutorial.builder; + +/** + * Created by shiqifeng on 2017/5/7. + * Mail byhieg@gmail.com + */ +public class Person { + + private int age; + private String name; + private int height; + private int weight; + + public int getAge() { + return age; + } + + public String getName() { + return name; + } + + public int getHeight() { + return height; + } + + public int getWeight() { + return weight; + } + + public Person(Builder builder) { + age = builder.age; + name = builder.name; + height = builder.height; + weight = builder.weight; + } + + @Override + public String toString() { + return "age = " + age + " name = " + name + " height = " + height + " weight = " + weight; + } + + public static class Builder{ + + private int age; + private String name; + private int height; + private int weight; + + public Builder setAge(int age) { + this.age = age; + return this; + } + + public Builder setName(String name) { + this.name = name; + return this; + } + + public Builder setHeight(int height) { + this.height = height; + return this; + } + + public Builder setWeight(int weight) { + this.weight = weight; + return this; + } + + public Person build() { + return new Person(this); + } + } +} diff --git a/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialog.java b/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialog.java new file mode 100644 index 0000000..0d90062 --- /dev/null +++ b/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialog.java @@ -0,0 +1,78 @@ +package cn.byhieg.designpatterntutorial.builder; + +/** + * Created by shiqifeng on 2017/5/7. + * Mail byhieg@gmail.com + */ +public class SimpleDialog { + + + public SimpleDialogController controller; + + public SimpleDialog(){ + controller = new SimpleDialogController(); + } + + public void setIcon(String icon) { + controller.setIcon(icon); + } + + public void setTitle(String title) { + controller.setTitle(title); + } + + public void setMessage(String message) { + controller.setMessage(message); + } + + public void setPositiveButton(String positiveButton) { + controller.setPositiveButton(positiveButton); + } + + public void setNegativeButton(String negativeButton) { + controller.setNegativeButton(negativeButton); + } + + + public static class Builder{ + SimpleDialogController.DialogParams P; + + public Builder(){ + P = new SimpleDialogController.DialogParams(); + } + + public Builder setIcon(String icon){ + P.icon = icon; + return this; + } + + public Builder setTitle(String title) { + P.title = title; + return this; + } + + public Builder setMessage(String message) { + P.message = message; + return this; + } + + public Builder setPositiveButton(String positiveButton) { + P.positiveButton = positiveButton; + return this; + } + + public Builder setNegativeButton(String negativeButton) { + P.negativeButton = negativeButton; + return this; + } + + public SimpleDialog create(){ + SimpleDialog dialog = new SimpleDialog(); + P.apply(dialog.controller); + System.out.println(" ICON = " + P.icon + " MESSAGE = " + P.message + " positiveButton = " + P.positiveButton + " negativeButton" + P.negativeButton); + return dialog; + } + } + + +} diff --git a/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialogController.java b/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialogController.java new file mode 100644 index 0000000..4d41b74 --- /dev/null +++ b/src/main/java/cn/byhieg/designpatterntutorial/builder/SimpleDialogController.java @@ -0,0 +1,81 @@ +package cn.byhieg.designpatterntutorial.builder; + +/** + * Created by shiqifeng on 2017/5/7. + * Mail byhieg@gmail.com + */ +public class SimpleDialogController { + + private String icon; + private String title; + private String message; + private String positiveButton; + private String negativeButton; + + public void setIcon(String icon) { + this.icon = icon; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setMessage(String message) { + this.message = message; + } + + public void setPositiveButton(String positiveButton) { + this.positiveButton = positiveButton; + } + + public void setNegativeButton(String negativeButton) { + this.negativeButton = negativeButton; + } + + public String getIcon() { + return icon; + } + + public String getTitle() { + return title; + } + + public String getMessage() { + return message; + } + + public String getPositiveButton() { + return positiveButton; + } + + public String getNegativeButton() { + return negativeButton; + } + + public static class DialogParams{ + public String icon; + public String title; + public String message; + public String positiveButton; + public String negativeButton; + + public void apply(SimpleDialogController controller) { + if (icon != null) { + controller.setIcon(icon); + } + if (title != null) { + controller.setTitle(title); + } + if (message != null) { + controller.setMessage(message); + } + if (positiveButton != null) { + controller.setPositiveButton(positiveButton); + } + if (negativeButton != null) { + controller.setNegativeButton(negativeButton); + } + } + } + +} diff --git a/src/main/java/cn/byhieg/niotutorial/NioTest.java b/src/main/java/cn/byhieg/niotutorial/NioTest.java deleted file mode 100644 index 7ee0a08..0000000 --- a/src/main/java/cn/byhieg/niotutorial/NioTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package cn.byhieg.niotutorial; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; - -/** - * Created by shiqifeng on 2017/4/10. - * Mail byhieg@gmail.com - */ -public class NioTest { - - public static void main(String[] args) throws IOException { - String s = File.separator; - RandomAccessFile aFile = new RandomAccessFile("D:" + s + "read_file.txt","rw"); - FileChannel inChannel = aFile.getChannel(); - - ByteBuffer buf = ByteBuffer.allocate(48); - int bytesRead = inChannel.read(buf); - while (bytesRead != -1) { - buf.flip(); - while (buf.hasRemaining()) { - System.out.print((char) buf.get()); - } - - buf.clear(); - bytesRead = inChannel.read(buf); - } - - aFile.close(); - } -} diff --git a/src/main/java/cn/byhieg/niotutorial/read_file.txt b/src/main/java/cn/byhieg/niotutorial/read_file.txt deleted file mode 100644 index 2cb3b74..0000000 --- a/src/main/java/cn/byhieg/niotutorial/read_file.txt +++ /dev/null @@ -1,5 +0,0 @@ -I don't know what I do now is right, those are wrong, and when I finally die then I know these. -So what I can do now is to try to do everything well, and then wait to die . -Sometimes I can be very happy to talk to everyone, can be very presumptuous, but no one knows, -it is but very deliberately camouflage, camouflage; I can make him very happy very happy, -but couldn't find the source of happiness, just giggle. \ No newline at end of file diff --git a/src/main/java/cn/byhieg/threadtutorial/concurrent/atom/AtomFactory.java b/src/main/java/cn/byhieg/threadtutorial/concurrent/atom/AtomFactory.java new file mode 100644 index 0000000..f5da772 --- /dev/null +++ b/src/main/java/cn/byhieg/threadtutorial/concurrent/atom/AtomFactory.java @@ -0,0 +1,36 @@ +package cn.byhieg.threadtutorial.concurrent.atom; + +import java.util.concurrent.atomic.*; + +/** + * Created by shiqifeng on 2017/5/5. + * Mail byhieg@gmail.com + */ +public class AtomFactory { + + private static final AtomFactory atomFactory = new AtomFactory(); + + private AtomFactory(){ + + } + + public static AtomFactory getInstance(){ + return atomFactory; + } + + public AtomicInteger createAtomInt(int a){ + return new AtomicInteger(a); + } + + public AtomicIntegerArray createAtomArray(int[] a) { + return new AtomicIntegerArray(a); + } + + public AtomicReference createAtomReference(MyObject object){ + return new AtomicReference<>(); + } + + public AtomicIntegerFieldUpdater createAtomIntegerUpdate(String fieldName) { + return AtomicIntegerFieldUpdater.newUpdater(MyObject.class, fieldName); + } +} diff --git a/src/main/java/cn/byhieg/threadtutorial/concurrent/atom/MyObject.java b/src/main/java/cn/byhieg/threadtutorial/concurrent/atom/MyObject.java new file mode 100644 index 0000000..69ebbb6 --- /dev/null +++ b/src/main/java/cn/byhieg/threadtutorial/concurrent/atom/MyObject.java @@ -0,0 +1,12 @@ +package cn.byhieg.threadtutorial.concurrent.atom; + +/** + * Created by shiqifeng on 2017/5/5. + * Mail byhieg@gmail.com + */ +public class MyObject { + + public String name = "byhieg"; + public int age = 24; + public volatile int id = 1; +} diff --git a/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/ArrayBlock.java b/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/ArrayBlock.java index b31b674..3687b31 100644 --- a/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/ArrayBlock.java +++ b/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/ArrayBlock.java @@ -1,7 +1,10 @@ package cn.byhieg.threadtutorial.concurrent.blocking; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.BlockingQueue; +import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; + +import javax.sound.midi.SoundbankResource; +import java.lang.management.LockInfo; +import java.util.concurrent.*; /** * Created by byhieg on 17/5/3. @@ -11,8 +14,18 @@ public class ArrayBlock { private BlockingQueue blockingQueue; - public ArrayBlock(){ - blockingQueue = new ArrayBlockingQueue(1024); + public ArrayBlock(int index) { + switch (index) { + case 0: + blockingQueue = new ArrayBlockingQueue(3); + break; + case 1: + blockingQueue = new LinkedBlockingQueue<>(); + break; + case 2: + blockingQueue = new SynchronousQueue<>(); + break; + } } public BlockingQueue getBlockingQueue() { diff --git a/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/Costumer.java b/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/Costumer.java index d69c647..f88b45f 100644 --- a/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/Costumer.java +++ b/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/Costumer.java @@ -20,7 +20,7 @@ public void run() { super.run(); while (true) { try { - Thread.sleep(2000); + Thread.sleep(5000); String str = blockingQueue.take(); System.out.println(getName() + " 取出数据 " + str); } catch (InterruptedException e) { diff --git a/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/Producer.java b/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/Producer.java index 5298546..a2d12eb 100644 --- a/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/Producer.java +++ b/src/main/java/cn/byhieg/threadtutorial/concurrent/blocking/Producer.java @@ -14,9 +14,8 @@ public void run() { super.run(); for (int i = 0 ; i < 5;i++) { try { - System.out.println(getName() + " 生产数据"); blockingQueue.put(i + ""); - Thread.sleep(1000); + System.out.println(getName() + " 生产数据"); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/src/test/java/cn/byhieg/algorithmtutorialtest/SingleLinkListTest.java b/src/test/java/cn/byhieg/algorithmtutorialtest/SingleLinkListTest.java index 862359a..e7f2145 100644 --- a/src/test/java/cn/byhieg/algorithmtutorialtest/SingleLinkListTest.java +++ b/src/test/java/cn/byhieg/algorithmtutorialtest/SingleLinkListTest.java @@ -43,6 +43,7 @@ public void testInsertFromHead() throws Exception { // System.out.println("头插入"); } public void testReverseLinkList() throws Exception { + System.out.println(); linkList.insertFromHead(1); linkList.insertFromHead(2); linkList.insertFromHead(3); @@ -51,6 +52,17 @@ public void testReverseLinkList() throws Exception { linkList.insertFromHead(6); linkList.printLinkList(linkList.reverseLinkList()); } + + public void testReverseLinkList2() throws Exception{ + System.out.println("递归反转链表"); + linkList.insertFromHead(1); + linkList.insertFromHead(2); + linkList.insertFromHead(3); + linkList.insertFromHead(4); + linkList.insertFromHead(5); + linkList.insertFromHead(6); + linkList.printLinkList(linkList.reverseLinkList(linkList.getHead())); + } public void testGetHead() throws Exception { } diff --git a/src/test/java/cn/byhieg/algorithmtutorialtest/SortTest.java b/src/test/java/cn/byhieg/algorithmtutorialtest/SortTest.java index 08c1d90..046daca 100644 --- a/src/test/java/cn/byhieg/algorithmtutorialtest/SortTest.java +++ b/src/test/java/cn/byhieg/algorithmtutorialtest/SortTest.java @@ -41,8 +41,13 @@ public void tearDown() throws Exception { // new Sort().quickSort(nums); // } - public void testHeapSort() throws Exception { - new Sort().heapSort(nums); +// public void testHeapSort() throws Exception { +// new Sort().heapSort(nums); +// } + + public void testMergeSort() throws Exception { + new Sort().mergeSort(nums); + } } \ No newline at end of file diff --git a/src/test/java/cn/byhieg/bitoperatetutorialtest/BitOperateTest.java b/src/test/java/cn/byhieg/bitoperatetutorialtest/BitOperateTest.java new file mode 100644 index 0000000..962eccd --- /dev/null +++ b/src/test/java/cn/byhieg/bitoperatetutorialtest/BitOperateTest.java @@ -0,0 +1,23 @@ +package cn.byhieg.bitoperatetutorialtest; + +import cn.byhieg.bitoperatetutorial.BitOperate; +import junit.framework.TestCase; + +/** + * Created by byhieg on 2017/6/27. + * Mail to byhieg@gmail.com + */ +public class BitOperateTest extends TestCase { + + BitOperate bitOperate; + @Override + protected void setUp() throws Exception { + super.setUp(); + bitOperate = new BitOperate(); + } + + public void testGetRightestOne() throws Exception { + assertEquals(bitOperate.getRightestOne(Integer.valueOf("100100",2)),"100"); + } + +} \ No newline at end of file diff --git a/src/test/java/cn/byhieg/designpatterntutorialtest/BuilderTest.java b/src/test/java/cn/byhieg/designpatterntutorialtest/BuilderTest.java new file mode 100644 index 0000000..2a67a00 --- /dev/null +++ b/src/test/java/cn/byhieg/designpatterntutorialtest/BuilderTest.java @@ -0,0 +1,16 @@ +package cn.byhieg.designpatterntutorialtest; + +import cn.byhieg.designpatterntutorial.builder.Person; +import junit.framework.TestCase; + +/** + * Created by shiqifeng on 2017/5/7. + * Mail byhieg@gmail.com + */ +public class BuilderTest extends TestCase { + public void testBuild() throws Exception { + Person person = new Person.Builder().setAge(24).setHeight(178).setName("byhieg").setWeight(80).build(); + System.out.println(person.toString()); + } + +} \ No newline at end of file diff --git a/src/test/java/cn/byhieg/designpatterntutorialtest/SimpleDialogTest.java b/src/test/java/cn/byhieg/designpatterntutorialtest/SimpleDialogTest.java new file mode 100644 index 0000000..002115a --- /dev/null +++ b/src/test/java/cn/byhieg/designpatterntutorialtest/SimpleDialogTest.java @@ -0,0 +1,18 @@ +package cn.byhieg.designpatterntutorialtest; + +import cn.byhieg.designpatterntutorial.builder.SimpleDialog; +import junit.framework.TestCase; + +/** + * Created by shiqifeng on 2017/5/7. + * Mail byhieg@gmail.com + */ +public class SimpleDialogTest extends TestCase { + + public void testBuilder() throws Exception { + SimpleDialog dialog = new SimpleDialog.Builder().setIcon("图标").setMessage("这是Dialog").setPositiveButton("确认").setNegativeButton("否定").create(); + + + } + +} \ No newline at end of file diff --git a/src/test/java/cn/byhieg/threadtutorialtest/concurrenttest/atomtest/AtomFactoryTest.java b/src/test/java/cn/byhieg/threadtutorialtest/concurrenttest/atomtest/AtomFactoryTest.java new file mode 100644 index 0000000..e8b706a --- /dev/null +++ b/src/test/java/cn/byhieg/threadtutorialtest/concurrenttest/atomtest/AtomFactoryTest.java @@ -0,0 +1,82 @@ +package cn.byhieg.threadtutorialtest.concurrenttest.atomtest; + +import cn.byhieg.threadtutorial.concurrent.atom.AtomFactory; + +import cn.byhieg.threadtutorial.concurrent.atom.MyObject; +import junit.framework.TestCase; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicIntegerArray; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; +import java.util.concurrent.atomic.AtomicReference; + +/** + * Created by shiqifeng on 2017/5/5. + * Mail byhieg@gmail.com + */ +public class AtomFactoryTest extends TestCase { + AtomicInteger integer; + AtomicIntegerArray array; + AtomicReference reference; + AtomicIntegerFieldUpdater updater; + + public void setUp() throws Exception { + super.setUp(); + integer = AtomFactory.getInstance().createAtomInt(1); + } + + public void testAtomInt() throws Exception { + System.out.println("int原子类"); + new Thread(()->{ + for (int i = 0; i < 10; i++) { + integer.getAndIncrement(); + System.out.println(getName() + " " + integer.get()); + } + }).start(); + + new Thread(()->{ + for (int i = 0; i < 10; i++) { + integer.getAndIncrement(); + System.out.println(getName() + " " + integer.get()); + } + }).start(); + + Thread.sleep(1000); + } + + public void testAtomArray() throws Exception { + System.out.println("原子类数组"); + int [] value = new int[]{1,2,3,4}; + array = AtomFactory.getInstance().createAtomArray(value); + array.getAndSet(1,10); + System.out.println(array.get(1)); + System.out.println(value[1]); + } + + public void testAtomRef()throws Exception { + System.out.println("原子类"); + MyObject object = new MyObject(); + reference = AtomFactory.getInstance().createAtomReference(object); + reference.set(object); + MyObject newObject = new MyObject(); + newObject.name = "xiaoli"; + reference.compareAndSet(object, newObject); + System.out.println(reference.get().name); + } + + + public void testUpdater() throws Exception { + System.out.println("原子类更新字段"); + updater = AtomFactory.getInstance().createAtomIntegerUpdate("id"); + MyObject object = new MyObject(); + System.out.println(updater.getAndIncrement(object)); + System.out.println(updater.get(object)); + } + + + public void tearDown() throws Exception { + + } + + +} \ No newline at end of file diff --git a/src/test/java/cn/byhieg/threadtutorialtest/concurrenttest/blockingtest/ArrayBlockTest.java b/src/test/java/cn/byhieg/threadtutorialtest/concurrenttest/blockingtest/ArrayBlockTest.java index 8c695b2..c48566c 100644 --- a/src/test/java/cn/byhieg/threadtutorialtest/concurrenttest/blockingtest/ArrayBlockTest.java +++ b/src/test/java/cn/byhieg/threadtutorialtest/concurrenttest/blockingtest/ArrayBlockTest.java @@ -15,7 +15,7 @@ public class ArrayBlockTest extends TestCase { ArrayBlock block; public void setUp() throws Exception { super.setUp(); - block = new ArrayBlock(); + block = new ArrayBlock(2); } public void tearDown() throws Exception {