MINA(2)Producer Consumer and Encode

MINA(2)Producer Consumer and Encode

1. Producer and Consumer, Threads pool
Threads Pool Example
package com.sillycat.easynio.plugins.mina.threadpool;

public class Worker implements Runnable {

private String cardNumber;

public Worker(String cardNumber) {
this.cardNumber = cardNumber;
}

public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread: " + Thread.currentThread().getName()
+ " do the job " + cardNumber);
}

}

package com.sillycat.easynio.plugins.mina.threadpool;

import java.util.LinkedList;

public class ThreadPool {

private final int nThreads;

private final PoolWorker[] threads;

private final LinkedList<Runnable> queue;

public ThreadPool(int nThreads) {
this.nThreads = nThreads;
queue = new LinkedList<Runnable>();
threads = new PoolWorker[nThreads];
for (int i = 0; i < nThreads; i++) {
threads[i] = new PoolWorker();
threads[i].start();
}
}

public void addJob(Runnable r) {
synchronized (queue) {
queue.addLast(r);
queue.notify();
}
}

private class PoolWorker extends Thread {
public void run() {
Runnable r;
while (true) {
synchronized (queue) {
while (queue.isEmpty()) {
try {
queue.wait();
} catch (InterruptedException ignored) {
}
}
r = queue.removeFirst();
}
try {
r.run();
} catch (RuntimeException e) {
}
}
}
}

public int getnThreads() {
return nThreads;
}

}

package com.sillycat.easynio.plugins.mina.threadpool;

public class ThreadPoolTest {

public static void main(String[] args) {
ThreadPool pool = new ThreadPool(20);
pool.addJob(new Worker("050"));
pool.addJob(new Worker("job0110"));
pool.addJob(new Worker("001job"));
}

}

MINA Thread Pool
IoAcceptor
IoConnector
IoProcessor
ExecutorFilter

2. Encode
ProtocolCodecFilter

references:
http://www.iteye.com/topic/1124736
http://www.iteye.com/topic/1125178

http://mina.apache.org


http://www.blogjava.net/mikechen/archive/2012/03/15/371938.html
http://www.iteye.com/topic/1112123


猜你喜欢

转载自sillycat.iteye.com/blog/1595618