gpt4 book ai didi

android - 3 个线程 + 3 个处理程序不起作用

转载 作者:行者123 更新时间:2023-11-30 03:56:51 24 4
gpt4 key购买 nike

我在每个线程中有 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();
}

这可能是完全错误的吗?

最佳答案

使用

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个runnable;阅读有关线程池的教程 http://www.vogella.com/articles/JavaConcurrency/article.html , http://www.caveofprogramming.com/java/java-multithreading-thread-pools-video-tutorial-part/

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

关于android - 3 个线程 + 3 个处理程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13209783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com