多线程
Posted jassy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程相关的知识,希望对你有一定的参考价值。
import java.util.concurrent.CountDownLatch; public class ThreadTest { public static void main(String[] args) { ThreadTest ti=new ThreadTest(); ti.multiThreadImport(1); ti.multiThreadImport(10); System.out.println("本机CPU数:"+Runtime.getRuntime().availableProcessors()); } public void multiThreadImport(final int ThreadNum){ final CountDownLatch cdl= new CountDownLatch(ThreadNum); long starttime=System.currentTimeMillis(); for(int k=1;k<=ThreadNum;k++){ new Thread(new Runnable() { @Override public void run() { try { System.out.println("多线程"+ThreadNum); cdl.countDown(); } catch (Exception e) { e.printStackTrace(); } } }).start(); } try { cdl.await(); long spendtime=System.currentTimeMillis()-starttime; System.out.println( ThreadNum+"个线程花费时间:"+spendtime); } catch (Exception e) { e.printStackTrace(); } } }
多线程插入数据
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.UUID; import java.util.concurrent.CountDownLatch; public class ThreadTest { static Connection conn; public void multiThreadImport( final int ThreadNum){ final CountDownLatch cdl= new CountDownLatch(ThreadNum); long starttime=System.currentTimeMillis(); for(int k=1;k<=ThreadNum;k++){ new Thread(new Runnable() { @Override public void run() { // 获得连接 try { conn=getConnect(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //插入500/线程数条数据 for(int i=1;i<=10/ThreadNum;i++){ String uuid=UUID.randomUUID().toString().replace("-", ""); try { //Statement statement = conn.createStatement(); PreparedStatement ps = conn.prepareStatement("insert into FILEINFO(id) values (?)"); //statement.executeUpdate(sql); //ps.addBatch("insert into demo(name,adress) values(‘"+uuid+"‘,‘"+uuid+"‘)"); ps.setString(1, uuid); ps.executeUpdate(); if(i%10==0){ ps.executeBatch(); //10条执行一次 System.out.println("INSERT into FILEINFO(id) values " + uuid); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } cdl.countDown(); } }).start(); } try { cdl.await(); long spendtime=System.currentTimeMillis()-starttime; System.out.println( ThreadNum+"个线程花费时间:"+spendtime); } catch (InterruptedException e) { e.printStackTrace(); } } public Connection getConnect(){ Connection con = null; String url = ""; String user = ""; String password = ""; try { Class.forName("oracle.jdbc.driver.OracleDriver"); if(MyFile.getInfo("d:/njdx.ini").length()>0){ for(int i=0;i<MyFile.getInfo("d:/njdx.ini").split(",").length;i++){ url = MyFile.getInfo("d:/njdx.ini").split(",")[0];//数据库连接地址 user = MyFile.getInfo("d:/njdx.ini").split(",")[1];//数据库用户名 password = MyFile.getInfo("d:/njdx.ini").split(",")[2];//数据库密码 } } con=DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } return con; } public static void main(String[] args) throws Exception { ThreadTest ti=new ThreadTest(); ti.multiThreadImport(1); System.out.println("+++1个线程结束+++"); ti.multiThreadImport(2); System.out.println("+++2个线程结束+++"); ti.multiThreadImport(8); System.out.println("+++8个线程结束+++"); ti.multiThreadImport(10); System.out.println("+++10个线程结束+++"); System.out.println("笔记本CPU数:"+Runtime.getRuntime().availableProcessors()); } }
以上是关于多线程的主要内容,如果未能解决你的问题,请参考以下文章