Skip to content

Commit

Permalink
update run future task
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusJiang1306 committed Nov 11, 2020
1 parent 92b11b9 commit 00dc8e8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Week_02/GCAnalysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
在写本分析的时候我发现1g到4g的CMS的新生代分配都是固定的,应该是基于宿主机内存的多少份之一和最低下限设定的,应该是有意控制新生代大小。也就是说CMS对对象的生成速率以及宿主机核心/线程数都比较敏感,若线程数较少或对象生成速度较快的时候不适合CMS和应该选用G1和并行GC
在分析CMS的表现的时候通过和助教的交流发现在jdk14之后,CMS已经被移除,我也纠结还要不要写本次分析,但最终觉得这是我对gc行为的观察的个人见解,有必要拿出来讨论是否理解上有偏差。

##测试方法
## 测试方法
因为手动写参数太麻烦我又写了个简化版的java命令 (在script下的myjar和jarByBatch)(因为没Python环境和不会写Python - -)
13 changes: 13 additions & 0 deletions Week_02/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,17 @@
<version>4.7.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
16 changes: 10 additions & 6 deletions Week_02/src/main/java/MyHttpClient.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.*;
import okhttp3.internal.http.HttpHeaders;

import java.io.IOException;

Expand All @@ -11,13 +9,19 @@ public class MyHttpClient {

public static void main(String[] args) {
MyHttpClient myHttpClient = new MyHttpClient();
String response = myHttpClient.getResponse("http://localhost:8088/api/hello");
String response = myHttpClient.getResponse("http://localhost:8801/header");
System.out.println(response);
}

private String getResponse(String url) {
String responseString = null;
Request request = new Request.Builder().url(url).build();
MediaType header = MediaType.parse("text/plain; charset=utf-8");

Request request = new Request.Builder()
.url(url)
.get()
.header("Connection","close")
.build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
Expand Down
56 changes: 56 additions & 0 deletions Week_04/src/main/java/homework/myHomework/SemaphoreMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package homework.myHomework;

import java.util.concurrent.Semaphore;

public class SemaphoreMethod {

private int sum;
final Semaphore semaphore = new Semaphore(1);

private void setSum() {
try {
semaphore.acquire();

This comment has been minimized.

Copy link
@jinze1107

jinze1107 Nov 15, 2020

可以再简洁些

this.sum = fibo(36);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
semaphore.release();
}

}

private void printResult(long stratTime) {

try {
semaphore.acquire();
// 确保 拿到result 并输出
int result = sum;
System.out.println("异步计算结果为:" + result);
System.out.println("使用时间:"+ (System.currentTimeMillis()-stratTime) + " ms");

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
}

public static void main(String[] args) throws InterruptedException {

long start=System.currentTimeMillis();
// 在这里创建一个线程或线程池,
// 异步执行 下面方法
SemaphoreMethod semaphoreMethod = new SemaphoreMethod();

new Thread(semaphoreMethod::setSum).start();
new Thread(() -> semaphoreMethod.printResult(start)).start();

// 然后退出main线程
}

public static int fibo(int a) {
if ( a < 2)
return 1;
return fibo(a-1) + fibo(a-2);
}
}

1 comment on commit 00dc8e8

@jinze1107
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

作业写法很多,可以参考其他同学的作业看看。继续加油小兄弟。

Please sign in to comment.