Skip to content

Commit b6932bb

Browse files
committed
java线程和并发学习
1 parent 8e75441 commit b6932bb

20 files changed

+463
-280
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.code.repository;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedList;
5+
import java.util.concurrent.atomic.AtomicBoolean;
6+
7+
public class Test implements Runnable {
8+
9+
private static AtomicBoolean flag = new AtomicBoolean (true);
10+
11+
12+
@Override
13+
public void run() {
14+
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.code.repository.study.bigDecimal;
2+
3+
import java.math.BigDecimal;
4+
5+
/**
6+
* 保留两位小数,
7+
* 使用BigDecimal实现
8+
*/
9+
public class Save2Scale {
10+
public static void main(String[] args) {
11+
Double dd = new Double(12.3456); // double 带小数数字
12+
BigDecimal bd = new BigDecimal(dd); // 转换成BigDecimal
13+
BigDecimal bd2 = bd.setScale(2,BigDecimal.ROUND_HALF_UP);// 设置保留2位小数,同时实现四舍五入(还有其他舍入方式)
14+
String result1 = String.valueOf(bd2.doubleValue()); // 输出最终结果
15+
String result2 = bd2.toPlainString(); // 输出最终结果
16+
System.out.println(result1);
17+
System.out.println(result2);
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.code.repository.study.io;
2+
3+
import java.io.IOException;
4+
import java.util.Scanner;
5+
6+
public class SystemIn {
7+
8+
/**
9+
* next() 方法遇见第一个有效字符(非空格,非换行符)时,开始扫描,即获得第一个扫描到的不含空格、换行符的单个字符串。
10+
* 例如:
11+
* 输入 " qwe ",输出"qwe"
12+
* 输入 " q w e ",输出"q"
13+
*
14+
* 使用nextLine()时,则可以扫描到一行内容并作为一个字符串而被获取到。
15+
*/
16+
public static void main(String[] args) throws IOException {
17+
Scanner s = new Scanner(System.in);
18+
System.out.println("input words:");
19+
String str1 = s.nextLine();
20+
String str2 = s.nextLine();
21+
System.out.println("input words is str1:"+str1+",str2:"+str2);
22+
}
23+
}

src/main/java/com/code/repository/study/thread/Fibonacci.java renamed to src/main/java/com/code/repository/study/math/Fibonacci.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
* Project: study
33
* File Created at 2013-5-19下午3:30:56
44
*/
5-
package com.code.repository.study.thread;
5+
package com.code.repository.study.math;
66

77
/**
88
* 斐波那契数列 f(n) = f(n-1) + f(n-2)
9+
* F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)
10+
* 1、1、2、3、5、8、13、21、34 ....
911
*
1012
* @author zhaoyuanli
1113
* 2013-5-19下午3:30:56
@@ -48,7 +50,6 @@ public void run() {
4850

4951
public static void main(String[] args) {
5052
Fibonacci f = new Fibonacci(5);
51-
5253
Thread t1 = new Thread(f,"t1:");
5354
t1.start();
5455
System.out.println("ok!");

src/main/java/com/code/repository/study/thread/MyExtendsThread.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/main/java/com/code/repository/study/thread/MyImplRunnable.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/main/java/com/code/repository/study/thread/RunnableShareDataDemo.java

Lines changed: 0 additions & 170 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.code.repository.study.thread.callable;
2+
3+
import java.util.concurrent.Callable;
4+
import java.util.concurrent.FutureTask;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* 实现Callable接口,线程运行后可以有返回值,线程运行时主线程可以继续运营
9+
* 待有结果时,返回结果再继续运行
10+
*/
11+
public class CallableTreadTest implements Callable<String> {
12+
13+
private String name;
14+
public CallableTreadTest(String name){
15+
this.name = name;
16+
}
17+
18+
@Override
19+
public String call() throws Exception {
20+
System.out.println("subTread running...");
21+
TimeUnit.SECONDS.sleep(3);
22+
System.out.println("subTread running done !");
23+
return this.name;
24+
}
25+
26+
public static void main(String[] args) throws Exception {
27+
CallableTreadTest t1 = new CallableTreadTest("t1");
28+
FutureTask<String> task = new FutureTask<String>(t1); // 生成futureTask
29+
new Thread(task).start(); // 启动线程
30+
System.out.println("main process other done!"); // 主线程继续执行其他逻辑
31+
if(!task.isDone()){
32+
System.out.println("main waiting subTread result ...");
33+
}
34+
String result = task.get(); // 主线程阻塞,等待子线程结果
35+
System.out.println("main get sub result:"+result);
36+
}
37+
}

0 commit comments

Comments
 (0)