发表于: 2020-05-22 22:40:44

1 1421


今天完成的事情:学习下多线程,批量插入100万条数据。还有就是简单学了git命令

在 Intellij IDEA 中部署 Java 应用到 阿里云 ECS,

https://yq.aliyun.com/articles/673825

安装 Cloud Toolkit是一个免费的 IDE 插件,帮助阿里云用户更高效的使用阿里云。

http://toolkit.aliyun.com/eclipse/#AquireAccessKey

一.多线程

1.进程和线程的概念

进程是可执行程序在计算机存储器中的指令序列,动态执行的过程。

线程看作是子程序,一个进程可以由多个线程组成;

2.线程创建有三种方法

2.1创建一个Thread类,或者Thread的子类的对象

线程执行的顺序是随机的,并且start()方法只能调用一次。

2.2创建一个实现Runable接口的对象

Runable只有一个方法run(),任何实现线程功能的类必须实现该接口

public class TestApp implements Runnable {
   @Test
   public void insert100W() throws InterruptedException {
       TestApp testApp = new TestApp();
       Thread th1 = new Thread(testApp);
       th1.start();
   }
}

2.3创建一个实现Ca、


3.线程的五种运行状态

新建>可运行>正在运行>阻塞>终止

3.1 sleep()方法

是thread的方法:public static void sleep(long millis)
作用是让正在执行的线程休眠指定的毫秒数,之后重新变为可运行状态。参数是休眠的时间,单位是毫秒。
在run方法中调用时,使用Thread.sleep(xxx);
调用sleep()方法,需要进行异常处理。调用try catch。

3.2 join()方法

Thread类的方法:public final void join()
作用:调用join方法的线程,具有不限时的优先执行权。
public final void join(longs millis)
作用:调用join方法的线程,具有millis毫秒的优先执行权,之后又回到随机执行。

3.3 线程优先级

Java为线程提供了10个优先级
优先级可以用整数1-10表示,超过范围会抛出异常。
主线程默认优先级为5,数字越大优先级别越高
优先级常量
MAX_PRIORITY : 线程的最高优先级10
MIN_PRIORITY : 线程的最低优先级1
NORM_PRIORITY : 线程的默认优先级5

优先级相关的方法
public int getPriority() 获取线程优先级的方法
public void setPriority(int newPriority) 设置线程优先级的方法

3.4线程同步

使用关键字synchronized(同步)实现,确保共享对象在同一时刻只能被一个线程访问,这种机制称为线程的同步,或者叫做线程的互斥。

3.5线程互斥

线程间的通信
wait()方法:中断方法的执行,使线程处于等待状态;(阻塞) 需要进行异常处理
notify()方法:唤醒处于等待的某一个线程,使其进入可运行状态
notifyAll()方法:唤醒所有处于等待的线程,进入可运行状态,一般用这个方法比较多


4.单个插入100万次,不做任何优化,一秒一个插入,差不多花了3个多小时才10万个

long insert(Student student); //接口

Mapper

<insert id="insert" parameterType="com.hyx.entity.Student" >
   insert into student (number,name,qq,job,university,link,target,brother,createTime,updateTime)
   values
      (#{number},#{name},#{qq},#{job},#{university},#{link},#{target},#{brother},#{createTime},#{updateTime})
</insert>
@Test
public void insert100W() throws InterruptedException {
   long begin = System.currentTimeMillis();
   for(int j=0;j<1000000;j++){
       Student stu = new Student();
       stu.setNumber(j+2);
       stu.setName("周星星");
       stu.setUniversity("西南财经大学");
       stu.setBrother("周正华");
       stu.setJob("PM工程师");
       System.out.println("......1");
       stu.setLink("www.jnshu.com");
       stu.setTarget("不入IT誓不还");
       Long s = Long.valueOf(new Date().getTime());
       stu.setCreateTime(s);
       stu.setUpdateTime(s);
       stu.setQq("1131023043");
       studentService.insert(stu);
   }
   long end = System.currentTimeMillis();
   logger.info("插入100万条数据耗时:" + (end-begin));
}

2.看了周师兄的日报,跟着寻求优化条件(多线程+修改数据源+两个for语句+修改SQL)

接口

//插入100万条
long insert(List<Student> student);
<insert id="insert" parameterType="java.util.List" >
insert into student (number,name,qq,job,university,link,target,brother,createTime,updateTime)
values
<foreach collection="student"  index="index" item="item"  open="(" separator="," close=")">
(#{number},#{name},#{qq},#{job},#{university},#{link},#{target},#{brother},#{createTime},#{updateTime})
</foreach>
</insert>

测试

@Test
public void insert100W() throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(1000);
//
   final CountDownLatch latch = new CountDownLatch(1);
executorService.execute(new Runnable() {
public void run() {
List<Student> lists = new ArrayList<Student>();
long begin = System.currentTimeMillis();
for (int i=1;i<10;i++) {
for (int j = 1; j < 1000; j++) {
Student stu = new Student();
stu.setNumber(j);
stu.setName("周星星");
stu.setUniversity("西南财经大学");
stu.setBrother("周正华");
stu.setJob("PM工程师");
stu.setLink("www.jnshu.com");
stu.setTarget("不入IT誓不还");
Long s = Long.valueOf(new Date().getTime());
stu.setCreateTime(s);
stu.setUpdateTime(s);
stu.setQq("1131023043");
lists.add(stu);
}
System.out.println("......1");
}
try{
studentService.insert(lists);
}
catch (Exception e){
e.printStackTrace();
}

long end = System.currentTimeMillis();
logger.info("插入100万条数据耗时:" + (end - begin));
}
});
//开始等待,主线程挂起
   latch.await();
}

但是报错,明天看下Mybatis映射的关系。

明天计划的事情:明天做深度思考
遇到的问题: 绑定不到
收获:完成的事情内容


返回列表 返回列表
评论

    分享到