Skip to content

Commit 179e12f

Browse files
committed
init java in thinking
0 parents  commit 179e12f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1988
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.swp
2+
*.swo
3+
*.metadata/*
4+
*.RemoteSystemsTempFiles/*
5+
*.recommenders/
6+
*/bin/*
7+
*.classpath
8+
*.project
9+
*/.settings/*
10+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.chapter05;
2+
3+
import java.util.Arrays;
4+
import java.util.Random;
5+
6+
public class ArrayClassObj {
7+
8+
public static void main(String[] args) {
9+
// TODO Auto-generated method stub
10+
Random rand = new Random(47);
11+
Integer[] a1 = new Integer[rand.nextInt(20)];
12+
13+
for(int i = 0; i < a1.length; i++) {
14+
a1[i] = rand.nextInt(500);
15+
}
16+
17+
System.out.println(Arrays.toString(a1));
18+
19+
Integer[] a2 = new Integer[]{
20+
new Integer(1),
21+
new Integer(2),
22+
3,
23+
};
24+
25+
System.out.println(Arrays.toString(a2));
26+
27+
}
28+
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.chapter05;
2+
3+
import java.util.Arrays;
4+
import java.util.Random;
5+
6+
public class ArrayNew {
7+
8+
public static void main(String[] args) {
9+
// TODO Auto-generated method stub
10+
Random rand = new Random(47);
11+
int[] a1 = new int[rand.nextInt(20)];
12+
13+
System.out.println(Arrays.toString(a1));
14+
}
15+
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.chapter05;
2+
3+
public class ArraysOfPrimitives {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
int[] a1 = {1, 2, 3, 4, 5};
8+
int[] a2;
9+
a2 = a1;
10+
11+
for(int i=0; i<a2.length; i++) {
12+
a1[i] = a1[i] + 3;
13+
}
14+
15+
for(int i=0; i<a1.length; i++) {
16+
System.out.println(a1[i]);
17+
}
18+
}
19+
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.chapter05;
2+
3+
public class DynaticArray {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
Other.main(new String[]{"hello", "fine", "ok"});
8+
}
9+
10+
}
11+
12+
class Other{
13+
14+
public static void main(String[] args) {
15+
for(String s: args) {
16+
System.out.println(s);
17+
}
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//: concurrentcy/BasicThreads
2+
// The most basic use of the Thread class
3+
4+
package com.concurrency21;
5+
6+
public class BasicThreads {
7+
8+
public static void main(String[] args) {
9+
// TODO Auto-generated method stub
10+
Thread t = new Thread(new LiftOff());
11+
t.start();
12+
System.out.println("Waiting for LiftOff");
13+
}
14+
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//: concurrency/CachedThreadPool
2+
3+
package com.concurrency21;
4+
5+
import java.util.concurrent.*;
6+
7+
public class CachedThreadPool {
8+
9+
public static void main(String[] args) {
10+
// TODO Auto-generated method stub
11+
ExecutorService exec = Executors.newCachedThreadPool();
12+
for(int i = 0; i < 5; i++)
13+
exec.execute(new LiftOff());
14+
exec.shutdown();
15+
}
16+
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//: concunrrency/CallableDemo.java
2+
3+
package com.concurrency21;
4+
5+
import java.util.concurrent.*;
6+
import java.util.*;
7+
8+
class TaskWithResult implements Callable{
9+
10+
private int id;
11+
public TaskWithResult(int id) {
12+
this.id = id;
13+
}
14+
15+
@Override
16+
public String call() throws Exception {
17+
// TODO Auto-generated method stub
18+
return "Result of TaskWithResult " + this.id;
19+
}
20+
21+
}
22+
23+
24+
public class CallableDemo {
25+
26+
public static void main(String args[]) {
27+
ExecutorService exec = Executors.newCachedThreadPool();
28+
ArrayList<Future<String>> results = new ArrayList<Future<String>>();
29+
for(int i = 0; i < 10; i++) {
30+
results.add(exec.submit(new TaskWithResult(i)));
31+
}
32+
33+
for(Future<String> fs : results) {
34+
try{
35+
System.out.println(fs.get());
36+
} catch(InterruptedException e) {
37+
System.out.println(e);
38+
return;
39+
} catch(ExecutionException e) {
40+
System.out.println(e);
41+
return;
42+
} finally {
43+
exec.shutdown();
44+
}
45+
}
46+
}
47+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//: concurrency/FixedThreadPool.java
2+
3+
package com.concurrency21;
4+
5+
import java.util.concurrent.*;
6+
7+
public class FixedThreadPool {
8+
9+
public static void main(String[] args) {
10+
// TODO Auto-generated method stub
11+
ExecutorService exec = Executors.newFixedThreadPool(5);
12+
for(int i = 0; i < 5; i++)
13+
exec.execute(new LiftOff());
14+
exec.shutdown();
15+
}
16+
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.concurrency21;
2+
3+
public class LiftOff implements Runnable {
4+
5+
protected int countDown = 10;
6+
private static int taskCount = 0;
7+
private final int id = taskCount++;
8+
9+
public LiftOff() {}
10+
11+
public LiftOff(int countDown) {
12+
this.countDown = countDown;
13+
}
14+
15+
public String status() {
16+
return "#" + id + "(" + (countDown > 0? countDown : "Liftoff!") + "). ";
17+
}
18+
19+
public void run() {
20+
while(countDown-- > 0) {
21+
System.out.print(status());
22+
//Thread.yield();
23+
}
24+
}
25+
26+
public static void main(String[] args) {
27+
// TODO Auto-generated method stub
28+
29+
}
30+
31+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.concurrency21;
2+
3+
public class MainThread {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
LiftOff launch = new LiftOff();
8+
launch.run();
9+
}
10+
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//: concurrency/MoreBasicThreads.java
2+
// Adding more threads
3+
4+
package com.concurrency21;
5+
6+
public class MoreBasicThreads {
7+
8+
public static void main(String[] args) {
9+
// TODO Auto-generated method stub
10+
for(int i = 0; i < 5; i++) {
11+
new Thread(new LiftOff()).start();
12+
}
13+
System.out.println("Waiting for LiftOff");
14+
}
15+
16+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//: concurrency/SimplePriorities.java
2+
3+
package com.concurrency21;
4+
5+
import java.util.concurrent.*;
6+
7+
public class SimplePriorities implements Runnable {
8+
9+
private int countDown = 5;
10+
private volatile double d; // No optimization
11+
private int priority;
12+
13+
public SimplePriorities(int priority) {
14+
this.priority = priority;
15+
}
16+
17+
public String toString() {
18+
return Thread.currentThread() + ":" + countDown;
19+
}
20+
21+
public void run() {
22+
Thread.currentThread().setPriority(this.priority);
23+
while(true) {
24+
for(int i = 1; i < 100000000; i++) {
25+
d += (Math.PI + Math.E) / (double) i;
26+
if(i % 1000 == 0) {
27+
Thread.yield();
28+
}
29+
}
30+
31+
System.out.println(this);
32+
if(--countDown == 0) return;
33+
}
34+
}
35+
36+
public static void main(String[] args) {
37+
// TODO Auto-generated method stub
38+
ExecutorService exec = Executors.newCachedThreadPool();
39+
exec.execute(new SimplePriorities(Thread.MAX_PRIORITY));
40+
41+
for(int i = 0; i < 5; i++) {
42+
exec.execute(new SimplePriorities(Thread.MIN_PRIORITY));
43+
}
44+
45+
46+
exec.shutdown();
47+
}
48+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//: concurrency/SingleThreadExecutor.java
2+
3+
package com.concurrency21;
4+
5+
import java.util.concurrent.*;
6+
7+
public class SingleThreadExecutor {
8+
9+
public static void main(String[] args) {
10+
// TODO Auto-generated method stub
11+
ExecutorService exec = Executors.newSingleThreadExecutor();
12+
for(int i = 0; i < 5; i++)
13+
exec.execute(new LiftOff());
14+
exec.shutdown();
15+
}
16+
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//: concurrency/SleepingTask.java
2+
3+
package com.concurrency21;
4+
5+
import java.util.concurrent.*;
6+
7+
public class SleepingTask extends LiftOff{
8+
9+
public void run() {
10+
try {
11+
while(countDown-- > 0) {
12+
System.out.println(status());
13+
TimeUnit.MILLISECONDS.sleep(1000);
14+
}
15+
} catch (InterruptedException e) {
16+
System.err.println("Interrupt");
17+
}
18+
}
19+
20+
public static void main(String[] args) {
21+
// TODO Auto-generated method stub
22+
ExecutorService exec = Executors.newCachedThreadPool();
23+
for(int i = 0; i < 5; i++) {
24+
exec.execute(new SleepingTask());
25+
}
26+
}
27+
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//: interfaces/Adventure.java
2+
// Multiple interfaces
3+
4+
package com.interface09;
5+
6+
interface CanFlight {
7+
void flight();
8+
}
9+
10+
interface CanSwim {
11+
void swim();
12+
}
13+
14+
interface CanFly {
15+
void fly();
16+
}
17+
18+
class ActionCharactor {
19+
public void flight() {}
20+
}
21+
22+
class Hero extends ActionCharactor
23+
implements CanFlight, CanSwim, CanFly {
24+
public void fly() {}
25+
public void swim() {}
26+
}
27+
public class Adventure {
28+
public static void t(CanFlight x) { x.flight(); }
29+
public static void u(CanSwim x) { x.swim(); }
30+
public static void v(CanFly x) { x.fly(); }
31+
public static void w(ActionCharactor x) { x.flight(); }
32+
33+
public static void main(String[] args) {
34+
Hero h = new Hero();
35+
t(h);
36+
u(h);
37+
v(h);
38+
w(h);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)