
Originally Posted by
kurteknikk
Yes but i don't want it to block, i just want to read all the data without blocking... And that can be achieved only using the available, so if the available doesn't work well in some certain phone model i've still got a problem...
You could have two threads. One sits reading from the socket (and blocking when there's no data). That way, you don't need to keep looping and polling to see if there's data. When data arrives, it get's queued to the second thread, which wakes up and processes it. This second thread looks something like:
Code:
public class Processor implements Runnable {
private boolean exit;
private Vector queue = new Vector();
public synchronized void add(Message m) {
queue.addElement(m);
notifyAll();
}
public synchronized void kill() {
exit = true;
queue.removeAllElements();
notifyAll();
}
public synchronized void run() {
while (!exit) {
while (!queue.isEmpty()) {
Message m = (Message) queue.firstElement();
queue.removeElementAt(0);
// process message here
}
// go to sleep until there's a message to process
try {
wait();
} catch (InterruptedException ie) {
// ignore
}
}
}
}