多线程连接jdbc 造数据

多线程两种方式,第三种方式 用线程池   开线程池节省线程停止唤醒的时间。 并发包的时候用原子。

打jar包 用命令java -jar  xxx.jar  shell 脚本 跑 和监控  jvm

一、

package threads;

public class CreateDataByThreads implements Runnable {
	public void run() {
		while (true) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "线程run起来");
		}

	}

	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			//注释的方式不能进行多线程跑,必须得用start方法
//			System.out.println(i);
//			new CreateDataByThreads().run();
			//用start方法可以
			Thread tt = new Thread (new CreateDataByThreads());
			tt.start();
		}

	}

}


二、

package threads;

public class CreateDataByThread2 {
	public static void main(String[] args){
		System.out.println(Thread.currentThread().getName()+"线程run起来");
		for(int i = 0 ; i < 3 ;i++){
			Thread tt = new Thread (new Runnable() {
				public void run (){
					for(int i = 0 ;i < 1000; i++){
						System.out.println(Thread.currentThread().getName()+"run ok"+i);
					}
				}
				
			});
			tt.start();
		}
		
		
	
	}
}


三、线程池

package threadPool;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import threads.CreateDataByThreads;

public class ThreadPoolTest {
	public static void main(String[] args){
		Executor executor = Executors.newFixedThreadPool(10);
		for(int i = 0; i < 1000;i++){
			executor.execute(new CreateDataByThreads());
		}
	}
}

四、多线程连接jdbc 造数据

package threads;

import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class CreateDatasClient implements Runnable{
	private String url;
	private String username;
	private String password;
	
	public  CreateDatasClient(String url,String username,String password){
		this.url = url;
		this.username = username;
		this.password = password;
	}
	
	public static void main (String[] args){
		String url ="jdbc:mysql://localhost:3306/testing91?"
				+ "user=root&password=123456&useUnicode=true&characterEncoding=UTF8";
		String username = "testusername";
		String password = "testpassword";
		for (int i = 0; i < 10; i++){
			Thread tt = new Thread(new CreateDatasClient(url,username,password));
			tt.start();
			
		}
	}

	public void run() {
		Connection conn = getConn(url);
		try {
			String sql = "insert into testing91 values ('" + username + "','" + password + "');";
			System.out.println(sql);
			Statement stmt = (Statement) conn.createStatement();
			stmt.executeUpdate(sql);
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
	}
	public static Connection getConn(String url){
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = (Connection) DriverManager.getConnection(url);
			if (null != conn) {
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值