java 多个常驻实例_Java实现监控多个线程状态的简单实例

实例如下:

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.Executor;

import java.util.concurrent.Executors;

/**

* 测试监控类

*

* @author

*

*/

public class WatchThread {

/**

* 测试函数

*

* @throws InterruptedException

*/

public void testThread() throws InterruptedException {

int threadNum = 10;

// 初始化countDown

CountDownLatch threadSignal = new CountDownLatch(threadNum);

// 创建固定长度的线程池

Executor executor = Executors.newFixedThreadPool(threadNum);

for (int i = 0; i < threadNum; i++) { // 开threadNum个线程

Runnable task = new TestThread(threadSignal);

// 执行

executor.execute(task);

}

threadSignal.await(); // 等待所有子线程执行完

// do work

System.out.println(Thread.currentThread().getName() + "+++++++结束.");

}

/**

* 测试函数

*/

public static void main(String[] args) throws InterruptedException {

WatchThread test = new WatchThread();

test.testThread();

}

/**

*

* @author jill

*

*/

private class TestThread implements Runnable {

private CountDownLatch threadsSignal;

public TestThread(CountDownLatch threadsSignal) {

this.threadsSignal = threadsSignal;

}

public void run() {

System.out.println(Thread.currentThread().getName() + "开始...");

// do shomething

System.out.println("开始了线程::::" + threadsSignal.getCount());

// 线程结束时计数器减1

threadsSignal.countDown(); //这句代码 建议放在 finally里执行

System.out.println(Thread.currentThread().getName() + "结束. 还有"

+ threadsSignal.getCount() + " 个线程");

}

}

}

以上这篇Java实现监控多个线程状态的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持聚米学院。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以通过多线程来获取数据库数据。一种常见的做法是使用线程池来管理线程,同时启动多个消费者线程来处理数据库查询和结果的处理。 首先,创建一个生产者线程来执行数据库查询操作,将查询结果存放在队列中。这里可以使用Java线程池来模拟生产者线程的并发执行。 接下来,在主线程中同时启动多个消费者线程,消费者线程从队列中获取查询结果并进行相应的处理。消费者线程常驻线程,当队列中没有数据时就进入阻塞状态等待数据的到来。 具体的代码示例如下: ``` import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class MainThread { public static void main(String[] args) { // 创建线程池 ExecutorService exec = new ThreadPoolExecutor(10, 20, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); for (int i = 0; i < 100; i++) { // 使用线程池模拟生产者生产数据 exec.execute(new ProducerThread()); } for (int i = 0; i < 2; i++) { // 启动两个消费者线程 new ConsumerThread().start(); } } } class ProducerThread extends Thread { public void run() { // 执行数据库查询操作 // 将查询结果存放在队列中 // ... } } class ConsumerThread extends Thread { public void run() { while (!this.isInterrupted()) { try { // 从队列中获取数据进行处理 // ... } catch (InterruptedException e) { e.printStackTrace(); } } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值