-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo.java
143 lines (142 loc) · 4.1 KB
/
Demo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class DemoRunnable implements Runnable {
private int index_;
DemoRunnable(int index) {
index_ = index;
System.out.println("E: this should not be printed");
}
@Override
public void run() {
System.out.printf("Thread[%d].start=%d", java.lang.Thread.currentThread().getId(), index_);
}
}
public class Demo {
static long x_;
public static void setX(int x) {
x_ = x;
}
public static void setX(long x) {
x_ = x;
}
public static int getX() {
return (int)x_;
}
static enum Random {
ZERO,
ONE
}
// class InnerClass { };
public static void demoPrintf() {
String differentAddress = "a";
if(differentAddress == differentAddress + "") {
System.out.println("E: + operator returned old address");
}
if(!differentAddress.equals("a" + "")) {
System.out.println("E: .equals fail");
}
try {
// String methodName = new Throwable().getStackTrace()[0].getMethodName();
// String methodName = InnerClass.class.getEnclosingMethod().getName();
// String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
// String methodName = new Object() {}.getClass().getEnclosingMethod().getName();
String methodName = "demoPrintf";
System.out.printf("%7.8s", methodName + " entered");
System.out.printf("%7d", (int)01210.1);
System.out.printf("%7.8f", (double)01210.1);
System.out.printf("%b\n", (boolean)true);
} catch(java.util.IllegalFormatPrecisionException e) {
System.out.println("IllegalFormatPrecisionException ");
// e.printStackTrace();
}
}
public static void demoArgs(String args[]) {
for(int i = 0; args.length > i; ++i) {
try {
//setX(Integer.parseInt(args[i]));
setX(Math.round(Double.parseDouble(args[i])));
System.out.print(" round(arg1) = " + getX());
setX((int)Double.parseDouble(args[i]));
System.out.print(" (int)arg1 = " + getX());
} catch(NumberFormatException e) {
// System.out.print("NumberFormatException");
System.out.printf("args[%d]=\"%s\"\n", i, args[i]);
}
}
}
public static void demoMath() {
int n = 3;
double[] constant = {
Math.PI,
Math.E,
Double.NEGATIVE_INFINITY
};
double[] rand = new double[n];
for(int i = 0; n > i; ++i) {
rand[i] = Math.random();
}
int x = 1, y = 2;
x ^= y;
y ^= x;
x ^= y;
if(x != 2 || y != 1) {
System.out.println("mathSwap failed");
}
System.out.print("PI=" + constant[0]);
System.out.print(" E=" + constant[1]);
System.out.print(" -INF=" + constant[2]);
System.out.printf(" [0,1)=" + rand[0]);
System.out.printf(" [0,1)=%1.2f=%1.2e", rand[1], rand[1]);
System.out.printf(" %b=", 0.5 > rand[2] ? false : true);
switch((int)Math.round(rand[2])) {
case 0: System.out.println("case 0"); break;
case 1: System.out.println("case 1"); break;
}
}
public static void demoThread() {//throws InterruptedException {
final int n = 8;
Thread threads[] = new Thread[n];
for(int i = 0; n > i; ++i) {
try {
final int threadIndex = i;
threads[i] = new Thread(new DemoRunnable(i));
/*threads[i] = new Thread(new Runnable() {
@Override
public void run() {
System.out.printf("threads[%d].start.getId=%d", threadIndex, java.lang.Thread.currentThread().getId());
}
});*/
threads[i].start();
// threads[i] = new Thread( () -> {
// System.out.printf("threads[%i]", currentThread().getId());
// });
System.out.printf("threads[%d].getId=%d ", i, threads[i].getId());
} catch (Exception e) {
System.out.printf("E: threads[%d].start threw", i);
e.printStackTrace();
}
}
for(int i = n; 0 != i--; ) {
long threadID = threads[i].getId();
try {
threads[i].join();
System.out.printf(" %d->threads[%d].join", threadID, i);
} catch (InterruptedException e) {
System.out.println("E: {%d}.join=InterruptedException");
e.printStackTrace();
}
}
}
public static void main(String args[]) {
try {
demoPrintf();
demoArgs(args);
demoMath();
demoThread();
// demoThreadPool();
DemoCollection.main(args);
DemoClass.main(args);
} catch (NoClassDefFoundError e) {
System.out.println("NoClassDefFoundError, check for NAME$*.class to link");
e.printStackTrace();
}
}
}