我怎么控制线程个数呢?

jige_hanhan 2007-09-19 01:44:59
class A extends Thread{

public void run() {
处理代码...........
}
}

--------------------------------------------
class B {
main(){
for(无限循环){

创建A线程并启动线程

}

}
}
------------------------------------------------
我应该怎么控制A的线程个数呢?(当线程数 大于最大数时,for停止,当线程数小于最大数时,for继续创建线程并启动!)
控制是写在A中呢,还是B中呢?
怎么控制呢?
...全文
833 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
jige_hanhan 2007-09-25
  • 打赏
  • 举报
回复
搞定了,是我自己把建的池子方到递归里面了
jige_hanhan 2007-09-24
  • 打赏
  • 举报
回复
ChDw ,问你一下pool.setMaxActive(1);为什么该成pool.setMaxActive(20)后,我在用Thread.activeCount得到的是几百呢?
我用pool.setMaxActive(10)后,Thread.activeCount得到值不大于20!
djy1135 2007-09-19
  • 打赏
  • 举报
回复
控制线程的最爽操作就是回调 ^_^
http://community.csdn.net/Expert/TopicView3.asp?id=5752824
oM落叶Mo 2007-09-19
  • 打赏
  • 举报
回复
int count=0;

while(true){
if(count<10){//假设你要创建10个线程
A a=new A(); //假设你的线程继承Thread
a.start();
count++;
}
这种写法有问题,如果中间有线程销毁啦,你的COUNT就不准确啦,实际线程和你要的就不符啦可以该为


while(true){
if(Thread.activeCount()<10){//假设你要创建10个线程
A a=new A(); //假设你的线程继承Thread
a.start();
}
javafreshfish 2007-09-19
  • 打赏
  • 举报
回复
preferme() 用的方法明白了,
lky5387(风尘浪子) 1.5的就是简单啊,
liky5387 2007-09-19
  • 打赏
  • 举报
回复
static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(initThreadNum,
initThreadNum, _THREAD_KEEP_ALIVE_TIME, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(_QUEUE_LENGTH),
new ThreadPoolExecutor.AbortPolicy());
ljmat427 2007-09-19
  • 打赏
  • 举报
回复
mark
LoveJava520 2007-09-19
  • 打赏
  • 举报
回复
冰思雨 2007-09-19
  • 打赏
  • 举报
回复
我刚才编了一个简单点的方法,测试了一下还可以。楼主要是用JDK1.5,不妨用用里面的线程池来管理,比较方便。下面是我的代码,没有使用线程池,因为不知道楼主的JDK版本。
public class B {

/**
* @param args
*/
public static void main(String[] args) {
int size = 20;
List pool = new ArrayList();
while(true){
if(isPoolFull(pool,size)){
try {Thread.sleep(1);} catch (InterruptedException e) {}
}else{
A a = new A();
pool.add(a);
a.start();
}
}
}
public static boolean isPoolFull(List pool,int poolSize){
int size = pool.size();
for(int i=0;i<size;i++){
A a = (A)pool.get(i);
if(!a.isAlive()){
pool.remove(i);i--;size--;
}
}
if(size>=poolSize)return true;
else return false;
}

}
class A extends Thread {
public void run(){
try {
sleep(5000);
System.out.println(this.getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
sharelist 2007-09-19
  • 打赏
  • 举报
回复
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author admin
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ThreadPoolTest implements Callable {
private int index;
/**
* @param index
*/
public ThreadPoolTest(int index) {
this.index = index;
}
/* (non-Javadoc)
* @see java.util.concurrent.Callable#call()
*/
public Object call() throws Exception {
int sec=(int)Math.floor(Math.random()*15);
System.out.println("Thread "+index+" start!");
System.out.println("Thread "+index+" will run "+sec+" second...");
Thread.sleep(sec*1000);
System.out.println("Thread "+index+" finished!");
return null;
}

public static void main(String[] args) {
ExecutorService pool=Executors.newFixedThreadPool(5);
for (int i = 0; i < 30; i++) {
ThreadPoolTest tt=new ThreadPoolTest(i+1);
pool.submit(tt);
}
pool.shutdown();
}
}
joejoe1991 2007-09-19
  • 打赏
  • 举报
回复
用同步可以做到
jige_hanhan 2007-09-19
  • 打赏
  • 举报
回复
1.4没有直接的类,我一般借用Apache的ObjectPool
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;


GenericObjectPool pool = new GenericObjectPool();
pool.setFactory(new PoolableObjectFactory() {

public void activateObject(Object arg0) throws Exception {
}

public void destroyObject(Object arg0) throws Exception {
}

public Object makeObject() throws Exception {
return new Object();
}

public void passivateObject(Object arg0) throws Exception {
}

public boolean validateObject(Object arg0) {
return true;
}
});
pool.setMaxActive(1);


for(无限循环){
Object lock = pool.borrowObject();
new A(pool, lock).start();//A在run的finally中执行pool.returnObject(lock);
}


这样就保证最大线程数
-------------------------------------
我的class A 的构造函数中需要传参数呀!
即new A(pra1,pra2)
new A(pool, lock).start();这句话怎么写呢?
A在run的finally中执行pool.returnObject(lock);是什么意思呢?
hzalan 2007-09-19
  • 打赏
  • 举报
回复
哇,强~!!学习
jige_hanhan 2007-09-19
  • 打赏
  • 举报
回复
ChDw(米)
你真强啊!
那么我在是不是还要下载包含这个类的jar包呀
那个包呢!
谢谢呀!ChDw(米)
ChDw 2007-09-19
  • 打赏
  • 举报
回复
1.4没有直接的类,我一般借用Apache的ObjectPool
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;


GenericObjectPool pool = new GenericObjectPool();
pool.setFactory(new PoolableObjectFactory() {

public void activateObject(Object arg0) throws Exception {
}

public void destroyObject(Object arg0) throws Exception {
}

public Object makeObject() throws Exception {
return new Object();
}

public void passivateObject(Object arg0) throws Exception {
}

public boolean validateObject(Object arg0) {
return true;
}
});
pool.setMaxActive(1);


for(无限循环){
Object lock = pool.borrowObject();
new A(pool, lock).start();//A在run的finally中执行pool.returnObject(lock);
}


这样就保证最大线程数
jige_hanhan 2007-09-19
  • 打赏
  • 举报
回复
ExecutorService pool = Executors.newFixedThreadPool(100);
for(无限循环){
//创建A线程并启动线程
pool.execute(new A());
}

是jdk1.5的呀!1.4怎么办呢?
hzalan 2007-09-19
  • 打赏
  • 举报
回复
单独启动一个线程不停的检测线程数...这个线程要一直开着...
whiel(true){
if(count<10){//假设你要创建10个线程
A a=new A(); //假设你的线程继承Thread
a.start();
count++;
}

再你关掉一个线程的地方使:
count--;

之后就是count的问题,如何确保count一致性.

ChDw 2007-09-19
  • 打赏
  • 举报
回复
写错
ExecutorService pool = Executors.newFixedThreadPool(100);
for(无限循环){
//创建A线程并启动线程
pool.execute(new A());
}

当然这个无限循环要有一定数量,否则会创建过多的A而导致OutOfMemory的
ChDw 2007-09-19
  • 打赏
  • 举报
回复
for(无限循环){

创建A线程并启动线程
ExecutorService pool = Executors.newFixedThreadPool(100);
pool.execute(new A());
}

这样就只会100个线程在执行,不会更多
jige_hanhan 2007-09-19
  • 打赏
  • 举报
回复
那该用什么实现呢?兄弟!
加载更多回复(10)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧