TOO MUCH THREAD EXCEPTION ERROR

I ran into a problem when creating a Blackberry application, I have up to 7 threds of calls, each of which downloads audio from the server, and it works fine, but when I run my application twice, an uncaught exception occurred, "TOO MANY THREADS EXCEPTION ERRORS", So let me know how I can solve this problem.

+3
multithreading exception blackberry
source share
2 answers

I think, instead of starting 7 threads, use one 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. create an abstract task class

 public abstract class Task { abstract void doTask(); } 

3. Now create the task.

 public class DownloadTask extends Task{ void doTask() { //do something } } 

4. and add this task to the workflow

 TaskWorker taskWorker = new TaskWorker(); taskWorker.addTask(new DownloadTask()); 
+5
source share

If this happens when you run the RESTART app, it means you have to have some zombies ... Are you sure you have joined all your topics?

+1
source share

All Articles