天天看点

首选线程池,而不是多线程; 创建线程的方法; 存储过程和for循环插入数据; String字符串一般有什么方法?面向对象怎么理解?数据库排序?左连接 ?右连接?jQuery是什么?SpringBoot

首选线程池,而不是多线程

首选线程池,而不是多线程

/**

  • corePoolSize:线程长期为维持线程数 核心线程数,常用线程数
  • maximumPoolSize:线程数的上限,最大线程数
  • keepAliveTime:超过线程时长:60s
  • unit 时间单位
  • workQueue 阻塞队列
  • threadFactory 线程工厂 可以为线程创建时起个好名字
  • handler 拒绝策略 四种
  • 任务的排队队列:ArrayBlockingQueue是一个阻塞式的队列,一个基于数组的阻塞队列:先进先出,有界队列,队列不支持空元素
•  */
 private static ExecutorService executor = new ThreadPoolExecutor(10, 10,
 60L, TimeUnit.SECONDS, new ArrayBlockingQueue(10));      
//ex:2
 private static ThreadFactory threadFactory = new ThreadFactoryBuilder().build();
 private static ExecutorService executorService = new ThreadPoolExecutor(10, 10,
 60L, TimeUnit.SECONDS, new ArrayBlockingQueue(10),
 threadFactory, new ThreadPoolExecutor.AbortPolicy());      

创建线程的方法

创建线程的方法

通过实现 Runnable 接口;

通过继承 Thread 类本身;

通过 Callable 和 Future 创建线程。

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ReportApplication.class)
public class LocalCacheTest {
@Test
public void test() throws InterruptedException {
    RunnableDemo R1 = new RunnableDemo("11111");
    R1.start();
    R1.start();
    R1.start();
    R1.start();
    R1.start();
    R1.start();
    R1.start();
    R1.start();
}

//通过实现Runnable接口创建线程
class RunnableDemo implements Runnable {
    private Thread t;
    private String threadName;

    RunnableDemo(String name) {
        threadName = name;
        System.out.println("creating " + threadName);
    }


    @Override
    public void run() {
        System.out.println("Running " + threadName);
        for (int i = 4; i > 0; i--) {
            System.out.println("Thread: " + threadName + "," + i);
        }
        System.out.println("Thread " + threadName + " exiting.");
    }

    public void start() {
        System.out.println("Staring " + threadName);
        if (t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }


}


//通过实现Runnable接口创建线程
class DisplayMessage implements Runnable {

    private String message;

    public DisplayMessage(String message) {
        this.message = message;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println(message);
        }
    }
}

//继承Thread类创建线程
class GuessANumber extends Thread {
    private int number;
    public GuessANumber(int number) {
        this.number = number;
    }

    public void run() {
        System.out.println("继承Thread创建线程" + number);
    }
}

@Test
public void threadDemo() {

    System.out.println("Starting thread3...");
    Thread thread3 = new GuessANumber(27);
    thread3.start();
    try {
        thread3.join();
    } catch (InterruptedException e) {
        System.out.println("Thread interrupted");

    }

    System.out.println("Starting thread4...");
    Thread thread4 = new GuessANumber(75);
    thread4.start();

    Thread thread5 = new GuessANumber(5);
    thread5.start();

    Thread thread6 = new GuessANumber(6);
    thread6.start();

    Thread thread7 = new GuessANumber(7);
    thread7.start();

    Thread thread8 = new GuessANumber(8);
    thread8.start();

    System.out.println("main() is ending...");

}



//通过Callable和Future创建线程
class CallableThread implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        int i = 0;
        for (; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        return i;
    }
}

@Test
public void callableTest() {
    CallableThread ctt = new CallableThread();
    FutureTask<Integer> ft = new FutureTask<>(ctt);
    for (int i = 0; i < 100; i++) {
        System.out.println(Thread.currentThread().getName() + " 的循环变量i的值" + i);
        if (i == 20) {
            new Thread(ft, "有返回值的线程").start();
        }
    }
    try {
        System.out.println("子线程的返回值:" + ft.get());
    } catch (InterruptedException e) {
        log.error("e", e);
    } catch (ExecutionException e) {
        log.error("e", e);
    } catch (Exception e) {
        log.error("e", e);
    }
}
}      

存储过程和for循环插入数据

#删除存储过程

drop procedure callback

#设置存储过程
 create procedure callback()
 begin
 declare num int;
 set num = 100;
 while
 num < 300 DO
 insert into test-demo(merchant_id, shop_name, remark)
 values(concat(num), concat(“shop_name”, num), concat(“remark”, num));
 set num = num + 1;
 end while;
 end;      

#执行存储过程

call callback();

String字符串一般有什么方法?

1、构造方法

String(String …)

String(char[] …)

String(char[] …,int …)

2、获取长度

length()

3、获取str在字符串对象中第一次出现的索引

indexOf(String str)

4、从start开始,到end结束截取字符串。包括start,不包括end

String substring(int start,int end)

5、判断是否相同

equals(Object obj)

6、比较字符串的内容是否相同,忽略大小写

equalsIgnoreCase(… …)

7、把字符串转换为小写字符串

toLowerCase()

8、把字符串转换为大写字符串

toUpperCase()

9、去除字符串两端空格

trim()

面向对象怎么理解?

1、面向对象概念

其本质是以建立模型体现出来的抽象思维过程和面向对象的方法

面向对象:将功能封装进对象,强调具备了功能的对象

—》面向过程—》功能和行为

一切皆对象

打开关闭电脑是种行为,这过程是面向过程。

而电脑是对象,它有打开,上网,关闭的功能。

面向对象是一种思想,能让复杂问题简单化,程序员不需要了解具体的实现过程,只需要指挥对象去实现功能。

好比把对象的功能封装起来,我实例化对象,调用对象即可,它怎么实现什么功能让它去实现。

2、三大特性扩开来讲

继承–》。。。。

多态–》。。。。

封装–》。。。。

3、类与对象

人类是类。

小明是具体的人,是为对象。

4、扩展说面向对象其他特点

比如构造方法,static

数据库排序?左连接 ?右连接?

1、数据库排序

语法

select column_name,column_name from table_name order by column_name,column_name asc|desc

即select 列名 from 表名 order by 列名 asc|desc

用途

默认:升序

asc:指定列按升序排列

desc:指定列按降序排列

desc/asc :只对后方的第一个列名有效,其他不受影响,仍是默认的升序。

2、

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3BynD1XP-1662873402593)(1)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EGBws14Q-1662873402596)(2)]

jQuery是什么?

jQuery是前端重要的知识

jQuery 是一个 JavaScript 库。

jQuery 极大地简化了 JavaScript 编程。

简单来说jQuery 就是把一些js方法封装到库里面,应用jQuery可以使js的实现更加容易。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2iELyqwG-1662873419644)(3)]

把jQuery库引用到前端文件中如index.jsp

<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>      

SpringBoot是什么?它的优势?