check方法加锁
This commit is contained in:
parent
5243ec1fe1
commit
24335ccd95
@ -12,6 +12,8 @@ import java.util.Map;
|
|||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
public class ThreadBasedConnectPool<T> {
|
public class ThreadBasedConnectPool<T> {
|
||||||
static {
|
static {
|
||||||
@ -28,6 +30,7 @@ public class ThreadBasedConnectPool<T> {
|
|||||||
private PoolConfig config;
|
private PoolConfig config;
|
||||||
|
|
||||||
public ThreadBasedConnectPool(PoolConfig config) {
|
public ThreadBasedConnectPool(PoolConfig config) {
|
||||||
|
Lock l = new ReentrantLock();
|
||||||
this.config = config;
|
this.config = config;
|
||||||
threadPool.scheduleAtFixedRate(new Runnable() {
|
threadPool.scheduleAtFixedRate(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@ -62,10 +65,14 @@ public class ThreadBasedConnectPool<T> {
|
|||||||
|
|
||||||
boolean checkRet = false;
|
boolean checkRet = false;
|
||||||
try {
|
try {
|
||||||
|
l.lock();
|
||||||
checkRet = check(entry.getValue().getObject());
|
checkRet = check(entry.getValue().getObject());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
checkRet = false;
|
checkRet = false;
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
l.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
if (checkRet) {
|
if (checkRet) {
|
||||||
log.debug("一个链接检查未通过");
|
log.debug("一个链接检查未通过");
|
||||||
|
|||||||
@ -9,8 +9,7 @@ import org.junit.Assert;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class PrefTest
|
public class PrefTest {
|
||||||
{
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
|
||||||
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
@ -19,8 +18,8 @@ public class PrefTest
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
api.SYD_Short_Connect_Ex(
|
api.SYD_Short_Connect_Ex(
|
||||||
new String[]{ "192.168.0.200" },
|
new String[]{"192.168.0.200"},
|
||||||
new int[]{ 8889 },
|
new int[]{8889},
|
||||||
10,
|
10,
|
||||||
20
|
20
|
||||||
);
|
);
|
||||||
@ -29,13 +28,13 @@ public class PrefTest
|
|||||||
|
|
||||||
// 预热
|
// 预热
|
||||||
final int max = 1;
|
final int max = 1;
|
||||||
for (int i = 0; i < max; i ++) {
|
for (int i = 0; i < max; i++) {
|
||||||
// shortData( api );
|
// shortData( api );
|
||||||
// batchData( api );
|
// batchData( api );
|
||||||
try {
|
try {
|
||||||
api.SYD_SM4_ShortData( 0 , new byte[16], Consts.ECB_ENC );
|
api.SYD_SM4_ShortData(0, new byte[16], Consts.ECB_ENC);
|
||||||
// api.SYD_SM4_ShortData( 0 , new byte[16], Consts.ECB_DEC );
|
// api.SYD_SM4_ShortData( 0 , new byte[16], Consts.ECB_DEC );
|
||||||
} catch ( Exception e ){
|
} catch (Exception e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,16 +46,16 @@ public class PrefTest
|
|||||||
public void run() {
|
public void run() {
|
||||||
// 短数据
|
// 短数据
|
||||||
long st = System.currentTimeMillis();
|
long st = System.currentTimeMillis();
|
||||||
shortData( api );
|
shortData(api);
|
||||||
long et = System.currentTimeMillis();
|
long et = System.currentTimeMillis();
|
||||||
System.out.println("short t1=" + ( et -st ));
|
System.out.println("short t1=" + (et - st));
|
||||||
|
|
||||||
|
|
||||||
// 批量
|
// 批量
|
||||||
st = System.currentTimeMillis();
|
st = System.currentTimeMillis();
|
||||||
batchData( api );
|
batchData(api);
|
||||||
et = System.currentTimeMillis();
|
et = System.currentTimeMillis();
|
||||||
System.out.println("batch t1=" + ( et -st ));
|
System.out.println("batch t1=" + (et - st));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -69,54 +68,55 @@ public class PrefTest
|
|||||||
|
|
||||||
// 短数据
|
// 短数据
|
||||||
long st = System.currentTimeMillis();
|
long st = System.currentTimeMillis();
|
||||||
shortData( api );
|
shortData(api);
|
||||||
long et = System.currentTimeMillis();
|
long et = System.currentTimeMillis();
|
||||||
System.out.println("short t1=" + ( et -st ));
|
System.out.println("short t1=" + (et - st));
|
||||||
|
|
||||||
|
|
||||||
// 批量
|
// 批量
|
||||||
st = System.currentTimeMillis();
|
st = System.currentTimeMillis();
|
||||||
batchData( api );
|
batchData(api);
|
||||||
et = System.currentTimeMillis();
|
et = System.currentTimeMillis();
|
||||||
System.out.println("batch t1=" + ( et -st ));
|
System.out.println("batch t1=" + (et - st));
|
||||||
} finally {
|
} finally {
|
||||||
if ( null != api ) {
|
if (null != api) {
|
||||||
api.SYD_Disconnect_Ex();
|
api.SYD_Disconnect_Ex();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void shortData(SydApi api){
|
public static void shortData(SydApi api) {
|
||||||
// 密钥索引
|
// 密钥索引
|
||||||
int keyIdx = 0;
|
int keyIdx = 0;
|
||||||
// 测试数据
|
// 测试数据
|
||||||
byte[] data = new byte[16];
|
byte[] data = new byte[16];
|
||||||
byte[] encData = new byte[16];
|
byte[] encData = new byte[16];
|
||||||
try {
|
try {
|
||||||
encData = api.SYD_SM4_ShortData( keyIdx , data, Consts.ECB_ENC );
|
encData = api.SYD_SM4_ShortData(keyIdx, data, Consts.ECB_ENC);
|
||||||
}catch ( Exception e){}
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
byte[] orgData = api.SYD_SM4_ShortData( keyIdx , encData, Consts.ECB_DEC );
|
byte[] orgData = api.SYD_SM4_ShortData(keyIdx, encData, Consts.ECB_DEC);
|
||||||
}catch ( Exception e){}
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
// System.out.println( "HEX=" + Util.bytes2HexString( encData ) );
|
// System.out.println( "HEX=" + Util.bytes2HexString( encData ) );
|
||||||
// Assert.assertArrayEquals( orgData, data );
|
// Assert.assertArrayEquals( orgData, data );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void batchData(SydApi api){
|
public static void batchData(SydApi api) {
|
||||||
// 密钥索引
|
// 密钥索引
|
||||||
int[] keyIdx = new int[]{ 0, 1, 2 };
|
int[] keyIdx = new int[]{0, 1, 2};
|
||||||
// 测试数据
|
// 测试数据
|
||||||
List<byte[]> data = new ArrayList<>();
|
List<byte[]> data = new ArrayList<>();
|
||||||
data.add( new byte[8] );
|
data.add(new byte[8]);
|
||||||
data.add( new byte[16] );
|
data.add(new byte[16]);
|
||||||
data.add( new byte[1024] );
|
data.add(new byte[1024]);
|
||||||
|
|
||||||
List<byte[]> encData = api.SYD_SM4_BatchData( keyIdx , data, Consts.ECB_ENC );
|
List<byte[]> encData = api.SYD_SM4_BatchData(keyIdx, data, Consts.ECB_ENC);
|
||||||
List<byte[]> orgData = api.SYD_SM4_BatchData( keyIdx , encData, Consts.ECB_DEC );
|
List<byte[]> orgData = api.SYD_SM4_BatchData(keyIdx, encData, Consts.ECB_DEC);
|
||||||
|
|
||||||
for ( int i = 0 ; i < data.size() ; i++ ) {
|
for (int i = 0; i < data.size(); i++) {
|
||||||
System.out.println( "HEX=" + Util.bytes2HexString( encData.get( i ) ) );
|
System.out.println("HEX=" + Util.bytes2HexString(encData.get(i)));
|
||||||
Assert.assertArrayEquals( orgData.get( i ), data.get( i ) );
|
Assert.assertArrayEquals(orgData.get(i), data.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user