我正在进行一个电路板项目,我需要在另一侧显示绘图点。我有2个并行运行的线程,其中一个添加到从这个
接收的阻塞中public class ClientThread implements Runnable {
@TargetApi(Build.VERSION_CODES.KITKAT)
public void run() {
try {
while (true) {
String dataString = "";
try {
byte[] messageByte = new byte[1024*2];
inputStream.read(messageByte);
dataString = new String(messageByte);
final String parsedDataString = dataString.trim();
if(!Objects.equals(parsedDataString.toString(), "")) {
receiverQueue.put(parsedDataString);
//inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
我能够成功地放置从服务器返回的值。
和从另一个线程中阻塞队列读取的代码
work = new Runnable() {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void run() {
while (true) {
Object queueVal = null;
try {
queueVal = receiverQueue.take();
// parse string and draw on canvas
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
问题是当我从服务器收到一个字符串并且正在被解析时,它会在收到新字符串时被中断。我无法阻止“receiverQueue.put(parsedDataString);”因为我会错过服务器的积分。 如何阻止或等待“receiverQueue.take()”循环,直到我的字符串被完全解析。
编辑:我的整个阅读代码
work = new Runnable() {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void run() {
while (true) {
Object queueVal = null;
try {
queueVal = receiverQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
if (json != null) {
JSONObject json = new JSONObject(queueVal.toString());
try {
JSONArray jArray = json.getJSONArray("jsonobj");
String[] points = jArray.getJSONObject(0).getString("points").split("-");
Boolean isCler = Boolean.parseBoolean(jArray.getJSONObject(1).getString("isClear"));
String strokeColor = jArray.getJSONObject(2).getString("strokeColor");
Boolean scrolling = Boolean.parseBoolean(jArray.getJSONObject(3).getString("isScrolling"));
final float scrollpos = Float.parseFloat(jArray.getJSONObject(4).getString("scrollPosition"));
String imageName = jArray.getJSONObject(5).getString("imageName");
boolean erasing= Boolean.parseBoolean(jArray.getJSONObject(6).getString("isErasing"));
isPointerEnabled = Boolean.parseBoolean(jArray.getJSONObject(7).getString("isPointerEnabled"));
String rectcoord = jArray.getJSONObject(8).getString("rectangle");
if(rectcoord.equals("null")) {
isRectangle = false;
}
else {
isRectangle = true;
int left = (int) virtToLocal(Float.parseFloat(rectcoord.split(",")[0]));
int top = (int) virtToLocalH(Float.parseFloat(rectcoord.split(",")[1]));
int right = (int) virtToLocal(Float.parseFloat(rectcoord.split(",")[2]));
int bottom = (int) virtToLocalH(Float.parseFloat(rectcoord.split(",")[3]));
rect = new Rect(left, top, right, bottom);
path_view.drawRectangle(left, top, right, bottom);
}
if (isCler) {
paths.clear();
pathX.clear();
pathY.clear();
onPause();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
path_view.invalidate();
onResume();
}
});
}
else {
if (isPointerEnabled) {
if (!isPointerEnabled) {
isPointerEnabled = true;
}
} else {
if (isPointerEnabled) {
isPointerEnabled = false;
}
}
if (isRectangle) {
System.out.println("RECTANGLE");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
isRectangle = true;
path_view.invalidate();
}
});
}
else {
if (isRectangle) {
isRectangle = false;
}
}
try {
//`colorCode = strokeColor;
if (!strokeColor.equals(colorCode)) {
if (colorCode.startsWith("#")) {
colorChanged(strokeColor);
}
}
} catch (Exception ex) {
System.out.println("In Ex " + ex.getMessage());
// colorCode = "#000000";
//colorChanged(colorCode);
}
if (erasing) {
if (!isErasing) {
startErasing(20);
isErasing = true;
}
}
else {
//FIXME: disable eraser mode
if (isErasing) {
System.out.println("Disabled ");
stopErasing(3);
isErasing = false;
}
}
if (scrolling) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
previousScrollPosition = (int)scrollpos;
scrollView.scrollTo(0, previousScrollPosition);
} catch (Exception ex) {
scrollView.scrollTo(0, previousScrollPosition);
}
}
});
} else {
for (String point : points) {
try {
if (point.split(",").length > 1) {
Float x = parseFloat(point.split(",")[0]);
Float y = parseFloat(point.split(",")[1]);
if (!redraw) {
redraw = true;
}
if (x == 0.0F) {
pathX.add(x);
pathY.add(y);
} else {
pathX.add(virtToLocal(x));
pathY.add(virtToLocalH(y));
}
}
} catch (Exception ed) {
ed.printStackTrace();
}
}
try {
//Redraw();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
System.out.println("POSDS" + pathY.size());
if (pathX.size() > 0 && pathY.size() > 0) {
onPause();
path_view.draw(pathX, pathY);
pathX.clear();
pathY.clear();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
};