Skip to content

Commit fe89f4c

Browse files
committed
【Java】 update
1 parent ff58595 commit fe89f4c

File tree

10 files changed

+287
-16
lines changed

10 files changed

+287
-16
lines changed

.gitignore

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
/Thread/Thraed.iml
22
/Guava/.idea
33
/.idea
4-
# Created by .ignore support plugin (hsz.mobi)
5-
### Java template
64
*.class
7-
8-
# Mobile Tools for Java (J2ME)
9-
.mtj.tmp/
10-
11-
# Package Files #
5+
/OptimizeJava/OptimizeJava.iml
126
/JavaBase/.idea
137
/JavaBase/JavaBase.iml
148
/Thread/target
159
/Thread/.idea
1610
/Thread/Thread.iml
1711

18-
*.jar
19-
*.war
20-
*.ear
21-
22-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23-
hs_err_pid*
24-

OptimizeJava/OptimizeJava.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
</component>
15+
</module>

OptimizeJava/doc/readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
####《Java程序性能优化》读书笔记
2+
一、调优层次
3+
1、设计调优
4+
2、代码调优
5+
3、JVM调优
6+
4、数据库调优
7+
5、操作系统调优
8+
9+
二、

OptimizeJava/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>OptimizeJava</groupId>
8+
<artifactId>OptimizeJava</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.optimize.code.thread.design.future;
2+
3+
import java.util.concurrent.Callable;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.FutureTask;
7+
8+
/**
9+
* ClassName: FutureMode
10+
* Description: 并行设计:Future
11+
* Date: 2016/6/12 21:11
12+
*
13+
* @author SAM SHO
14+
* @version V1.0
15+
*/
16+
public class FutureMode {
17+
18+
19+
public static void main(String[] args) throws Exception {
20+
FutureTask<String> futureTask = new FutureTask<String>(new RealData("a"));
21+
ExecutorService executor = Executors.newFixedThreadPool(1);
22+
executor.submit(futureTask);
23+
System.out.println("请求完毕");
24+
// 这里依然可以做一些额外的数据操作
25+
Thread.sleep(2000);
26+
// 如果此时 call() 方法没有执行完毕,依然会等待
27+
System.out.println("数据 = " + futureTask.get());
28+
}
29+
30+
31+
static class RealData implements Callable<String> {
32+
33+
private String para;
34+
35+
public RealData(String para) {
36+
this.para = para;
37+
}
38+
39+
public String call() throws Exception {
40+
41+
//
42+
StringBuilder sb = new StringBuilder();
43+
for (int i = 0; i < 10; i++) {
44+
sb.append(i);
45+
Thread.sleep(100);
46+
}
47+
return sb.toString();
48+
}
49+
}
50+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.optimize.code.thread.design.mw;
2+
3+
import java.util.Map;
4+
import java.util.Set;
5+
6+
/**
7+
* ClassName: MWMode
8+
* Description: 并行设计:Master-Worker
9+
* Date: 2016/6/12 21:55
10+
*
11+
* @author SAM SHO
12+
* @version V1.0
13+
*/
14+
public class MWMode {
15+
16+
public static void main(String[] args) {
17+
// 使用5个Worker
18+
Master master = new Master(new PlusWorker(), 5);
19+
20+
// 提交100个任务
21+
for (int i = 0; i < 100; i++) {
22+
master.submit(i);
23+
}
24+
// 开始计算
25+
master.execute();
26+
27+
int result = 0; // 保存最终结果
28+
Map<String, Object> resultMap = master.getResultMap();
29+
// 不需要等待所有的Worker 都执行完成,即可开始计算最终结果
30+
while (resultMap.size() > 0 || !master.isComplete()) {
31+
Set<String> keys = resultMap.keySet();
32+
String key = null;
33+
for (String k : keys) {
34+
key = k;
35+
break;
36+
}
37+
38+
Integer i = null;
39+
if (key != null) {
40+
i = (Integer) resultMap.get(key);
41+
}
42+
43+
if (i != null) {
44+
result += i; // 最终结果
45+
}
46+
47+
if (key != null) {
48+
resultMap.remove(key);
49+
}
50+
}
51+
52+
System.out.println("结果 : " + result);
53+
54+
}
55+
56+
/**
57+
* 内部类 ,立方和的应用
58+
*/
59+
static class PlusWorker extends Worker {
60+
@Override
61+
protected Object handle(Object input) {//求立方和
62+
Integer i = (Integer) input;
63+
return i * i * i;
64+
}
65+
}
66+
67+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.optimize.code.thread.design.mw;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Queue;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
import java.util.concurrent.ConcurrentLinkedQueue;
8+
9+
/**
10+
* ClassName: Master
11+
* Description:
12+
* Date: 2016/6/12 21:23
13+
*
14+
* @author SAM SHO
15+
* @version V1.0
16+
*/
17+
public class Master {
18+
19+
// 任务队列
20+
protected Queue<Object> workQueue = new ConcurrentLinkedQueue<Object>();
21+
22+
// worker 进程队列
23+
protected Map<String, Thread> threadMap = new HashMap<String, Thread>();
24+
25+
// 子任务处理处理结果集
26+
protected Map<String, Object> resultMap = new ConcurrentHashMap<String, Object>();
27+
28+
29+
/**
30+
* 是否所有的任务都结束了
31+
*
32+
* @return
33+
*/
34+
public boolean isComplete() {
35+
36+
for (Map.Entry<String, Thread> entry : threadMap.entrySet()) {
37+
if (entry.getValue().getState() != Thread.State.TERMINATED) {
38+
return false;
39+
}
40+
}
41+
return true;
42+
}
43+
44+
// Master 的构造,需要一个 Worker 进行逻辑,和需要的Worker 进行数
45+
public Master(Worker worker, int countWorker) {
46+
worker.setWorkQueue(workQueue);
47+
worker.setResultMap(resultMap);
48+
for (int i = 0; i < countWorker; i++) {
49+
threadMap.put(Integer.toString(i), new Thread(worker, Integer.toString(i)));
50+
}
51+
}
52+
53+
/**
54+
* 提交一个任务
55+
*
56+
* @param job
57+
*/
58+
public void submit(Object job) {
59+
workQueue.add(job);
60+
}
61+
62+
/**
63+
* 开始运行所有 Worker 进程,进行处理
64+
*/
65+
public void execute() {
66+
for (Map.Entry<String, Thread> entry : threadMap.entrySet()) {
67+
entry.getValue().start();
68+
}
69+
}
70+
71+
72+
public Map<String, Object> getResultMap() {
73+
return resultMap;
74+
}
75+
76+
77+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.optimize.code.thread.design.mw;
2+
3+
import java.util.Map;
4+
import java.util.Queue;
5+
6+
/**
7+
* ClassName: Worker
8+
* Description:
9+
* Date: 2016/6/12 21:31
10+
*
11+
* @author SAM SHO
12+
* @version V1.0
13+
*/
14+
public class Worker implements Runnable {
15+
16+
// 任务队列,用于获取子任务
17+
protected Queue<Object> workQueue;
18+
19+
// 子任务处理处理结果集
20+
protected Map<String, Object> resultMap;
21+
22+
public void setWorkQueue(Queue<Object> workQueue) {
23+
this.workQueue = workQueue;
24+
}
25+
26+
public void setResultMap(Map<String, Object> resultMap) {
27+
this.resultMap = resultMap;
28+
}
29+
30+
/**
31+
* 子任务处理逻辑,在子类中实现具体逻辑
32+
*
33+
* @param input
34+
* @return
35+
*/
36+
protected Object handle(Object input) {
37+
return input;
38+
}
39+
40+
public void run() {
41+
while (true) {
42+
// 获取子任务
43+
Object input = workQueue.poll();
44+
if (input == null) {
45+
break;
46+
}
47+
// 处理子任务
48+
Object result = handle(input);
49+
// 将处理结果写入结果集
50+
resultMap.put(Integer.toString(input.hashCode()), result);
51+
}
52+
53+
}
54+
}

Thread/src/main/java/com/thread/future/CallableAndFuture.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public String call() throws Exception {
4545
System.out.println("等待结果");
4646
try {
4747
System.out.println("拿到结果: "+future.get());//拿到结果,拿不到的时候会一直等...
48-
// System.out.println("拿到结果: "+future.get(timeout, unit)get());
48+
// System.out.println("拿到结果: "+design.get(timeout, unit)get());
4949
} catch (InterruptedException e) {
5050
e.printStackTrace();
5151
} catch (ExecutionException e) {

Thread/src/main/java/com/thread/future/example1/main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private static void batch() throws InterruptedException, ExecutionException {
2525
}
2626

2727
private static void sampleSubmit() throws InterruptedException, ExecutionException {
28-
FutureTask<String> future = new FutureTask<String>(new RealData("future"));
28+
FutureTask<String> future = new FutureTask<String>(new RealData("design"));
2929
ExecutorService executorService = Executors.newFixedThreadPool(1);
3030
executorService.submit(future);
3131

0 commit comments

Comments
 (0)