同时运行线程太多,报内存等异常或错误 ,哈哈,分组队列就可以解决问题

本文介绍了一种基于线程组实现线程池的方法,并通过限制最大线程数来控制并发量,确保程序稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天移动电话面试那头问的一个问题,不过面试官的技能才一般,估计他还不懂怎么做,我现在把这个过程的代码贴一下

思路:

1、设置一个最大允许线程数量,首先是要通过你自身的机器承压能力和程序运行复杂情况来设定的

2、利用线程组来获取目前运行的线程数量



import java.util.LinkedList;
import java.util.Queue;


public class ThreadGroudTest {
private static final ThreadGroup tg = new ThreadGroup("tg");
private static final int maxthread=3;//最大允许线程数量
private static final int num = 100000;//模拟跑程序
public static void main(String[] args) {
//添加队列
Queue<Thread> queue = new LinkedList<Thread>();
queue.add(Thread1(tg));
queue.add(Thread2(tg));
queue.add(Thread3(tg));
queue.add(Thread4(tg));
queue.add(Thread5(tg));
queue.add(Thread6(tg));
queue.add(Thread7(tg));
queue.add(Thread8(tg));
queue.add(Thread9(tg));
queue.add(Thread10(tg));
Thread thread;
int i=0;
while((thread=queue.poll())!=null){
thread.start();//运行线程
while(true){
if(tg.activeCount()<=maxthread){//判断是否超过最大值  
break;
}else{
System.out.println("等待前..."+System.currentTimeMillis());
waits();
System.out.println("等待后..."+System.currentTimeMillis());
i++;
System.out.println("超过了,需要等待!"+i+"次");
}
}
}  
}

private static void waits(){
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}


public static Thread Thread1(ThreadGroup tg){
return new Thread(tg,new Runnable(){
public void run() {
for(int i=0;i<num;i++){
//System.out.println("thread1:"+i);
}

}
});
}
public static Thread Thread2(ThreadGroup tg){
return new Thread(tg,new Runnable(){
public void run() {
for(int i=0;i<num;i++){
//System.out.println("thread2:"+i);
}

}
});
}
public static Thread Thread3(ThreadGroup tg){
return new Thread(tg,new Runnable(){
public void run() {
for(int i=0;i<num;i++){
//System.out.println("thread3:"+i);
}
}
});
}public static Thread Thread4(ThreadGroup tg){
return new Thread(tg,new Runnable(){
public void run() {
for(int i=0;i<num;i++){
//System.out.println("thread4:"+i);
}

}
});
}public static Thread Thread5(ThreadGroup tg){
return new Thread(tg,new Runnable(){
public void run() {
for(int i=0;i<num;i++){
//System.out.println("thread5:"+i);
}
}
});
}public static Thread Thread6(ThreadGroup tg){
return new Thread(tg,new Runnable(){
public void run() {
for(int i=0;i<num;i++){
//System.out.println("thread6:"+i);
}
}
});
}public static Thread Thread7(ThreadGroup tg){
return new Thread(tg,new Runnable(){
public void run() {
for(int i=0;i<num;i++){
//System.out.println("thread7:"+i);
}
}
});
}public static Thread Thread8(ThreadGroup tg){
return new Thread(tg,new Runnable(){
public void run() {
for(int i=0;i<num;i++){
//System.out.println("thread8:"+i);
}
}
});
}public static Thread Thread9(ThreadGroup tg){
return new Thread(tg,new Runnable(){
public void run() {
for(int i=0;i<num;i++){
//System.out.println("thread9:"+i);
}
}
});
}public static Thread Thread10(ThreadGroup tg){
return new Thread(tg,new Runnable(){
public void run() {
for(int i=0;i<num;i++){
//System.out.println("thread10:"+i);
}
}
});
}


}




运行结果是

等待前...1493968016581
等待后...1493968016681
超过了,需要等待!1次
等待前...1493968016681
等待后...1493968016782
超过了,需要等待!2次


适当调整模拟跑程序数量,有时候可能不输出,有时候还可能输出

等待前...1493968300130
等待后...1493968300231
超过了,需要等待!1次


说明程序是OK的l

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

英雄汉孑

每一滴心意都是对知识分享的赞美

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

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

打赏作者

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

抵扣说明:

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

余额充值