多线程_Day01

多线程

程序 进程 线程

  • 程序是为了完成特定的任务,用某种语言编写的一组指令的集合,通常指一段静态的代码
  • 进程是程序的一次成型过程或者是正在运行的程序,是一个动态的过程,有它自身的产生,存在和消亡的过程,称为生命周期
  • 线程是进程的细化,是一个进程内部的一个执行路径,

多线程的优点

  • 提高应用程序的响应
  • 提高计算机系统CPU的利用率
  • 改善程序结构,将即长有复杂的进程分为多个线程,独立运行,有利于理解和修改

线程的创建和使用

  • 第一种创建方式
    1. 创建一个继承Thread类的子类
    2. 重写父类Thread类run()
    3. 创建Thread类的子类对象
    4. 通过创建的子类对象调用start()
public class ThreadTest {
    public static void main(String[] args) {
        SubTest subTest = new SubTest();
        subTest.start();
    }
}

class SubTest extends Thread{
    @Override
    public void run() {
        for (int i = 2; i < 100; i++) {
            if (i % 2 == 0){
                System.out.println(Thread.currentThread().getName() +" "+ Thread.currentThread().getState() +" i = " + i + "***");
            }
        }
    }
}

  • 第二种创建方式
    1. 创建一个实现了Runnable接口的类
    2. 实现类去实现Runnable接口中的抽象方法run()
    3. 创建实现类的对象
    4. 将此对象作为参数传递到Thraed类的构造器中,创建THread对象类
    5. 通过Thread类的对象调用start()方法
public class Test {
    public static void main(String[] args) {
        sub sub1 = new sub();
        new Thread(sub1).start();
        /*
        Thread的构造器
        public Thread(Runnable target) {
            init(null, target, "Thread-" + nextThreadNum(), 0);
        }
        Thread的run方法
        public void run() {
            if (target != null) {
                target.run();
            }
        }
         */
    }
}

class sub implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("i = " + i);
        }
    }
}
  • 开发中优先选择Runnable接口的方式
    1. 实现的方式没有类的单继承的限制的局限性
    2. 更适合来处理多个线程有共享数据的情况
    3. 相同点: Thread也实现了Runnable
    4. 相同点: 都需要重写run()方法

多线程使用注意点

  • 不能直接调用run()的方式启动线程
  • 不能连续调用一个实例话子对象调用两次start(),会报错误,如果真的想再同时执行就再实例话子对象用来start()

Thread的常用方法

  • start() 启动当前线程 调用当前线程的run
  • run() 通常需要重写Thread类的此方法,将需要执行的操作声明在此方法中
  • currentThread() 静态方法,返回当前代码的线程
  • setName() 设置当前线程的名字
  • getName() 获取当前线程的名字
  • yield() 释放当前cpu的执行权,让别的线程上,自己再等
  • join() 在线程a中调用线程b的join方法,此时线程a就进入阻塞状态,直到线程b完全执行结束之后线程a才结束阻塞状态
  • stop() 已过时 当执行此方法时,强制结束当前线程
  • sleep(long millis) 让当前献策很难过"睡眠"指定的millis毫秒,在指定的millis毫秒内,当前线程是阻塞状态
  • isAlive() 判断当前线程是否存活

线程的优先集

  • Thread的优先级等级
    public final static int MIN_PRIORITY = 1;
    public final static int NORM_PRIORITY = 5;
    public final static int MAX_PRIORITY = 10;
  • 获取线程的优先级(默认是5)
    getPriority()
  • 设置线程的优先级(newPriority的范围是[1,10])
    setPriority(int newPriority)
  • 高优先级的线程抢占低优先级的cpu的执行权,只是从概率上讲,高优先级的高概率被执行,并不是值高优先
public class Test {
    public static void main(String[] args) {
        HelloThread helloThread = new HelloThread("线程二");
//        helloThread.setName("线程一");
        helloThread.setPriority(10);
        helloThread.start();
        // 主线程
        Thread.currentThread().setName("主线程");
        Thread.currentThread().setPriority(1);
        System.out.println(Thread.currentThread().getName());
        for (int i = 0; i < 100; i++) {
            if (i % 2 ==1){
                System.out.println(Thread.currentThread().getPriority());
                System.out.println(Thread.currentThread().getName()+" : " + i);
            }
        }
    }
}

class HelloThread extends Thread{
    public HelloThread(){}
    public HelloThread(String threadname){
        super(threadname);
    }
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
//            try {
//                sleep(10);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            if (i % 2 ==0){
                System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().getPriority()+" : " + i);
            }
            if (i % 20 == 0){
                yield();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,没有找到关于ES多线程分组分页查询的信息。不过,ES支持多线程查询和分组分页查询,可以分别介绍一下。 1. ES多线程查询 ES支持多线程查询,可以通过设置线程池来控制并发查询的数量。以下是一个使用Python Elasticsearch库进行多线程查询的例子: ```python from elasticsearch import Elasticsearch from elasticsearch.helpers import parallel_bulk from multiprocessing import Pool es = Elasticsearch() def process_data(data): # 处理数据的函数 pass def index_data(data): # 索引数据的函数 pass def run_query(query): # 运行查询的函数 pass def run_parallel_bulk(data): with Pool(processes=4) as pool: for success, info in parallel_bulk(es, data, index='my-index', chunk_size=1000, thread_count=4): if not success: print('A document failed:', info) ``` 在上面的例子中,我们使用了Python的multiprocessing库来创建一个进程池,然后使用Elasticsearch的parallel_bulk函数来并发地索引数据。 2. ES分组分页查询 ES支持分组分页查询,可以使用Elasticsearch的search方法来实现。以下是一个使用Python Elasticsearch库进行分组分页查询的例子: ```python from elasticsearch import Elasticsearch es = Elasticsearch() def search_data(): query = { "size": 0, "aggs": { "group_by_field": { "terms": { "field": "my_field", "size": 10 }, "aggs": { "group_by_date": { "date_histogram": { "field": "my_date_field", "interval": "day" }, "aggs": { "sum_by_field": { "sum": { "field": "my_sum_field" } } } } } } } } res = es.search(index="my-index", body=query) return res ``` 在上面的例子中,我们使用了Elasticsearch的聚合功能来进行分组分页查询。我们首先按照my_field字段进行分组,然后按照my_date_field字段进行日期直方图聚合,最后对my_sum_field字段进行求和聚合。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

临水而愚

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

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

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

打赏作者

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

抵扣说明:

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

余额充值