diff --git a/src/WorkerThread/Channel.java b/src/WorkerThread/Channel.java new file mode 100644 index 0000000..e88b910 --- /dev/null +++ b/src/WorkerThread/Channel.java @@ -0,0 +1,60 @@ +package WorkerThread; + +public class Channel { + private static final int MAX_REQUEST = 100; + private final Request[] requestQueue; + private int tail; + private int head; + private int count; + + private final WorkerThread[] threadPool; + + public Channel(int threads) { + this.requestQueue = new Request[MAX_REQUEST]; + this.head = 0; + this.tail = 0; + this.count = 0; + + threadPool = new WorkerThread[threads]; + + for(int i=0;i= requestQueue.length) { + try { + wait(); + }catch(InterruptedException e) { + + } + } + requestQueue[tail] = request; + tail = (++tail)%requestQueue.length; + count++; + this.notifyAll(); + } + + public synchronized Request takeRequest() { + while(count <= 0) { + try { + wait(); + }catch(InterruptedException e) { + + } + } + Request request = requestQueue[head]; + head = (++head)%requestQueue.length; + count--; + this.notifyAll(); + return request; + + } +} diff --git a/src/WorkerThread/ClientThread.java b/src/WorkerThread/ClientThread.java new file mode 100644 index 0000000..721a6eb --- /dev/null +++ b/src/WorkerThread/ClientThread.java @@ -0,0 +1,23 @@ +package WorkerThread; + +import java.util.Random; + +public class ClientThread extends Thread { + private final Channel channel; + private static final Random random = new Random(); + public ClientThread(String name,Channel channel) { + super(name); + this.channel = channel; + } + public void run() { + try { + for(int i = 0;true;i++) { + Request request = new Request(getName(),i); + channel.putRequest(request); + Thread.sleep(random.nextInt(1000)); + } + }catch(InterruptedException e) { + + } + } +} diff --git a/src/WorkerThread/Main.java b/src/WorkerThread/Main.java new file mode 100644 index 0000000..0caa1e5 --- /dev/null +++ b/src/WorkerThread/Main.java @@ -0,0 +1,13 @@ +package WorkerThread; + +public class Main { + + public static void main(String[] args) { + Channel channel = new Channel(5); + channel.startWorkers(); + new ClientThread("³ÂÄ˾²",channel).start(); + new ClientThread("ÅëÖó",channel).start(); + new ClientThread("ÅôÖí^(*£þ(oo)£þ)^",channel).start(); + } + +} diff --git a/src/WorkerThread/Request.java b/src/WorkerThread/Request.java new file mode 100644 index 0000000..3c23d14 --- /dev/null +++ b/src/WorkerThread/Request.java @@ -0,0 +1,26 @@ +package WorkerThread; + +import java.util.Random; + +public class Request { + private final String name; + private final int number; + private static final Random random = new Random(); + public Request(String name,int number) { + this.name = name; + this.number = number; + } + + public void execute() { + System.out.println(Thread.currentThread().getName()+"executes"+this); + try { + Thread.sleep(random.nextInt(1000)); + }catch(InterruptedException e) { + + } + } + + public String toString() { + return "[ Request from "+name+" No."+number+"]"; + } +} diff --git a/src/WorkerThread/WorkerThread.java b/src/WorkerThread/WorkerThread.java new file mode 100644 index 0000000..bbdba0a --- /dev/null +++ b/src/WorkerThread/WorkerThread.java @@ -0,0 +1,15 @@ +package WorkerThread; + +public class WorkerThread extends Thread { + private final Channel channel; + public WorkerThread(String name,Channel channel) { + super(name); + this.channel = channel; + } + public void run() { + while(true) { + Request request = channel.takeRequest(); + request.execute(); + } + } +} diff --git a/src/btp/oneP/ABtpTest.java b/src/btp/oneP/ABtpTest.java index ce51024..569f13c 100644 --- a/src/btp/oneP/ABtpTest.java +++ b/src/btp/oneP/ABtpTest.java @@ -12,7 +12,6 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.AbstractQueuedSynchronizer; import java.util.concurrent.locks.ReentrantLock; -import java.util.zip.DeflaterOutputStream; public class ABtpTest { diff --git a/src/btp/oneP/DeepClone.java b/src/btp/oneP/DeepClone.java new file mode 100644 index 0000000..709c136 --- /dev/null +++ b/src/btp/oneP/DeepClone.java @@ -0,0 +1,60 @@ +package btp.oneP; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +public class DeepClone implements Serializable,Cloneable{ + + /** + * + */ + private static final long serialVersionUID = 1L; + + private String name; + private DeepClone son; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public DeepClone getSon() { + return son; + } + public void setSon(DeepClone son) { + this.son = son; + } + + public DeepClone(String name) { + super(); + this.name = name; + } + public DeepClone deepClone() throws IOException, ClassNotFoundException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(this); + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bis); + return (DeepClone)ois.readObject(); + } + + @Override + public String toString() { + return "DeepClone [name=" + name + ", son=" + son + "]"; + } + public static void main(String[] args) throws ClassNotFoundException, IOException, CloneNotSupportedException { + DeepClone f = new DeepClone("father"); + f.setSon(new DeepClone("son")); + DeepClone clone = f.deepClone(); + System.out.println("É±´£º"+clone); + System.out.println(clone.getSon() == f.getSon()); + DeepClone ff = (DeepClone)f.clone(); + System.out.println("dz¿½±´£º"+ff); + System.out.println(ff.getSon() == f.getSon()); + } + +} diff --git a/src/btp/oneP/DynamicalProxy.java b/src/btp/oneP/DynamicalProxy.java new file mode 100644 index 0000000..4c85194 --- /dev/null +++ b/src/btp/oneP/DynamicalProxy.java @@ -0,0 +1,40 @@ +package btp.oneP; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +public class DynamicalProxy { + + public static void main(String[] args) { + Fly fly = (Fly)Proxy.newProxyInstance(RealFly.class.getClassLoader(), new Class[] {Fly.class}, new ProxyImpl(new RealFly())); + fly.fly(); + } + + + interface Fly{ + void fly(); + } + static class RealFly implements Fly{ + + @Override + public void fly() { + System.out.println("flying..."); + } + + } + static class ProxyImpl implements InvocationHandler{ + Fly fly; + public ProxyImpl(Fly fly){ + this.fly = fly; + } + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + System.out.println("before"); + method.invoke(fly, args); + System.out.println("after"); + return null; + } + + } +} diff --git a/src/btp/oneP/HashMapTest.java b/src/btp/oneP/HashMapTest.java new file mode 100644 index 0000000..daba96d --- /dev/null +++ b/src/btp/oneP/HashMapTest.java @@ -0,0 +1,23 @@ +package btp.oneP; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +public class HashMapTest { + + public static void main(String[] args) { + LinkedHashMap hashMap = new LinkedHashMap<>(); + hashMap.put(5, "e"); + hashMap.put(1, "a"); + hashMap.put(2, "b"); + hashMap.put(3, "c"); + hashMap.put(4, "d"); + + for(Entry entry:hashMap.entrySet()) { + System.out.println(entry.getKey()+":"+entry.getValue()); + } + } + +} diff --git a/src/btp/oneP/LRU.java b/src/btp/oneP/LRU.java new file mode 100644 index 0000000..e23309a --- /dev/null +++ b/src/btp/oneP/LRU.java @@ -0,0 +1,20 @@ +package btp.oneP; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.locks.AbstractQueuedSynchronizer; + +public class LRU extends LinkedHashMap implements Map{ + public LRU(int initialCapacity,float loadFactor,boolean accessOrder) { + super(initialCapacity, loadFactor, accessOrder); + } + + public static void main(String[] args) { + AbstractQueuedSynchronizer aqs = null; + } + +} + +abstract class KHGKJ{ + abstract void f(); +} \ No newline at end of file diff --git a/src/btp/oneP/TestSync2.java b/src/btp/oneP/TestSync2.java new file mode 100644 index 0000000..1ed45cd --- /dev/null +++ b/src/btp/oneP/TestSync2.java @@ -0,0 +1,38 @@ +package btp.oneP; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.NavigableMap; + +public class TestSync2 implements Runnable{ + int b = 100; + + synchronized void m1() throws InterruptedException{ + b = 1000; + Thread.sleep(500); + System.out.println("b="+b); + } + synchronized void m2() throws InterruptedException{ + Thread.sleep(250); + b = 2000; + } + + public static void main(String[] args) throws InterruptedException{ + TestSync2 tt = new TestSync2(); + Thread t = new Thread(tt); + t.start(); + tt.m2(); + System.out.println("main thread b="+tt.b); + HashMap hm = new HashMap<>(); + } + + @Override + public void run() { + try { + m1(); + }catch(InterruptedException e) { + e.printStackTrace(); + } + } + +}