过多线程错误异常 [英] TOO MANY THREADS ERROR EXCEPTION
问题描述
我面临的一个问题,同时使黑莓的应用程序,我有高达7 threds调用,它的每一个从服务器下载一个音频,它工作正常,但是当我开始我的应用程序的两倍,则已经发生了未捕获的异常太多的线程错误异常,所以,让我知道我怎么能解决这个问题。
i am facing a problem while making an application of Blackberry that i have upto 7 threds call, of which each downloads an audio from the Server and it works fine but when i start my application twice then an uncaught exception has been occurred that "TOO MANY THREADS ERROR EXCEPTION", So, let me know that how i can solve this problem.
推荐答案
我想,而不是开始7线程使用一个线程。
1.创建一个TaskWorker类
i think instead of starting 7 threads use single thread. 1. create a TaskWorker class
public class TaskWorker implements Runnable {
private boolean quit = false;
private Vector queue = new Vector();
public TaskWorker() {
new Thread(this).start();
}
private Task getNext() {
Task task = null;
if (!queue.isEmpty()) {
task = (Task) queue.firstElement();
}
return task;
}
public void run() {
while (!quit) {
Task task = getNext();
if (task != null) {
task.doTask();
queue.removeElementAt(task);
} else {// task is null and only reason will be that vector has no more tasks
synchronized (queue) {
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public void addTask(Task task) {
synchronized (queue) {
if (!quit) {
queue.addElement(task);
queue.notify();
}
}
}
public void quit() {
synchronized (queue) {
quit = true;
queue.notify();
}
}
}
2。创建一个抽象类的任务。
2. create a abstract Task class
public abstract class Task {
abstract void doTask();
}
3。现在创建任务。
3. now create task.
public class DownloadTask extends Task{
void doTask() {
//do something
}
}
4。而这个任务添加到taskworker线程
4. and add this task to the taskworker thread
TaskWorker taskWorker = new TaskWorker();
taskWorker.addTask(new DownloadTask());
这篇关于过多线程错误异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!