package cn.ispl.cims.common.util;import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;/**
* JDBC连接数据库工具类
* @author admin
*
*/
public class ConnUtil {
/*模拟数据库连接池*/
private static List<Connection> connList = new ArrayList<Connection>();
/*数据库连接url*/
private static String url = "";
/*连接驱动*/
private static String driverClassName = "";
/*数据库用户名*/
private static String username = "";
/*数据库密码*/
private static String password = ""; /**
* 初始化数据库连接参数
*/
public static void initConn(){
Properties prop = new Properties();
InputStream is = null;
try {
is = ConnUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
prop.load(is);
} catch (IOException e1) {
e1.printStackTrace();
}
ConnUtil.url = prop.getProperty("datasource.url");
ConnUtil.driverClassName = prop.getProperty("datasource.driverClassName");
ConnUtil.username = prop.getProperty("datasource.username");
ConnUtil.password = prop.getProperty("datasource.password");
try {
Class.forName(ConnUtil.driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} initConnPool(); }
/**
* 初始化数据库连接池
*/
public static void initConnPool(){
for(int i=0;i<30;i++){
try {
connList.add(DriverManager.getConnection(ConnUtil.url, ConnUtil.username, ConnUtil.password));
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 得到数据库连接
* @return 数据库连接
*/
public synchronized static Connection getConn() {
Connection conn = connList.get(1);
connList.remove(1);
return conn;
}
/**
* 关闭数据库连接
* @param rs
* @param ps
* @param conn
*/
public synchronized static void close(ResultSet rs,PreparedStatement ps,Connection conn){
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
back(conn);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 把数据库连接返回给数据池
* @param conn
*/
public static void back(Connection conn){
connList.add(conn);
}
}
// scheduler.schedule(new PersistSSTThread(json), 0, TimeUnit.SECONDS);
}
static class PersistSSTThread implements Runnable {
static int count=0;;
long lastSSTSentTime=0;
String json;
boolean flag=false;
public PersistSSTThread(String json) {
this.json=json;
}
//if flag is true,mean need send sst to proxy ignoring the other conditions
public PersistSSTThread(boolean flag) {
this.flag=flag;
} @Override
public void run() {
try {
SingleDAO.getinstance().insertSST(json);
} catch (JSONException e) {
Log_E("insert sst message to db occur error and Exception is "+e.getMessage());
}
//Execution postSST2Server method at least meet one of the two conditions
if(Calendar.getInstance().getTimeInMillis() - lastSSTSentTime<Constant.SST_TIME * 60 * 1000){
postSST2Server(null,0);
Log_I("exceed time limit,send sst");
return;
}
if(count>=Constant.SST_AMOUNT){
postSST2Server(null,Constant.SST_AMOUNT);
Log_I("exceed count limit,send sst");
return;
}
}
}
我写了一个
class TestThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "的i值为" + i);
}
}}public class ThreadPoolTest {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(7);
pool.submit(new TestThread());
pool.submit(new TestThread());
pool.submit(new TestThread());
pool.submit(new TestThread());
pool.submit(new TestThread());
pool.submit(new TestThread());//第6条线程
pool.shutdown();
}
}怎么让/第6条线程能执行呢
我写了一个
class TestThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "的i值为" + i);
}
}}public class ThreadPoolTest {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(7);
pool.submit(new TestThread());
pool.submit(new TestThread());
pool.submit(new TestThread());
pool.submit(new TestThread());
pool.submit(new TestThread());
pool.submit(new TestThread());//第6条线程
pool.shutdown();
}
}怎么让/第6条线程能执行呢
你确定第六个线程没有执行?