锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
import java.util.Vector;
public class ObjectPool {
private static final boolean PRINTABLE = true;
private Class<?> clazz = null;
private int numObjects = 10;
private int maxObjects = 50;
private Vector<PooledObject> objects = null;
public static void main(String[] args) throws Exception {
ObjectPool pool = new ObjectPool(XX.class);
pool.createPool();
XX xx = (XX) pool.getObject();
xx.reset();
xx.setAge(1);
xx.setSex("M");
pool.returnObject(xx);
pool.closeObjectPool();
}
public ObjectPool(Class<?> clazz) {
this.clazz = clazz;
}
public synchronized void createPool() throws Exception {
if (this.objects != null)
return;
this.objects = new Vector<PooledObject>();
for (int i = 0; i < this.numObjects; ++ i) {
createObjects();
}
}
public synchronized Object getObject() throws Exception {
if (this.objects == null)
return null;
Object conn = this.getFreeObject();
while (conn == null) {
wait(250);
conn = this.getFreeObject();
}
return conn;
}
private Object getFreeObject() throws Exception {
Object obj = this.findFreeObject();
if (obj == null) {
this.createObjects();
obj = this.findFreeObject();
if (obj == null) {
return null;
}
}
return obj;
}
private void createObjects() throws Exception {
if (this.objects.size() < this.maxObjects) {
this.message("created" + this.objects.size());
Object obj = this.clazz.newInstance();
this.objects.addElement(new PooledObject(this.objects.size(), obj));
}
}
private Object findFreeObject() {
this.message("before free:--------");
this.print();
Object obj = null;
PooledObject pooledObject = null;
Enumeration<PooledObject> enumerate = objects.elements();
while (enumerate.hasMoreElements()) {
pooledObject = (PooledObject) enumerate.nextElement();
if (!pooledObject.isBusy()) {
pooledObject.setBusy(true);
obj = pooledObject.getObject();
this.message("free object");
break;
}
}
this.message("after free:--------");
this.print();
return obj;
}
public void returnObject(Object obj) {
if (this.objects == null)
return;
this.message("before return:--------");
this.print();
PooledObject pooledObject = null;
Enumeration<PooledObject> enumerate = objects.elements();
while (enumerate.hasMoreElements()) {
pooledObject = (PooledObject) enumerate.nextElement();
if (obj == pooledObject.getObject()) {
pooledObject.setBusy(false);
this.message("return object");
break;
}
}
this.message("after return:--------");
this.print();
}
public synchronized void closeObjectPool() {
if (this.objects == null)
return;
PooledObject pooledObject = null;
Enumeration<PooledObject> enumerate = objects.elements();
while (enumerate.hasMoreElements()) {
pooledObject = (PooledObject) enumerate.nextElement();
this.message(pooledObject.toString());
if (pooledObject.isBusy()) {
this.message("wait 5000");
wait(5000);
}
}
this.objects.clear();
this.objects = null;
}
private void wait(int millionSeconds) {
try {
Thread.sleep(millionSeconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void print() {
Enumeration<PooledObject> enumerate = objects.elements();
while (enumerate.hasMoreElements()) {
PooledObject pooledObject = (PooledObject) enumerate.nextElement();
this.message(pooledObject.toString());
}
this.message("print end==============================");
}
public void message(String msg) {
if (PRINTABLE) {
System.out.println(msg);
}
}
class PooledObject {
private int index = 0;
Object objection = null;// 瀵硅薄
boolean busy = false; // 姝ゅ璞℃槸鍚︽鍦ㄤ嬌鐢ㄧ殑鏍囧織錛岄粯璁ゆ病鏈夋鍦ㄤ嬌鐢?nbsp;
// 鏋勯犲嚱鏁幫紝鏍規嵁涓涓?nbsp;Object 鏋勫憡涓涓?nbsp;PooledObject 瀵硅薄
public PooledObject(int index, Object objection) {
this.index = index;
this.objection = objection;
this.busy = false;
}
// 榪斿洖姝ゅ璞′腑鐨勫璞?nbsp;
public Object getObject() {
return objection;
}
// 璁劇疆姝ゅ璞$殑錛屽璞?nbsp;
public void setObject(Object objection) {
this.objection = objection;
}
// 鑾峰緱瀵硅薄瀵硅薄鏄惁蹇?nbsp;
public boolean isBusy() {
return busy;
}
// 璁劇疆瀵硅薄鐨勫璞℃鍦ㄥ繖
public void setBusy(boolean busy) {
this.busy = busy;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public String toString() {
return String.format("[PooledObject] index:%d, busy:%b", this.index, this.busy);
}
}
}
class XX {
private int age = 0;
private String sex = "";
public XX() {
}
public void reset() {
this.age = 0;
this.sex = "";
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
]]>
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.StringTokenizer;
public class Test {
public static native void hello();
static final String LIBFILENAME = "/home/my/java/libtest.so";
static {
//System.loadLibrary(LIBFILENAME);
try {
// 鑾峰彇鍒癹ava.library.path 鍙婄郴緇熷彉閲忎腑Path涓殑鍐呭
String libpath = System.getProperty("java.library.path");
if (libpath == null || libpath.length() == 0) {
throw new RuntimeException("java.library.path is null");
}
//javaBinPath jdk鐨刡in鐩綍D:\Program Files\Java\jdk1.6.0_11\bin
String javaBinPath = null;
StringTokenizer st = new StringTokenizer(libpath,
System.getProperty("path.separator"));
if (st.hasMoreElements()) {
javaBinPath = st.nextToken();
} else {
throw new RuntimeException("can not split library path:"
+ libpath);
}
// 鎶奷ll鏂囦歡鍐欏叆鍒癹ava.library.path涓dll鏀懼湪ConvertWord2HM鐩稿悓鐩綍涓嬶紝榪欎釜鍙互鏄綘鐨勭被鍚?/span>
InputStream inputStream = Test.class.getResourceAsStream(
LIBFILENAME);
final File dllFile = new File(LIBFILENAME);
if (!dllFile.exists()) {
FileOutputStream outputStream = new FileOutputStream(dllFile);
byte[] array = new byte[1024];
int bytesRead = -1;
while ((bytesRead = inputStream.read(array)) != -1) {
outputStream.write(array, 0, bytesRead);
}
outputStream.flush();
outputStream.close();
}
// 鍔ㄦ佸姞杞絛ll
System.load(dllFile.getPath());
// 鍦ㄨ櫄鎷熸満鍏抽棴鐨勬椂鍊欏垹闄ll 榪欓噷鐪嬬潃鐢ㄥ惂
// dllFile.deleteOnExit();
} catch (Throwable e) {
throw new RuntimeException("load Convert.dll error!", e);
}
}
public static void main(String[] args) {
Test.hello();
}
}
榪欎釜鏂囦歡鏄湪鐩綍/home/my/java涓嬬殑
javac Test.java
javah Test //鐢熸垚Test.h
鐒跺悗鑷繁鍐橳est.cpp
.java .h .cpp 榪欎簺鏂囦歡鍚嶅瓧涓瀹氳涓鏍?br />
鐢⊿ystem.loadLibrary() 涓鐩村姞杞戒笉浜?鍙兘緗戜笂鎵句簡涓姞杞絣ib鐨勪唬鐮?/div>
]]>