Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2044980
  • 博文数量: 519
  • 博客积分: 10070
  • 博客等级: 上将
  • 技术积分: 3985
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-29 14:05
个人简介

只问耕耘

文章分类

全部博文(519)

文章存档

2016年(1)

2013年(5)

2011年(46)

2010年(220)

2009年(51)

2008年(39)

2007年(141)

2006年(16)

我的朋友

分类: Java

2010-01-19 16:57:21

本文试图证明多线程的效率,在有10000条数据的表中做大量查询,但结果证明多线程没有提高运行速度。
package c1;
import java.sql.*;
import java.util.Calendar;
import java.util.Properties;

public class Test {
 /**
  * @param args
  */
 public static void main(String[] args) {
  //MultiThread mt=new MultiThread();
  SingleThread.run();
  MultiThread.initconn();
 // String[] str={};
 for(int i=0;i<10;i++){
  //mt[i]=new MultiThread();
  //str[i]="s";
  //System.out.println(str[i]);
  new Thread(new MultiThread()).start();
  
  //mt[i].start();
 }
 
 
 }
}
class SingleThread{
 public static void run(){
    // TODO Auto-generated method stub
    //Test t=new Test();
//  select * from dba_tablespaces
    try{
        DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver());
           Properties conProps = new Properties();
           conProps.put("user", "sys");
           conProps.put("password", "test");
           conProps.put("defaultRowPrefetch", "15");
           conProps.put("internal_logon", "sysdba");
   //  Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test","system","test");
     Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test",conProps);
     PreparedStatement pstmt;
     ResultSet rset;
     //boolean tableexist;
     pstmt = conn.prepareStatement("select count(*) from user_tables where table_name='PERSONS'");
     //pstmt = conn.prepareStatement("select count(*) from dba_tablespaces");
     pstmt.execute();//
     rset = pstmt.executeQuery();
     while (rset.next()){
         //System.out.println (rset.getString(1));   // Print col 1
         if(rset.getInt(1)==1){
          pstmt = conn.prepareStatement("DROP TABLE Persons");
          pstmt.executeQuery();
         }
     }
  
     pstmt = conn.prepareStatement("CREATE TABLE Persons(Id_P int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255))");
     pstmt.execute();
     int i;
     int j=10000;
     for(i=0;i      pstmt = conn.prepareStatement("INSERT INTO Persons VALUES ("+i+", 'Gates"+i+"', 'Bill', 'Xuanwumen 10', 'Beijing')");
      pstmt.executeUpdate();
      pstmt.close();//we need to close pstmt, otherwise there might be exceptions like java.sql.SQLException: ORA-01000: maximum open cursors exceeded
     }
    System.out.println(i+" records were inserted into SingleThread;");
    Calendar calendar = Calendar.getInstance();
    int oday=calendar.get(Calendar.DAY_OF_MONTH);
    int ohour=calendar.get(Calendar.HOUR_OF_DAY);
    int omin=calendar.get(Calendar.MINUTE);
    int osec=calendar.get(Calendar.SECOND);
    for(i=0;i      pstmt = conn.prepareStatement("select * from persons where lastname='Gates"+i+"'");
      rset = pstmt.executeQuery();
      //while (rset.next()){
      //    System.out.println (i+"selecting records "+rset.getString(1));   // Print col 1
      //}
      pstmt.close();//we need to close pstmt, otherwise there might be exceptions like java.sql.SQLException: ORA-01000: maximum open cursors exceeded
    }
    Calendar ccalendar = Calendar.getInstance();
    int cday=ccalendar.get(Calendar.DAY_OF_MONTH);
    int chour=ccalendar.get(Calendar.HOUR_OF_DAY);
    int cmin=ccalendar.get(Calendar.MINUTE);
    int csec=ccalendar.get(Calendar.SECOND);
    if(csec-osec<0){
     csec=csec+60;
     cmin=cmin-1;
    }
    if(cmin-omin<0){    
     cmin=cmin+60;
     chour=chour-1;
    }
    if(chour-ohour<0){
     chour=chour+24;
     cday=cday-1;
    }
    System.out.println((cday-oday)+" day "+(chour-ohour)+" hour " + (cmin-omin)+" min "+(csec-osec)+" sec");
   
    //conn.close();
    System.out.println("SingleThread methord finished");
    }
    catch(SQLException e){e.printStackTrace();}
   
 }
}
class MultiThread implements Runnable {
 public static Connection conn;
 public   static int i=0;
 public static int j=10000;
 public static int cur=0;
 public int startpoint=0;
 public int endpoint=0;
 public static int id=0;
 public int threadid=id++;
 public static Calendar calendar;
 public static Calendar ccalendar;
 MultiThread(){
  
  //System.out.println("in MultiThread "+threadid);
 }
 public void run(){
  //System.out.println("in MultiThread run "+threadid);
  if(cur==0){
    calendar = Calendar.getInstance();
  }
  while(cur   synchronized(this){
    if(cur+100     startpoint=cur;
     cur=cur+100;
     endpoint=cur;
    }else{
     startpoint=cur;
     cur=cur+100;
     endpoint=j;
    }
   }
   //System.out.println("startpoint "+startpoint+"endpoint " + endpoint +"cur "+cur);
     try{
      PreparedStatement pstmt;
      ResultSet rset;
 
     for(;startpoint       pstmt = conn.prepareStatement("select * from persons where lastname='Gates"+startpoint+"'");
       rset = pstmt.executeQuery();
       //while (rset.next()){
       //    System.out.println ("selecting records "+rset.getString(1));   // Print col 1
       //}
       pstmt.close();//we need to close pstmt, otherwise there might be exceptions like java.sql.SQLException: ORA-01000: maximum open cursors exceeded
     }
 
     }
     catch(SQLException e){e.printStackTrace();}
    if(cur>=j){//cur>j means all the threads has finished;
     ccalendar = Calendar.getInstance();
       int oday=calendar.get(Calendar.DAY_OF_MONTH);
       int ohour=calendar.get(Calendar.HOUR_OF_DAY);
       int omin=calendar.get(Calendar.MINUTE);
       int osec=calendar.get(Calendar.SECOND);
      
       int cday=ccalendar.get(Calendar.DAY_OF_MONTH);
       int chour=ccalendar.get(Calendar.HOUR_OF_DAY);
       int cmin=ccalendar.get(Calendar.MINUTE);
       int csec=ccalendar.get(Calendar.SECOND);
      
       if(csec-osec<0){
        csec=csec+60;
        cmin=cmin-1;
       }
       if(cmin-omin<0){    
        cmin=cmin+60;
        chour=chour-1;
       }
       if(chour-ohour<0){
        chour=chour+24;
        cday=cday-1;
       }
       System.out.println((cday-oday)+" day "+(chour-ohour)+" hour " + (cmin-omin)+" min "+(csec-osec)+" sec");
       System.out.println("MultiThread methord finished");
    }
  }
  
  //System.out.println("in MultiThread runend "+threadid);
 }
 public static void initconn(){
    try{
        DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver());
           Properties conProps = new Properties();
           conProps.put("user", "sys");
           conProps.put("password", "test");
           conProps.put("defaultRowPrefetch", "15");
           conProps.put("internal_logon", "sysdba");
   //  Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test","system","test");
     conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test",conProps);
     PreparedStatement pstmt;
     ResultSet rset;
     //boolean tableexist;
     pstmt = conn.prepareStatement("select count(*) from user_tables where table_name='PERSONS'");
     //pstmt = conn.prepareStatement("select count(*) from dba_tablespaces");
     pstmt.execute();//
     rset = pstmt.executeQuery();
     while (rset.next()){
         //System.out.println (rset.getString(1));   // Print col 1
         if(rset.getInt(1)==1){
          pstmt = conn.prepareStatement("DROP TABLE Persons");
          pstmt.executeQuery();
         }
     }
  
     pstmt = conn.prepareStatement("CREATE TABLE Persons(Id_P int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255))");
     pstmt.execute();
     for(i=0;i      pstmt = conn.prepareStatement("INSERT INTO Persons VALUES ("+i+", 'Gates"+i+"', 'Bill', 'Xuanwumen 10', 'Beijing')");
      pstmt.executeUpdate();
      pstmt.close();//we need to close pstmt, otherwise there might be exceptions like java.sql.SQLException: ORA-01000: maximum open cursors exceeded
     }
    System.out.println(i+" records were inserted in MultiThread;");
    }
    catch(SQLException e){e.printStackTrace();}
 }
 
}
/*
10000 records were inserted into SingleThread;
0 day 0 hour 0 min 57 sec
SingleThread methord finished
10000 records were inserted in MultiThread;
0 day 0 hour 0 min 57 sec
MultiThread methord finished
*/
阅读(1813) | 评论(0) | 转发(0) |
0

上一篇:构造函数特点

下一篇:synchronized 关键字

给主人留下些什么吧!~~