Java Multithreading Sub-database table data

Source: Internet
Author: User

Recently, the massive data analysis suddenly interest, plus the previous company has the need for a library, want to use multithreading to solve data query result set merge problem.

In the case of massive data, simple table partitioning has no way to meet the requirements of data table operation, this time need to use the database table or library. The table and the library can greatly reduce the pressure of the database or table, but the data query is more cumbersome, need to take the corresponding table or database data, then there is no way to achieve database consolidation. Multi-threaded query is a method that can improve execution efficiency and obtain result set. First create a database connection public class:

Package com.threadexecutor;

Import Sun.applet.Main;
Import java.sql.*;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
/** * Created by Jimmy on 2014/10/26.
    * * Public class Normaljdbc {private Connection Connection;
    Private PreparedStatement PS;

    Private ResultSet ResultSet;
            Public Connection getconnection () {try {class.forname ("com.mysql.jdbc.Driver");
        Connection = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "Test", "test");
        catch (ClassNotFoundException e) {e.printstacktrace ();
        catch (SQLException e) {e.printstacktrace ();
    return connection;
        Public list<map> getquery (String tablename) {list<map> List = new arraylist<> ();
        String sql = "Select Id,name,salary from" +tablename+ "";
        Connection = getconnection (); try {PS = CONNECTION.PReparecall (SQL);
            ResultSet = Ps.executequery ();
                while (Resultset.next ()) {map map = new hashmap<> ();
                Map.put ("id", Resultset.getint (1));
                Map.put ("Name", Resultset.getstring (2));
                Map.put ("Salary", Resultset.getint (3));
            List.add (map);
            Close (Connection,ps,resultset);
        Thread.Sleep (1000);
            catch (SQLException e) {e.printstacktrace ();
        Close (Connection,ps,resultset);
        }/*catch (Interruptedexception e) {e.printstacktrace ();
    }*/return list;
                public void Close (Connection conn,preparedstatement Ps,resultset rs) {try {if (ps!=null) {
                Ps.close ();
            Ps=null;
                } if (Rs!=null) {rs.close ();
            Rs=null;
     } if (Conn!=null) {conn.close ();           Conn=null;
        }}catch (SQLException e) {e.printstacktrace (); }

    }

}
Then write the threading class, where the thread with the return parameter is used:
Package com.threadexecutor;
Import java.util.ArrayList;
Import java.util.List;
Import java.util.concurrent.Callable;

Import Java.util.concurrent.CountDownLatch;
/** * Created by Jimmy on 2014/10/26.
    * * Public class Newjdbcthread implements callable<list>{private List list=new arraylist<> ();
    Private String TableName;
        The main thread is executing after the private countdownlatch latch;//child threads are executed, this method is not now applicable to public newjdbcthread (String tablename,countdownlatch latch) {
        This.tablename = tablename;
    This.latch = latch;
    Public Newjdbcthread (String tablename) {this.tablename = tablename;
        @Override public List Call () {Normaljdbc normaljdbc = new Normaljdbc ();
        List List1 = Normaljdbc.getquery (this.tablename);
        System.out.println (Thread.CurrentThread (). GetName () + "" +list1.size ());
        try {thread.sleep (1000);
        catch (Interruptedexception e) {e.printstacktrace (); }//latch. Countdown ();/return list1;
    Public List GetList () {return this.list;

 }
}
Main thread:
Package com.threadexecutor;
Import java.util.ArrayList;
Import java.util.List;
Import java.util.concurrent.ExecutionException;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;

Import Java.util.concurrent.Future;
/** * Created by Jimmy on 2014/10/26. 
        */public class Mergeresult {public static void main (string[] args) throws Interruptedexception, Executionexception {
        Long start = System.currenttimemillis ();
        List List = new arraylist<> ();
        Thread pool initialization Executorservice executor= (Executorservice) Executors.newcachedthreadpool ();
        Create a task list list<newjdbcthread> threads = new arraylist<> ();
        Initializes the task list for (int i=1;i<5;i++) {Threads.add (New Newjdbcthread ("Test" +i)); } list<future<list>> results = null;//Accept processing returned result set//execute threading results = Executor.invokeall
        (threads); Get result set for (future<list> future:results) {
            List.addall ((List) future.get ());//Speak the thread return value to List and add to the main thread result set} System.out.println ("Execution Time:" + (Syste
    M.currenttimemillis ()-start) + "Data volume" +list.size ());
 }
}

After testing, this is because the thread sleeps, if single-threaded execution, 4 threads need more than 4,400 milliseconds, the database executes more than 400 milliseconds, but through multithreading it only takes more than 1400 milliseconds, which is the time of execution. Therefore, it is proved that multithreading execution is in line with the requirements. The first time to write a blog, Welcome to Pat Bricks. More welcome advice.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.