2011年9月2日

          ibatis分頁<一>

          package com.lxy.dialect;
          public abstract interface Dialect {
          public boolean supportLimit();
          public String getLimitString(String sql,boolean hasoffset);
          public String getLimitString(String sql,int offset,int limit);
          }


          package com.lxy.dialect;
          public class MyDialect implements Dialect {
          protected static final String SQL_END_DELIMITER = ";";
          public String getLimitString(String sql, boolean hasoffset) {
          return new StringBuffer(sql.length() + 20).append(trim(sql)).append(
          hasoffset ? "limit ?,?" : "limit ?").append(SQL_END_DELIMITER)
          .toString();
          }
          public String getLimitString(String sql, int offset, int limit) {
          sql = trim(sql);
          StringBuffer sb = new StringBuffer(sql.length() + 20);
          sb.append(sql);
          if (offset > 0) {
          sb.append("limit").append(offset).append(',').append(limit).append(
          ";");
          } else {
          sb.append("limit").append(limit).append(";");
          }
          return sb.toString();
          }
          public boolean supportLimit() {
          return true;
          }
          private String trim(String sql) {
          sql = sql.trim();
          if (sql.endsWith(";")) {
          sql = sql.substring(0, sql.length() - 1 - ";".length());
          }
          return sql;
          }
          }

          posted @ 2011-09-14 19:11 crazy-李陽 閱讀(215) | 評論 (0)編輯 收藏

          線程池代碼

          ThreadPoolManager

          package com.threadpool.test;
          import java.util.LinkedList;
          import java.util.Queue;
          import java.util.concurrent.ArrayBlockingQueue;
          import java.util.concurrent.Executors;
          import java.util.concurrent.RejectedExecutionException;
          import java.util.concurrent.RejectedExecutionHandler;
          import java.util.concurrent.ScheduledExecutorService;
          import java.util.concurrent.ScheduledFuture;
          import java.util.concurrent.ThreadPoolExecutor;
          import java.util.concurrent.TimeUnit;

          import org.omg.CORBA.TIMEOUT;

          import com.nio.test.ReadFileThread;

          public class ThreadPoolManager {

              private static ThreadPoolManager tpm = new ThreadPoolManager();

              // 線程池最小線程數(shù)
              private final static int MIN_SIZE = 4;
              // 線程池最大線程數(shù)
              private final static int MAX_SIZE = 10;
              // 線程池維護線程允許的空閑限制
              private final static int KEEP_ACTIVE_TIME = 0;
              // 線程池用的緩沖隊列大小
              private final static int WORK_QUEUE_SIZE = 10;
              // 消息緩沖隊列
              Queue queue = new LinkedList();
              
              final Runnable accessBuffeThread = new Runnable()
              {

                  public void run() {
                      
                      if( hasMoreAcquire() ){
                          
                          String msg = ( String ) queue.poll();
                          Runnable task = new AccessDBThread( msg );
                          threadpool.execute( task );
                      }
                  }
                  
              };
              // 無法由 ThreadPoolExecutor 執(zhí)行的任務(wù)的處理程序
              final RejectedExecutionHandler handler = new RejectedExecutionHandler()
              {

                  public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                      // TODO Auto-generated method stub
                      System.out.println(((AccessDBThread )r).getMsg()+"消息放入隊列中重新等待執(zhí)行");
                      queue.offer((( AccessDBThread ) r ).getMsg() );
                  }
                  
              };
              
              final ThreadPoolExecutor threadpool = new ThreadPoolExecutor(MIN_SIZE, MAX_SIZE, KEEP_ACTIVE_TIME, TimeUnit.SECONDS, new ArrayBlockingQueue(WORK_QUEUE_SIZE),this.handler);
              
              // 調(diào)度線程池

              final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool( 1 );

              final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(accessBuffeThread, 0, 1, TimeUnit.SECONDS);
              
              public static ThreadPoolManager newinstance()
              {
                  return tpm;
              }
              
              private ThreadPoolManager (){}
              
              
              private boolean hasMoreAcquire()
              {
                  return !queue.isEmpty();
              }
              
              public void addLogMsg(String msg)
              {
               Runnable task = new AccessDBThread(msg);
               threadpool.execute(task);
              }
          }
          AccessDBThread
          package com.threadpool.test;

          public class AccessDBThread implements Runnable{

              private String msg;
              public String getMsg() {
                  return msg;
              }
              public void setMsg(String msg) {
                  this.msg = msg;
              }
              
              public AccessDBThread(){
                  super();
              }
              
              public AccessDBThread(String msg)
              {
                  this.msg=msg;
              }
              public void run() {
                  // TODO Auto-generated method stub
                  System.out.println("Added the message: "+msg+" into the Database");
              }

              
          }

          測試類:
          package com.threadpool.test;


          public class TestDriver {

              ThreadPoolManager tpm = ThreadPoolManager.newinstance();
              
              public void addMsg(String msg)
              {
                  tpm.addLogMsg(msg);
              }
              public static void main(String[] args) {
                  for(int i=0;i<100;i++)
                  {
                      new TestDriver().addMsg(Integer.toString(i));
                  }
              }
          }



          posted @ 2011-09-02 15:20 crazy-李陽 閱讀(422) | 評論 (0)編輯 收藏

          <2011年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 胶州市| 镇安县| 桐乡市| 山东省| 和顺县| 噶尔县| 调兵山市| 璧山县| 汽车| 花莲县| 建平县| 陆丰市| 阳江市| 阳谷县| 凤台县| 株洲县| 安远县| 娱乐| 岑巩县| 六安市| 乌兰县| 汝南县| 集贤县| 田东县| 苏尼特左旗| 晋城| 马龙县| 崇文区| 绩溪县| 清远市| 东乡族自治县| 益阳市| 鲁甸县| 云林县| 公安县| 象州县| 辰溪县| 汕尾市| 古丈县| 乐陵市| 松溪县|