Skip to content

Commit d7ff411

Browse files
Something btp180309 (#17)
* 170537-Generic * 0528-Generic * Chapter-15-Generic-btp0530 * Chapter-16-Arrays-btp170603 * Chapter-17-Collections-btp170618 * Chapter 17 collections btp170710 * Chapter 17 collections btp170711 * Chapter 17 collections btp170722 * Chapter 18 IO/NIO btp170802 * Chapter 18 IO/NIO btp170806 * Chapter 19 enum btp170821 * Chapter 20 Annotation btp170826 * Chapter 20 concurrency btp170917 * 一些代码 * 一些代码
1 parent a4698d9 commit d7ff411

11 files changed

Lines changed: 318 additions & 1 deletion

File tree

src/WorkerThread/Channel.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package WorkerThread;
2+
3+
public class Channel {
4+
private static final int MAX_REQUEST = 100;
5+
private final Request[] requestQueue;
6+
private int tail;
7+
private int head;
8+
private int count;
9+
10+
private final WorkerThread[] threadPool;
11+
12+
public Channel(int threads) {
13+
this.requestQueue = new Request[MAX_REQUEST];
14+
this.head = 0;
15+
this.tail = 0;
16+
this.count = 0;
17+
18+
threadPool = new WorkerThread[threads];
19+
20+
for(int i=0;i<threadPool.length;i++) {
21+
threadPool[i] = new WorkerThread("Worker-"+i,this);;
22+
}
23+
}
24+
25+
public void startWorkers() {
26+
for(int i = 0;i<threadPool.length;i++) {
27+
threadPool[i].start();
28+
}
29+
}
30+
31+
public synchronized void putRequest(Request request) {
32+
while(count >= requestQueue.length) {
33+
try {
34+
wait();
35+
}catch(InterruptedException e) {
36+
37+
}
38+
}
39+
requestQueue[tail] = request;
40+
tail = (++tail)%requestQueue.length;
41+
count++;
42+
this.notifyAll();
43+
}
44+
45+
public synchronized Request takeRequest() {
46+
while(count <= 0) {
47+
try {
48+
wait();
49+
}catch(InterruptedException e) {
50+
51+
}
52+
}
53+
Request request = requestQueue[head];
54+
head = (++head)%requestQueue.length;
55+
count--;
56+
this.notifyAll();
57+
return request;
58+
59+
}
60+
}

src/WorkerThread/ClientThread.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package WorkerThread;
2+
3+
import java.util.Random;
4+
5+
public class ClientThread extends Thread {
6+
private final Channel channel;
7+
private static final Random random = new Random();
8+
public ClientThread(String name,Channel channel) {
9+
super(name);
10+
this.channel = channel;
11+
}
12+
public void run() {
13+
try {
14+
for(int i = 0;true;i++) {
15+
Request request = new Request(getName(),i);
16+
channel.putRequest(request);
17+
Thread.sleep(random.nextInt(1000));
18+
}
19+
}catch(InterruptedException e) {
20+
21+
}
22+
}
23+
}

src/WorkerThread/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package WorkerThread;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
Channel channel = new Channel(5);
7+
channel.startWorkers();
8+
new ClientThread("³ÂÄ˾²",channel).start();
9+
new ClientThread("ÅëÖó",channel).start();
10+
new ClientThread("ÅôÖí^(*£þ(oo)£þ)^",channel).start();
11+
}
12+
13+
}

src/WorkerThread/Request.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package WorkerThread;
2+
3+
import java.util.Random;
4+
5+
public class Request {
6+
private final String name;
7+
private final int number;
8+
private static final Random random = new Random();
9+
public Request(String name,int number) {
10+
this.name = name;
11+
this.number = number;
12+
}
13+
14+
public void execute() {
15+
System.out.println(Thread.currentThread().getName()+"executes"+this);
16+
try {
17+
Thread.sleep(random.nextInt(1000));
18+
}catch(InterruptedException e) {
19+
20+
}
21+
}
22+
23+
public String toString() {
24+
return "[ Request from "+name+" No."+number+"]";
25+
}
26+
}

src/WorkerThread/WorkerThread.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package WorkerThread;
2+
3+
public class WorkerThread extends Thread {
4+
private final Channel channel;
5+
public WorkerThread(String name,Channel channel) {
6+
super(name);
7+
this.channel = channel;
8+
}
9+
public void run() {
10+
while(true) {
11+
Request request = channel.takeRequest();
12+
request.execute();
13+
}
14+
}
15+
}

src/btp/oneP/ABtpTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.util.concurrent.atomic.AtomicInteger;
1313
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
1414
import java.util.concurrent.locks.ReentrantLock;
15-
import java.util.zip.DeflaterOutputStream;
1615

1716
public class ABtpTest {
1817

src/btp/oneP/DeepClone.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package btp.oneP;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.io.ObjectInputStream;
7+
import java.io.ObjectOutputStream;
8+
import java.io.Serializable;
9+
10+
public class DeepClone implements Serializable,Cloneable{
11+
12+
/**
13+
*
14+
*/
15+
private static final long serialVersionUID = 1L;
16+
17+
private String name;
18+
private DeepClone son;
19+
public String getName() {
20+
return name;
21+
}
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
public DeepClone getSon() {
26+
return son;
27+
}
28+
public void setSon(DeepClone son) {
29+
this.son = son;
30+
}
31+
32+
public DeepClone(String name) {
33+
super();
34+
this.name = name;
35+
}
36+
public DeepClone deepClone() throws IOException, ClassNotFoundException {
37+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
38+
ObjectOutputStream oos = new ObjectOutputStream(bos);
39+
oos.writeObject(this);
40+
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
41+
ObjectInputStream ois = new ObjectInputStream(bis);
42+
return (DeepClone)ois.readObject();
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "DeepClone [name=" + name + ", son=" + son + "]";
48+
}
49+
public static void main(String[] args) throws ClassNotFoundException, IOException, CloneNotSupportedException {
50+
DeepClone f = new DeepClone("father");
51+
f.setSon(new DeepClone("son"));
52+
DeepClone clone = f.deepClone();
53+
System.out.println("É±´£º"+clone);
54+
System.out.println(clone.getSon() == f.getSon());
55+
DeepClone ff = (DeepClone)f.clone();
56+
System.out.println("dz¿½±´£º"+ff);
57+
System.out.println(ff.getSon() == f.getSon());
58+
}
59+
60+
}

src/btp/oneP/DynamicalProxy.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package btp.oneP;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.Proxy;
6+
7+
public class DynamicalProxy {
8+
9+
public static void main(String[] args) {
10+
Fly fly = (Fly)Proxy.newProxyInstance(RealFly.class.getClassLoader(), new Class[] {Fly.class}, new ProxyImpl(new RealFly()));
11+
fly.fly();
12+
}
13+
14+
15+
interface Fly{
16+
void fly();
17+
}
18+
static class RealFly implements Fly{
19+
20+
@Override
21+
public void fly() {
22+
System.out.println("flying...");
23+
}
24+
25+
}
26+
static class ProxyImpl implements InvocationHandler{
27+
Fly fly;
28+
public ProxyImpl(Fly fly){
29+
this.fly = fly;
30+
}
31+
@Override
32+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
33+
System.out.println("before");
34+
method.invoke(fly, args);
35+
System.out.println("after");
36+
return null;
37+
}
38+
39+
}
40+
}

src/btp/oneP/HashMapTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package btp.oneP;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
import java.util.Map.Entry;
7+
8+
public class HashMapTest {
9+
10+
public static void main(String[] args) {
11+
LinkedHashMap<Integer,String> hashMap = new LinkedHashMap<>();
12+
hashMap.put(5, "e");
13+
hashMap.put(1, "a");
14+
hashMap.put(2, "b");
15+
hashMap.put(3, "c");
16+
hashMap.put(4, "d");
17+
18+
for(Entry<Integer, String> entry:hashMap.entrySet()) {
19+
System.out.println(entry.getKey()+":"+entry.getValue());
20+
}
21+
}
22+
23+
}

src/btp/oneP/LRU.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package btp.oneP;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
6+
7+
public class LRU<K,V> extends LinkedHashMap<K, V> implements Map<K,V>{
8+
public LRU(int initialCapacity,float loadFactor,boolean accessOrder) {
9+
super(initialCapacity, loadFactor, accessOrder);
10+
}
11+
12+
public static void main(String[] args) {
13+
AbstractQueuedSynchronizer aqs = null;
14+
}
15+
16+
}
17+
18+
abstract class KHGKJ{
19+
abstract void f();
20+
}

0 commit comments

Comments
 (0)