0

i am working on a board project where i need to display drawing points on the other side. I have 2 thread running parallel where one adds into blocking received from socket like this

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();
        }
    }
}

I am able to successfully put values returned from server.

and code to read from blocking queue from another thread

 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();
                            }
                    }
                }
            };

The problem is when i receive a string from server and is being parsed it is interrupted when a new string is received. I cannot block "receiverQueue.put(parsedDataString);" because i will miss points from server. How to block or wait "receiverQueue.take()" loop until my string is totally parsed.

EDIT: my whole reading code

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();
                                }

                            }
                        }
        };
Prakash
  • 357
  • 2
  • 14
  • 1
    Why is your client thread interrupted when a new string is received? – Craig Otis Feb 01 '16 at 13:15
  • @CraigOtis in my another thread i am reading from receiverQueue using receiverQueue.take() which throws InterruptedException. – Prakash Feb 01 '16 at 13:19
  • You have the queues for the purpose of not missing something...how do you know it's interrupted? Do you have an exception? – mattiash Feb 01 '16 at 13:19
  • @mattiash assuming your question is for receiverQueue.take(). i don't receive any exception while reading from queue. i've added catch(InterruptedException ex) because android studio suggested me. – Prakash Feb 01 '16 at 13:21
  • @user3036123, what interrupts it? You press Ctrl+C? Or do you have other relevant code not posted yet? – Gavriel Feb 01 '16 at 13:21
  • @Gavriel I removed parsing code from above code because it was big. – Prakash Feb 01 '16 at 13:25
  • 1
    If it throws an InterruptedException, then it's relevant pars should be put back IMHO – Gavriel Feb 01 '16 at 13:26
  • Post the parsing code...it might shed some light on it. – mattiash Feb 01 '16 at 13:28
  • @mattiash posted my parsing code – Prakash Feb 01 '16 at 13:49
  • The `take()` method _does_ have the potential to throw an InterruptedException _if_ the thread waiting on the method call is interrupted. Do you actually see this stack trace? Simply being required to catch a checked exception doesn't mean it's _being_ thrown. – Craig Otis Feb 01 '16 at 14:00
  • @CraigOtis no i have never seen this stacktrace. i think i've done mistaken in catching. But will this solve the issue ? – Prakash Feb 01 '16 at 14:03
  • Are you calling the onPause/onResume methods in your Activity directly? – mattiash Feb 01 '16 at 14:06
  • @mattiash, onPause & onResume is commented. sorry if not in code – Prakash Feb 01 '16 at 15:47

0 Answers0