Skip to content

Commit 263b137

Browse files
committed
一些工具类的测试
1 parent 5b049bb commit 263b137

11 files changed

Lines changed: 191 additions & 4 deletions

File tree

‎JavaTest_Zone/src/classload/DynamicLoaderTestCase.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ private static void testParent() throws ClassNotFoundException, InstantiationExc
6969
// clazz.getGenericSuperclass();
7070
Child parent = (Child) (clazz.newInstance());
7171

72-
73-
74-
7572
Map<String, byte[]> bytecode2 = DynamicLoader.compile("TestClass.java", javaSrc);
7673
classLoader.add(bytecode2);
7774
Class clazz2 = classLoader.loadClass("TestClass");

‎JavaTest_Zone/src/staticUtils顺序/Us.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package staticUtils顺序;
22

33
public class Us {
4+
static int i=1;
45
static {
6+
i=2;
7+
System.out.println(i);
58
System.out.println("static 前面的");
69
}
710
public static void gaga(){
@@ -10,4 +13,6 @@ public static void gaga(){
1013
static {
1114
System.out.println("static 后边的");
1215
}
16+
17+
1318
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package 并发.valatitle;
2+
3+
/**
4+
* Created by fuzhipeng on 2016/12/2.
5+
*/
6+
public class VolatileTest {
7+
//TODO volatile 第一个语义 所有线程的可见性。保证读取的值是最新;
8+
//TODO volatile 第2个语义 禁止指令重排序优化;
9+
10+
11+
//TODO 正确性的: 1. 确保单一线程(多线程就要 同步)修改值, 2 读取的话,是没有问题的;
12+
static volatile boolean shutdownRequested;
13+
14+
public void shutdown() {
15+
shutdownRequested = true;
16+
}
17+
18+
public void doWork() {
19+
while (!shutdownRequested) {
20+
System.out.println(Thread.currentThread() + ":haha");
21+
}
22+
System.out.println(Thread.currentThread() + ":停下来了");
23+
}
24+
25+
public static void main(String[] args) throws InterruptedException {
26+
for (int i = 0; i < 5; i++) {
27+
new Thread(new Runnable() {
28+
@Override
29+
public void run() {
30+
new VolatileTest().doWork();
31+
}
32+
}).start();
33+
}
34+
Thread.sleep(100);
35+
36+
synchronized (VolatileTest.class){
37+
VolatileTest.shutdownRequested=true;
38+
System.out.println("---Stop---");
39+
}
40+
41+
}
42+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package 并发.valatitle.反面教材;
2+
3+
/**
4+
* Created by fuzhipeng on 2016/12/2.
5+
*/
6+
public class VolatitleFail1 {
7+
//TODO volatile:保证了读取的值是正确的,
8+
//todo 但是++不是原子操作,那么并行的时候可能同时读取到race而不做真正的加1操作,
9+
//TODO 那么 同时读取的值 同时 在读取的值上+1 返回给race的时候 ,就相当于变少了;
10+
public static volatile int race=0;
11+
12+
public static void main(String[] args) {
13+
for (int i = 0; i < 20; i++) {
14+
new Thread(new Runnable() {
15+
@Override
16+
public void run() {
17+
for (int i1 = 0; i1 < 10000; i1++) {
18+
race++;
19+
}
20+
}
21+
}).start();
22+
}
23+
//当前活着的线程个数
24+
while(Thread.activeCount()>2)//大于两个我就等待 ,知道所有活着的<2就是1,就是main这个 就可以执行了
25+
//就是说当一个线程使用了这个方法之后,它就会把自己CPU执行的时间让掉,
26+
Thread.yield();
27+
System.out.println(race);
28+
}
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package 父类相关;
2+
3+
/**
4+
* Created by fuzhipeng on 2016/12/1.
5+
*/
6+
public class Par_ {
7+
public void heihei(){
8+
return ;
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package 访问权限;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Created by fuzhipeng on 2016/12/5.
7+
*/
8+
public class Bao {
9+
public static int b;
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package 访问权限.包1;
2+
3+
import 访问权限.Bao;
4+
5+
/**
6+
* Created by fuzhipeng on 2016/12/5.
7+
*/
8+
public class HeiTEST {
9+
public static void main(String[] args) {
10+
int k=Bao.b;
11+
}
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package 语法糖;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* Created by fuzhipeng on 2016/12/2.
8+
*/
9+
public class Sugar {
10+
//看bin目录下的编译文件;
11+
public static void main(String[] args) {
12+
13+
//泛型 自动装箱,自动拆箱,便利循环,变长参数;
14+
List<Integer> list= Arrays.asList(1,2,3,4);
15+
int sum=0;
16+
for (int integer : list) {
17+
sum+=integer;
18+
}
19+
System.out.println(sum);
20+
}
21+
22+
// // FIXME: 2016/12/2 下面两个,类型擦除 后是一样的所以不行
23+
// public static void ak(List<String> stringList){
24+
//
25+
// }
26+
// public static void ak(List<Integer> intList){
27+
//
28+
// }
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package 语法糖;
2+
3+
/**
4+
* Created by fuzhipeng on 2016/12/9.
5+
*/
6+
public class 推断泛型 {
7+
8+
public static void main(String[] args) {
9+
Integer i = get(new Child1());
10+
}
11+
12+
private static <T> T get(Par v) {
13+
return (T) v;
14+
}
15+
16+
public static class Par {
17+
18+
}
19+
20+
public static class Child1 extends Par {
21+
22+
}
23+
24+
public static class Child2 extends Par {
25+
26+
}
27+
28+
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package 语法糖;
2+
3+
/**
4+
* Created by fuzhipeng on 2016/12/2.
5+
*/
6+
public class 自动装箱陷阱 {
7+
public static void main(String[] args) {
8+
Integer a = 1;
9+
Integer b = 2;
10+
Integer c = 3;
11+
Integer d = 3;
12+
Integer e = 321;
13+
Integer f = 321;
14+
Long g = 3L;
15+
System.out.println(c == d);
16+
//todo 为什么e==f 是false
17+
//原因 这是源码中的 Integer 中 valueOf,也就是说cache中已有-128到127,不在这范围的会新new ,这时可以理解比较是内存地址
18+
System.out.println(e == f);
19+
System.out.println(c == (a + b));
20+
System.out.println(c.equals(a + b));
21+
System.out.println(g == (a + b));
22+
System.out.println(g.equals(a + b));
23+
}
24+
}

0 commit comments

Comments
 (0)