Skip to content

Commit 58fedc1

Browse files
committed
191224
1 parent eacbe25 commit 58fedc1

File tree

7 files changed

+178
-2
lines changed

7 files changed

+178
-2
lines changed

src/com/blankj/study/base/Node.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ public class Node {
44
private String data;
55
private Node next;
66

7+
public Node( ) {
8+
super();
9+
this.data = "";
10+
}
11+
712
public Node(String data) {
813
super();
914
this.data = data;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.blankj.study.corejava;
2+
//静态导入
3+
import javafx.beans.binding.ObjectExpression;
4+
5+
import static java.lang.Math.sqrt;
6+
7+
public class Test01 {
8+
public static void main(String[] args) {
9+
System.out.println(sqrt(4));
10+
//数值类型转换
11+
Object o1 = (1D+1);
12+
Object o2 = (2F+2);
13+
Object o3 = (3L+3);
14+
Object o4 = ('A'+'A');
15+
System.out.println("顺序dfli");
16+
17+
String ans = String.join("long","我","是","龙");
18+
System.out.println(ans);
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.blankj.study.corejava;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
public class Test02 {
7+
public static void main(String[] args) {
8+
ArrayList<Integer> list = new ArrayList<>();
9+
list.ensureCapacity(1);
10+
System.out.println(list.size());
11+
list.add(1);
12+
list.add(2);
13+
list.add(3);
14+
Integer[] arr = new Integer[list.size()];
15+
list.toArray(arr);
16+
System.out.println(Arrays.toString(arr));
17+
18+
Integer i = list.remove(1);
19+
System.out.println(i);
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.blankj.study.corejava;
2+
3+
/**
4+
* 枚举类
5+
*/
6+
enum Size{
7+
//example
8+
SMALL("s"),
9+
MEDIUM("m"),
10+
LARGE("l"),
11+
EXTRA_LARGE("xl");
12+
13+
private Size(String abbreviation){
14+
this.abbreviation = abbreviation;
15+
}
16+
17+
private String abbreviation;
18+
public String getAbbreviation(){
19+
return abbreviation;
20+
}
21+
}
22+
23+
public class Test03 {
24+
public static void main(String[] args) {
25+
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.blankj.study.corejava;
2+
3+
public class Test04 {
4+
public volatile static int i = 0;
5+
6+
public static class AddThread extends Thread {
7+
@Override
8+
public void run() {
9+
for (i = 0; i < 1000000000; i++) ;
10+
}
11+
}
12+
13+
static Acount instance = new Acount();
14+
static int a = 0;
15+
public static class Acount implements Runnable{
16+
@Override
17+
public void run(){
18+
for(int j = 0;j<100000000;j++){
19+
synchronized (instance){
20+
a++;
21+
}
22+
}
23+
}
24+
}
25+
26+
public static void main(String[] args) throws InterruptedException {
27+
AddThread addThread = new AddThread();
28+
long start = System.currentTimeMillis();
29+
addThread.start();
30+
//等待线程结束 归来 默认参数为0,无限等待其执行时间,否则指定有限时间等待
31+
addThread.join(1);
32+
long end = System.currentTimeMillis();
33+
System.out.println(end - start);
34+
35+
Thread t1 = new Thread(instance);
36+
Thread t2 = new Thread(instance);
37+
t1.start();t2.start();
38+
t1.join();t2.join();
39+
System.out.println(a);
40+
41+
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.blankj.study.corejava;
2+
3+
import com.blankj.study.base.Node;
4+
5+
public class Test05 {
6+
public static final int initData = 666;
7+
8+
//常量 静态变量 类信息置于方法区 (Test05.class 与node指针)
9+
public static Node node = new Node();
10+
11+
//每一个方法对应一个栈帧内存区域(cpmpute()栈帧)
12+
public int compute(){
13+
//局部变量表存放在栈
14+
int a = 1;
15+
int b = 2;
16+
//操作数栈
17+
int c = (a+b)*10;
18+
return c;
19+
}
20+
21+
public static void main(String[] args) {
22+
//对象置于堆,在main()栈帧中存放局部变量表 test指向它
23+
Test05 test = new Test05();
24+
test.compute();
25+
}
26+
}
Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,54 @@
11
package com.blankj.study.singleton;
22

33

4-
// 懒汉单例 线程安全
4+
/** 懒汉单例 线程安全
5+
* @author Administrator
6+
*/
57
public class LazySingleton {
8+
/**
9+
* 屏蔽外部构造实例的途径
10+
*/
611
private LazySingleton() {
712
}
813

14+
/**
15+
* static修饰属于类的变量 volatile禁止指令重排 初始null懒加载机制
16+
*/
917
private static volatile LazySingleton instance = null;
1018

1119
public static LazySingleton getInstance() {
1220
if (instance == null) {
21+
//加锁线程同步
1322
synchronized (LazySingleton.class) {
14-
if (instance == null)
23+
//双重检验,可能有多个线程进入到这里待执行
24+
if (instance == null){
1525
instance = new LazySingleton();
26+
}
1627
}
1728
}
1829
return instance;
1930
}
31+
32+
public static void main(String[] args) {
33+
new Thread(new Runnable() {
34+
@Override
35+
public void run() {
36+
LazySingleton lazySingleton = LazySingleton.getInstance();
37+
System.out.println(lazySingleton);
38+
}
39+
}).start();
40+
41+
new Thread(new Runnable() {
42+
@Override
43+
public void run() {
44+
LazySingleton lazySingleton = LazySingleton.getInstance();
45+
try {
46+
Thread.sleep(1000);
47+
} catch (InterruptedException e) {
48+
e.printStackTrace();
49+
}
50+
System.out.println(lazySingleton);
51+
}
52+
}).start();
53+
}
2054
}

0 commit comments

Comments
 (0)