1

我在每个线程中有 3 个线程和 1 个处理程序。但它只适用于一个,其他 2 个处理程序完全清除 textview。

代码:

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

这可能是完全错误的吗?

4

1 回答 1

1

利用

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 
    }

内部可运行 3 个;阅读有关 ThreadPools的 教程http://www.vogella.com/articles/JavaConcurrency/article.html,http://www.caveofprogramming.com/java/java-multithreading-thread-pools-video-tutorial-part/

或用同步块{}包装你的 run() 目标;

于 2012-11-03T14:05:09.603 回答