线程池实践小记

一、ThreadPoolExecutor

ThreadPoolExecutor 是JDK自带的线程池。

主要使用的构造方法:

    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              RejectedExecutionHandler handler) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), handler);
    }

corePoolSize:线程核心参数
maximumPoolSize:最大线程数
keepAliveTime:非核心闲置线程存活时间
unit:时间的单位,时/分/秒之类
workQueue:线程池等待队列
handler:如果线程池等待队列满了,而且正在运行的线程数量大于maximumPoolSize,线程池执行的拒绝策略

二、实践

	public static void main(String[] args) throws InterruptedException {
		ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 3, 0, TimeUnit.MILLISECONDS,
				new LinkedBlockingDeque<>(2), new ThreadPoolExecutor.CallerRunsPolicy());

		Thread t1 = new Thread(()-> {
            System.out.println("第一个线程开始!" + Thread.currentThread().getName());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.execute(t1);
        Thread.sleep(100);

        Thread t2 = new Thread(()-> {
            System.out.println("第二个线程开始!" + Thread.currentThread().getName());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.execute(t2);
        Thread.sleep(100);

        Thread t3 = new Thread(()-> {
            System.out.println("第三个线程开始!" + Thread.currentThread().getName());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.execute(t3);
        Thread.sleep(100);

        Thread t4 = new Thread(()-> {
            System.out.println("第四个线程开始!" + Thread.currentThread().getName());
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.execute(t4);
        Thread.sleep(100);

        Thread t5 = new Thread(()-> {
            System.out.println("第五个线程开始!" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.execute(t5);
        Thread.sleep(100);

        Thread t6 = new Thread(()-> {
            System.out.println("第六个线程开始!" + Thread.currentThread().getName());
        });
        executor.execute(t6);
        Thread.sleep(100);

        Thread t7 = new Thread(()-> {
            System.out.println("第七个线程开始!" + Thread.currentThread().getName());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executor.execute(t7);
        Thread.sleep(100);

        Thread t8 = new Thread(()-> {
            System.out.println("第八个线程开始!" + Thread.currentThread().getName());
        });
        executor.execute(t8);

	}

执行结果:

第一个线程开始!pool-1-thread-1
第四个线程开始!pool-1-thread-2
第五个线程开始!pool-1-thread-3
第六个线程开始!main
第七个线程开始!main
第二个线程开始!pool-1-thread-3
第三个线程开始!pool-1-thread-2
第八个线程开始!pool-1-thread-1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Protobuf是一种高效的序列化协议,可以用于数据交换和数据存储。它的主要优势是大小小,速度快,可扩展性强。下面是使用Protobuf的一些小记: 1. 定义消息格式 首先,需要定义消息格式,以便Protobuf可以将数据序列化和反序列化。消息格式定义在.proto文件中,使用protobuf语言编写。例如,下面是一个简单的消息格式定义: ``` syntax = "proto3"; message Person { string name = 1; int32 age = 2; } ``` 这个消息格式定义了一个名为Person的消息,包含两个字段:name和age。 2. 生成代码 一旦消息格式定义好,就可以使用Protobuf编译器生成代码。编译器将根据消息格式定义生成相应的代码,包括消息类、序列化和反序列化方法等。可以使用以下命令生成代码: ``` protoc --java_out=. message.proto ``` 这将生成一个名为message.pb.javaJava类,该类包含Person消息的定义以及相关方法。 3. 序列化和反序列化 一旦生成了代码,就可以使用Protobuf序列化和反序列化数据。例如,下面是一个示例代码,将一个Person对象序列化为字节数组,并将其反序列化为另一个Person对象: ``` Person person = Person.newBuilder() .setName("Alice") .setAge(25) .build(); byte[] bytes = person.toByteArray(); Person deserializedPerson = Person.parseFrom(bytes); ``` 这个示例代码创建了一个Person对象,将其序列化为字节数组,然后将其反序列化为另一个Person对象。在这个过程中,Protobuf使用生成的代码执行序列化和反序列化操作。 以上是使用Protobuf的一些基本步骤和注意事项,希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值