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

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

时间:2022-11-04 22:02:21浏览次数:40  
标签:Thread start 线程 println new 多线程 连接 out


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

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

  • 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是什么?它的优势?

1、Spring Boot是由某团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。
SSM的配置太多了,太繁杂了,于是SpringBoot来解决这个问题。

2、Spring Boot 的最大的优势是“约定优于配置“。“约定优于配置“是一种软件设计范式,开发人员按照约定的方式来进行编程,可以减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性。


标签:Thread,start,线程,println,new,多线程,连接,out
From: https://blog.51cto.com/u_14604401/5824838

相关文章

  • mysql连接超时的属性设置
    mysql连接超时的属性设置2022-10-2611:09:54.128[http-nio-6788-exec-5]ERRORo.s.t.i.TransactionAspectSupport#completeTransactionAfterThrowing[line:525]-App......
  • 电脑连接手机热点 | 如何手动设置固定ip | 手机访问电脑资源
    手机开启热点,电脑连接后的ip一般是DHCP分配好了的,但是如果我们想要自己手动设置ip呢?打开wifi属性这里动态分配的ip是192.168.10.69那我们想设置成192.168.10.1呢如......
  • 使用多线程可能带来什么问题?
    并发编程的目的就是为了能提高程序的执行效率提高程序运行速度,但是并发编程并不总是能提高程序运行速度的,而且并发编程可能会遇到很多问题,比如:内存泄漏、上下文切换、死锁还......
  • mysql 5 和 8 连接配置
    前言:mysql目前最常用的就是5和8两个版本,但连接配置有一些区别,以下列出两个模板备用mysql5连接配置spring:datasource:driver-class-name:com.mysql.jd......
  • 动态线程池使用
    1、复制到项目lib下2、File->ProjectStructure...->Modeles ->Dependencies找到目录下的jar包引用进去3、配置pom.xml<!--动态线程池配置begin--><dependency>......
  • LINQ的左连接、右连接、内连接
    1、左连接:varLeftJoin=fromempinListOfEmployeesjoindeptinListOfDepartmentonemp.DeptIDequalsdept.IDintoJoinedEmpDeptfromdeptinJoinedEmpDept......
  • 10-jmeter-初识负载测试的概念:逐步加压(阶梯式线程组)
    一、阶梯式线程组:jp@gc-SteppingThreadGroup(deprecated)->设计场景1、安装插件->将jmeter-plugins-manager-1.3放在ext目录下2、启动jmeter->在JMeterPluginsMa......
  • 线程安全的单例模式
    懒汉式加方法锁publicclassSingleton{privatestaticSingletonsingleton=null;privateSingleton(){}publicstaticsynchronizedSingletongetIn......
  • 在不杀进程的前提下关闭一个 TCP Socket 连接
    使用netstat找到进程#netstat-antp|grep6789tcp001.1.1.1:599501.1.1.2:6789ESTABLISHED45059/ceph-fuse如上,进程pid为4505......
  • Java线程状态详解
    Java的每个线程都具有自己的状态,Thread类中成员变量threadStatus存储了线程的状态: privatevolatileintthreadStatus=0; 在Thread类中也定义了状态的枚举,共六......