1

I have 3 threads and 1 handler in each thread. But it only works one, the other 2 handlers clear the textview completely.

Code:

 Thread getServerStatus = new Thread() {                     
        public void run() {                                                                         
            Document doc;
            try {
                doc = dereference("my url");
                String text = doc.select("div#serverstatus").toString();
                Message msg = handler_serverstatus.obtainMessage();
                msg.obj = text;
                handler_serverstatus.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        Handler handler_serverstatus = new Handler()
        {
            public void handleMessage(Message msg)
            {
                 String text = (String)msg.obj;
                 TextView tv = (TextView)findViewById(R.id.server_status);
                 if(text.contains("online")) {
                     //tv.setTextColor(2);
                     tv.setText("online");
                 } else if(text.contains("offline")) {
                     tv.setText("offline");
                 } else {
                     tv.setText("Error #1");
                 }
            }

        };
    };

    Thread getPlayersOnline = new Thread() {                     
        public void run() {                                                                         
            Document doc;
            try {
                doc = dereference("my url");
                String text = doc.select("div#players_on").toString();
                Message msg = handler_players.obtainMessage();
                msg.obj = text;
                handler_players.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        Handler handler_players = new Handler()
        {
            public void handleMessage(Message msg)
            {
                 String text = (String)msg.obj;
                 TextView tv = (TextView)findViewById(R.id.players_online);
                 text = text.replace("<div id=\"players_on\">", "");
                 text = text.replace("</div>", "");
                 tv.setText(text);
            }

        };
    };

    Thread getPlayersMax = new Thread() {                     
        public void run() {                                                                         
            Document doc;
            try {
                doc = dereference("url");
                String text = doc.select("div#players_max").toString();
                Message msg = handler_players_max.obtainMessage();
                msg.obj = text;
                handler_players_max.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        Handler handler_players_max = new Handler()
        {
            public void handleMessage(Message msg)
            {
                 String text = (String)msg.obj;
                 TextView tv = (TextView)findViewById(R.id.players_max);
                 text = text.replace("<div id=\"players_max\">", "");
                 text = text.replace("</div>",  "");
                 tv.setText(text);
            }

        };
    };

    public void ButtonClick(View view) throws IOException {
        getServerStatus.start();
        getPlayersOnline.start();
        getPlayersMax.start();
    }

    private Document dereference(String uri) throws IOException {
        Connection connection = Jsoup.connect(uri);
        return connection.get();
    }

Is this maybe totally wrong?

Thomas Dignan
  • 7,052
  • 3
  • 40
  • 48
Phil
  • 943
  • 2
  • 6
  • 18

1 Answers1

1

Use

ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(1); // number of working thread 
Runnable getServerStatus = new Runnable() {
        @Override
        public void run() {
             Document doc;
                try {
                    doc = dereference("my url");
                    String text = doc.select("div#serverstatus").toString();
                    Message msg = handler_serverstatus.obtainMessage();
                    msg.obj = text;
                    handler_serverstatus.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

           Handler handler_serverstatus = new Handler()
            {
                public void handleMessage(Message msg)
                {
                     String text = (String)msg.obj;
                     TextView tv = (TextView)findViewById(R.id.server_status);
                     if(text.contains("online")) {
                         //tv.setTextColor(2);
                         tv.setText("online");
                     } else if(text.contains("offline")) {
                         tv.setText("offline");
                     } else {
                         tv.setText("Error #1");
                     }
                }

            };


    };
    Runnable getPlayersOnline = new Runnable() {
        @Override
         public void run() {                                                                         
                Document doc;
                try {
                    doc = dereference("my url");
                    String text = doc.select("div#players_on").toString();
                    Message msg = handler_players.obtainMessage();
                    msg.obj = text;
                    handler_players.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            Handler handler_players = new Handler()
            {
                public void handleMessage(Message msg)
                {
                     String text = (String)msg.obj;
                     TextView tv = (TextView)findViewById(R.id.players_online);
                     text = text.replace("<div id=\"players_on\">", "");
                     text = text.replace("</div>", "");
                     tv.setText(text);
                }

            };
    }

    Runnable getPlayersMax = new Runnable() {

          public void run() {                                                                         
                Document doc;
                try {
                    doc = dereference("url");
                    String text = doc.select("div#players_max").toString();
                    Message msg = handler_players_max.obtainMessage();
                    msg.obj = text;
                    handler_players_max.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            Handler handler_players_max = new Handler()
            {
                public void handleMessage(Message msg)
                {
                     String text = (String)msg.obj;
                     TextView tv = (TextView)findViewById(R.id.players_max);
                     text = text.replace("<div id=\"players_max\">", "");
                     text = text.replace("</div>",  "");
                     tv.setText(text);
                }

            };
    };
    newFixedThreadPool.submit(getServerStatus);
    newFixedThreadPool.submit(getPlayersOnline);
    newFixedThreadPool.submit(getPlayersMax); // submit all your targets
}

 public void ButtonClick(View view) throws IOException {
        newFixedThreadPool.shutdown();// shutdown and execute it 
    }

with 3 runnable inside; Read tutorial about ThreadPools http://www.vogella.com/articles/JavaConcurrency/article.html, http://www.caveofprogramming.com/java/java-multithreading-thread-pools-video-tutorial-part/

or wrap your run() targets with synchronized block{};

Yahor10
  • 2,123
  • 1
  • 13
  • 13
  • Dont really know what u mean :/ Should I paste my code from aboce into this `{}`? Eclipse brings many mistakes :o – Phil Nov 03 '12 at 14:12
  • big thanks to you! but eclipse says error at `.submit` :o and there was a `{` too much i think – Phil Nov 03 '12 at 14:36
  • sorry not `{` I mean the `}` after the 3 submits, but eclipse says error at `.submit` – Phil Nov 04 '12 at 07:18