Skip to content

Commit 0964e4e

Browse files
committed
update 设计模式
1 parent eb4cccc commit 0964e4e

File tree

11 files changed

+396
-1
lines changed

11 files changed

+396
-1
lines changed

src/main/java/com/chen/algorithm/sort/BubbleSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.junit.Test;
44

55
/**
6-
* 冒泡排序算法
6+
* 冒泡排序算法 O(n平方)
77
* Created by Chen Weijie on 2017/8/17.
88
*/
99
public class BubbleSort {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.chen.api.util.base;
2+
3+
/**
4+
* created by zhubaobao on 2017-09-11
5+
*/
6+
public class StringTest {
7+
8+
static final int MAX = 100000;
9+
static final String[] arr = new String[MAX];
10+
11+
public static void main(String[] args) {
12+
13+
//1. 测试i++等,可通过反编译验证(http://blog.csdn.net/u013256816/article/details/50778902)
14+
// plus();
15+
16+
//2.1 测试String类的intern()方法 (http://blog.csdn.net/seu_calvin/article/details/52291082)
17+
// String str2="SEUCalvin";//新加的一行代码,其余不变
18+
// String str1=new String("SEU")+new String("Calvin");
19+
// System.out.println(str1.intern() == str2);
20+
// System.out.println(str1=="SEUCalvin");
21+
// System.out.println("你好打分三大件佛奥qdqwadsdas1214@#$%^&*(".hashCode());
22+
23+
//2.2 验证String类的intern方法的特点:节省空间,但是耗费时间
24+
// 为长度为10的Integer数组随机赋值
25+
// Integer[] sample = new Integer[10];
26+
// Random random = new Random(1000);
27+
// for (int i = 0; i < sample.length; i++) {
28+
// sample[i] = random.nextInt();
29+
// }
30+
// //记录程序开始时间
31+
// long t = System.currentTimeMillis();
32+
// //使用/不使用intern方法为10万个String赋值,值来自于Integer数组的10个数
33+
// for (int i = 0; i < MAX; i++) {
34+
// arr[i] = new String(String.valueOf(sample[i % sample.length]));
35+
//// arr[i] = new String(String.valueOf(sample[i %
36+
// sample.length])).intern();
37+
// }
38+
// System.out.println((System.currentTimeMillis() - t) + "ms");
39+
// System.gc();
40+
41+
//2.3 测试intern方法的内存结构
42+
// String s = new String("1");
43+
// s.intern();
44+
// String s2 = "1";
45+
// System.out.println(s == s2);
46+
47+
// String s3 = new String("1") + new String("1");
48+
// s3.intern();
49+
// String s4 = "11";
50+
// System.out.println(s3 == s4);
51+
52+
//3. 测试StringBuilder和StringBuffer中的reverse方法
53+
// StringBuffer 和 StringBuilder
54+
// 类有reverse()方法,它们其实是调用的父类AbstractStringBuilder的reverse()方法
55+
StringBuffer sBuffer = new StringBuffer("abcD890dfg");
56+
System.out.println(sBuffer.reverse());
57+
58+
// 4. 如何计算指定字符在字符串里出现的次数
59+
// 同样是使用Apache Commons Lang的StringUtils,如下:
60+
//int n = StringUtils.countMatches("aaaabbbb", "a");
61+
//Systemm.out.println(n);
62+
63+
//判断String 创建了几个对象,用反编译验证
64+
String s = "a" + new String("123");// 创建了几个对象
65+
66+
}
67+
68+
static void plus() {
69+
int i = 0;
70+
int y = i++ + ++i;
71+
System.out.println(String.valueOf(y));
72+
}
73+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
一、javac.exe、 java.exe、 java虚拟机三者之间的区别与联系:
2+
3+
JDK中
4+
javac:Java编译器,将Java源代码换成字节代;
5+
java:Java解释器,直接从类文件执行Java应用程序代码;
6+
7+
先编译 *.java文件――――>*.class文件
8+
运行 *.class ――加载――> JVM
9+
jvm加载二进制文件
10+
11+
javac编译后得到的class文件是二进制指令,但不是机器指令,而是java虚拟机可识别的指令。这样class文件就有了可移植行。你可以把class文件拿到windows、linux或者solaris等不同的系统上去,在jvm上执行。
12+
13+
java是启动jvm,jvm负责对class文件的内容进行处理,将字节码文件解释或者编译为机器指令,执行。
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.chen.api.util.lock;
2+
3+
/**
4+
* @Author chenweijie
5+
* @Date 2017/9/17 1:39
6+
*/
7+
public class SyncThread implements Runnable {
8+
9+
private Object obj1;
10+
private Object obj2;
11+
12+
public SyncThread(Object o1, Object o2) {
13+
this.obj1 = o1;
14+
this.obj2 = o2;
15+
}
16+
17+
18+
@Override
19+
public void run() {
20+
21+
String name = Thread.currentThread().getName();
22+
System.out.println(name + " acquiring lock on " + obj1);
23+
synchronized (obj1) {
24+
System.out.println(name + " acquiring lock on " + obj1);
25+
work();
26+
System.out.println(name + " acquiring lock on " + obj2);
27+
synchronized (obj2) {
28+
System.out.println(name + " acquiring lock on " + obj2);
29+
work();
30+
}
31+
System.out.println(name + " acquiring lock on " + obj2);
32+
33+
}
34+
System.out.println(name + " released lock on " + obj1);
35+
System.out.println(name + " finished execution.");
36+
}
37+
38+
private void work() {
39+
40+
try {
41+
Thread.sleep(1000);
42+
} catch (InterruptedException e) {
43+
e.printStackTrace();
44+
}
45+
46+
47+
}
48+
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.chen.api.util.lock;
2+
3+
/**
4+
* 死锁范例
5+
*
6+
* @Author chenweijie
7+
* @Date 2017/9/17 1:33
8+
*/
9+
public class ThreadDeadlock {
10+
11+
12+
public static void main(String[] args) throws InterruptedException {
13+
14+
Object obj1 = new Object();
15+
Object obj2 = new Object();
16+
Object obj3 = new Object();
17+
18+
Thread t1 = new Thread(new SyncThread(obj1, obj2), "t1");
19+
Thread t2 = new Thread(new SyncThread(obj1, obj3), "t2");
20+
Thread t3 = new Thread(new SyncThread(obj2, obj3), "t3");
21+
t1.start();
22+
Thread.sleep(5000);
23+
t2.start();
24+
Thread.sleep(5000);
25+
t3.start();
26+
}
27+
28+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.chen.api.util.syncthread;
2+
3+
/**
4+
* 线程同步的运用
5+
*
6+
* @Author chenweijie
7+
* @Date 2017/9/17 2:56
8+
*/
9+
public class SynchronizedThread {
10+
11+
12+
class Bank {
13+
private int account = 100;
14+
public int getAccount() {
15+
return account;
16+
}
17+
18+
/**
19+
* 用同步方法实现
20+
*
21+
* @param money
22+
*/
23+
public synchronized void save(int money) {
24+
account += money;
25+
}
26+
27+
/**
28+
* 用同步代码块实现
29+
*
30+
* @param money
31+
*/
32+
public void save1(int money) {
33+
synchronized (this) {
34+
account += money;
35+
}
36+
}
37+
}
38+
39+
class NewThread implements Runnable {
40+
private Bank bank;
41+
42+
public NewThread(Bank bank) {
43+
this.bank = bank;
44+
}
45+
46+
@Override
47+
public void run() {
48+
for (int i = 0; i < 10; i++) {
49+
bank.save1(10);
50+
// bank.save(10);
51+
System.out.println(i + "账户余额为:" + bank.getAccount());
52+
}
53+
}
54+
55+
}
56+
57+
/**
58+
* 建立线程,调用内部类
59+
*/
60+
public void useThread() {
61+
Bank bank = new Bank();
62+
NewThread new_thread = new NewThread(bank);
63+
System.out.println("线程1");
64+
Thread thread1 = new Thread(new_thread);
65+
thread1.start();
66+
System.out.println("线程2");
67+
Thread thread2 = new Thread(new_thread);
68+
thread2.start();
69+
}
70+
71+
public static void main(String[] args) {
72+
SynchronizedThread st = new SynchronizedThread();
73+
st.useThread();
74+
}
75+
76+
77+
78+
}
79+
80+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.chen.api.util.thread;
2+
3+
/**
4+
* @Author chenweijie
5+
* @Date 2017/9/18 23:45
6+
*/
7+
public class IncDecThread {
8+
9+
private int j = 10;
10+
11+
12+
/*
13+
* 题目:用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1
14+
* 两个问题:
15+
* 1、线程同步--synchronized
16+
* 2、线程之间如何共享同一个j变量--内部类
17+
*/
18+
public static void main(String[] args) {
19+
20+
IncDecThread incDecThread = new IncDecThread();
21+
22+
Inc inc = incDecThread.new Inc();
23+
Dec dec = incDecThread.new Dec();
24+
25+
for (int i = 0; i < 2; i++) {
26+
Thread thread = new Thread(inc);
27+
thread.start();
28+
thread = new Thread(dec);
29+
thread.start();
30+
}
31+
32+
}
33+
34+
35+
public synchronized void inc() {
36+
j++;
37+
System.out.println(Thread.currentThread().getName() + "-inc:" + j);
38+
}
39+
40+
public synchronized void dec() {
41+
j--;
42+
System.out.println(Thread.currentThread().getName() + "-dec:" + j);
43+
}
44+
45+
46+
class Inc implements Runnable {
47+
48+
@Override
49+
public void run() {
50+
for (int i = 0; i < 20; i++) {
51+
inc();
52+
}
53+
}
54+
}
55+
56+
class Dec implements Runnable {
57+
58+
@Override
59+
public void run() {
60+
for (int i = 0; i < 20; i++) {
61+
dec();
62+
}
63+
}
64+
}
65+
66+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.chen.api.util.thread;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.concurrent.locks.ReentrantReadWriteLock;
6+
7+
/**
8+
* @Author chenweijie
9+
* @Date 2017/9/20 23:19
10+
*/
11+
public class ReaderWriterList<T> {
12+
13+
14+
private ArrayList<T> lockedList;
15+
16+
private ReentrantReadWriteLock lock =new ReentrantReadWriteLock(true);
17+
18+
public ReaderWriterList(int size,T initialValue){
19+
lockedList =new ArrayList<T>(Collections.nCopies(size,initialValue));
20+
}
21+
22+
23+
24+
25+
26+
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.chen.api.util.thread.threadPool;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
/**
7+
* @Author chenweijie
8+
* @Date 2017/9/17 23:34
9+
*/
10+
public class ThreadPool {
11+
12+
13+
public static void main(String[] args) {
14+
15+
ExecutorService service =Executors.newSingleThreadExecutor();
16+
17+
ExecutorService service2 = Executors.newCachedThreadPool();
18+
19+
ExecutorService service3 =Executors.newFixedThreadPool(10);
20+
21+
// new ThreadPoolExecutor(1,1,1,1,1);
22+
}
23+
24+
}

src/main/java/com/chen/designPattern/singleton/Singleton1.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.chen.designPattern.singleton;
22

3+
import java.util.concurrent.atomic.AtomicInteger;
4+
35
/**
46
* 懒汉式单例模式
57
* Created by chenwj3 on 2017/2/28.
@@ -14,6 +16,8 @@ private Singleton1() {
1416

1517
public static Singleton1 getSingleton1() {
1618

19+
AtomicInteger integer =new AtomicInteger();
20+
1721
if (singleton1 == null) {
1822
singleton1 = new Singleton1();
1923
}

0 commit comments

Comments
 (0)