Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/WorkerThread/Channel.java
Original file line number Diff line number Diff line change
@@ -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<threadPool.length;i++) {
threadPool[i] = new WorkerThread("Worker-"+i,this);;
}
}

public void startWorkers() {
for(int i = 0;i<threadPool.length;i++) {
threadPool[i].start();
}
}

public synchronized void putRequest(Request request) {
while(count >= 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;

}
}
23 changes: 23 additions & 0 deletions src/WorkerThread/ClientThread.java
Original file line number Diff line number Diff line change
@@ -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) {

}
}
}
13 changes: 13 additions & 0 deletions src/WorkerThread/Main.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
26 changes: 26 additions & 0 deletions src/WorkerThread/Request.java
Original file line number Diff line number Diff line change
@@ -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+"]";
}
}
15 changes: 15 additions & 0 deletions src/WorkerThread/WorkerThread.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
1 change: 0 additions & 1 deletion src/btp/oneP/ABtpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
60 changes: 60 additions & 0 deletions src/btp/oneP/DeepClone.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
40 changes: 40 additions & 0 deletions src/btp/oneP/DynamicalProxy.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
}
23 changes: 23 additions & 0 deletions src/btp/oneP/HashMapTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer,String> 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<Integer, String> entry:hashMap.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}

}
20 changes: 20 additions & 0 deletions src/btp/oneP/LRU.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package btp.oneP;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

public class LRU<K,V> extends LinkedHashMap<K, V> implements Map<K,V>{
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();
}
38 changes: 38 additions & 0 deletions src/btp/oneP/TestSync2.java
Original file line number Diff line number Diff line change
@@ -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<String,Object> hm = new HashMap<>();
}

@Override
public void run() {
try {
m1();
}catch(InterruptedException e) {
e.printStackTrace();
}
}

}