use*_*184 -4 java multithreading android
当主用户线程调用另一个这样的线程中的一系列方法时,我有点困惑
在主线程中:
otherThread.callMethod();
otherThread.callMethod();
otherThread.callMethod();
otherThread.callMethod();
otherThread.callMethod();
otherThread.callMethod();
otherThread.callMethod();
Run Code Online (Sandbox Code Playgroud)
而callMethod在另一个线程是从这样的套接字写入和读取:
mmOutStream.write(buffer);
mmOutStream.flush();
while (!finished) {
if (mmInStream.available() > 0)
if ((char) (c = (byte) mmInStream.read()) != 'x') {
responseBuffer[responseBufferLen++] = c;
} else {
finished = true;
}
}
Run Code Online (Sandbox Code Playgroud)
这显然是一种阻塞方案 - 但是在按顺序调用这些方法时,不会阻止主用户线程.
我的意思是阻止所有按钮仍然反应,用户可以毫不拖延地与屏幕交互.怎么可能?
不在线程上调用方法.它们被称为对象(或类,对于静态方法).对象可以是类型Thread,但在类型对象上Thread调用方法与在任何其他类型的对象上调用方法完全相同.该方法仍由当前线程执行.
| 归档时间: |
|
| 查看次数: |
201 次 |
| 最近记录: |