问题描述
我有一个运行在循环中2个线程的应用程序。第1一个是在1秒的时间间隔更新的曲线图,第二个是更新在60秒的时间间隔另一个图表。第二个任务是花费很长的时间,因为它是quering一些服务器在互联网的3倍,可能并不总是可用,即使是它会占用到5-7s执行。
I have an app that runs 2 threads in loops. 1st one is updating a graph in 1s interval and the second one is updating another graph at 60s interval. The second task is taking a long time since it is quering some server in the internet 3 times that might not always be available and even if it is it will take up to 5-7s to execute.
正在发生的事情是,当我启动第二个线程将暂停第一个执行,这是不是我想要的,我想这两个同时运行。在这里,在Youtube的视频中可以看到应用程序运行的结果。
thread_updater1s正在运行一个绿色的曲线图,大的读出,并在角落里一个计时器,所以你很清楚地看到这档11秒。
What is happening is when I launch the second thread it will pause execution of the first one and that is not what I want, I wish both run concurrently. Here in the Youtube video you can see the results of the app running. http://youtu.be/l7K5zSWzlxI"thread_updater1s" is running a green graph, large readout, and a timer in the corner so you clearly see it stalls for 11 seconds.
1)首先这是为什么发生?如何解决它?
1)First of all why is that happening? how to fix it?
2)我知道,我可能不会启动线程正确的。我很难理解如何让东西在Java中的间隔循环运行,我的code一图/胎面正常工作。现在,当我在单独的线程2路我不知道为什么他们没有并发执行。
2)I'm aware that I might not launch the threads properly at all. I had hard time understanding how to make something to run in a interval loop in Java and my code worked fine for one graph/tread. Now when I have 2 loops in separate threads I don't know why they are not executing concurrently.
下面是code:
public class LoopExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
thread_updater1s.start();
thread_updater2.start();
}// end of onCreate
final Runnable r1s = new Runnable() {
public void run() {
do_1s_updates(); // those are very quick http calls to the local API server
} // to get data nessessary for some plot.
// They have 1s timeout as well but rarely timeout
};
final Runnable r2 = new Runnable() {
public void run() {
do_large_updates(); //This makes 7 long call over the Internet to the slow https
//server once every 60s. Has 10s timeout and sometimes takes as much as
//7s to execute
}
};
Thread thread_updater1s = new Thread() {
@Override
public void run() {
try {
while (true) {
handler.post(r1s);
sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread thread_updater2 = new Thread() {
@Override
public void run() {
try {
while (true) {
handler2.post(r2);
sleep(60000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
PS。请宽容和翔实我只code Java的15天至今完全没有之前的遭遇和教训。
PS. please be forgiving and informative I only code Java for 15 days so far with absolutely no prior experince or lesson.
推荐答案
您需要在线程(而不是发布可运行)的HTTP请求。然后,当你有下载的数据,您可以创建与该数据将更新该Runnable由UI线程执行图形和后一个可运行。下面是一个例子:
You need to make the http requests in the threads (not the posted runnables). Then, when you have the data downloaded, you create a runnable with that data that will update the graph and post that runnable to be executed by the UI thread. Here is an example:
public class LoopExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
thread_updater1s.start();
thread_updater2.start();
}// end of onCreate
Thread thread_updater1s = new Thread() {
@Override
public void run() {
try {
while (true) {
final Object data = getDataFromServer1();
handler.post(new Runnable() {
@Override
public void run() {
updateGraph1(data);
}
);
sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread thread_updater2 = new Thread() {
@Override
public void run() {
try {
while (true) {
final Object data = getDataFromServer2();
handler.post(new Runnable() {
@Override
public void run() {
updateGraph2(data);
}
);
sleep(60000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
显然,通过重新您的数据下载presents合适的类改变最终目标数据。
Obviously, change that final Object data by the appropriate class that represents your data downloaded.
这篇关于Android的 - 为什么第二个线程暂停第一个执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!