check方法加锁

This commit is contained in:
junj2.liang 2022-08-15 15:39:01 +08:00
parent 5243ec1fe1
commit 24335ccd95
2 changed files with 39 additions and 32 deletions

View File

@ -12,6 +12,8 @@ import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadBasedConnectPool<T> {
static {
@ -28,6 +30,7 @@ public class ThreadBasedConnectPool<T> {
private PoolConfig config;
public ThreadBasedConnectPool(PoolConfig config) {
Lock l = new ReentrantLock();
this.config = config;
threadPool.scheduleAtFixedRate(new Runnable() {
@Override
@ -62,10 +65,14 @@ public class ThreadBasedConnectPool<T> {
boolean checkRet = false;
try {
l.lock();
checkRet = check(entry.getValue().getObject());
} catch (Exception e) {
checkRet = false;
}
finally {
l.unlock();
}
if (checkRet) {
log.debug("一个链接检查未通过");

View File

@ -9,8 +9,7 @@ import org.junit.Assert;
import java.util.ArrayList;
import java.util.List;
public class PrefTest
{
public class PrefTest {
public static void main(String[] args) throws InterruptedException {
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
@ -73,7 +72,6 @@ public class PrefTest
long et = System.currentTimeMillis();
System.out.println("short t1=" + (et - st));
// 批量
st = System.currentTimeMillis();
batchData(api);
@ -94,10 +92,12 @@ public class PrefTest
byte[] encData = new byte[16];
try {
encData = api.SYD_SM4_ShortData(keyIdx, data, Consts.ECB_ENC);
}catch ( Exception e){}
} catch (Exception e) {
}
try {
byte[] orgData = api.SYD_SM4_ShortData(keyIdx, encData, Consts.ECB_DEC);
}catch ( Exception e){}
} catch (Exception e) {
}
// System.out.println( "HEX=" + Util.bytes2HexString( encData ) );
// Assert.assertArrayEquals( orgData, data );
}