我如何从for循环内部睡眠线程。
这就是我正在尝试的但是没有睡觉。
Thread newTread = new Thread() {
@Override
public void run() {
for (int x = 0; x < limit; x++) {
//some_code
try {
Thread.sleep(3000);
}
catch (Exception e){
e.getLocalizedMessage();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//someCode
}
});
}
}
};
newTread.start();
我的代码有问题吗?
答案 0 :(得分:5)
我已经将代码抛入我自己的项目中,并添加了一些调试代码。这是我使用的代码和我收到的输出。
Thread newTread = new Thread() {
@Override
public void run() {
for (int x = 0; x < 3; x++) {
Log.d("ThreadTest","1");
try {
Log.d("ThreadTest","2");
Thread.sleep(3000);
Log.d("ThreadTest", "3");
}
catch (Exception e){
e.printStackTrace();
}
Log.d("ThreadTest","4");
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("ThreadTest","5");
}
});
}
}
};
newTread.start();
11-04 08:23:38.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:1 11-04 08:23:38.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:2 11-04 08:23:41.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:3 11-04 08:23:41.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:4 11-04 08:23:41.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:1 11-04 08:23:41.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:2 11-04 08:23:41.920 11419-11419 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:5 11-04 08:23:44.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:3 11-04 08:23:44.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:4 11-04 08:23:44.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:1 11-04 08:23:44.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:2 11-04 08:23:44.920 11419-11419 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:5 11-04 08:23:47.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:3 11-04 08:23:47.920 11419-11452 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:4 11-04 08:23:47.920 11419-11419 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:5
在我的情况下,代码实际上等了三秒钟,所以没有任何错误。然而,值得注意的是,“5”可以在之后出现这一事实。这是因为runOnUiThread的工作方式。它的作用是将你提供的runnable放在队列中。从这个队列中,android将一次抓取一个runnable并在Ui线程有时间运行它时运行它。这意味着,如果你要做这样的事情:
newTread.start();
boolean a = true;
while (a){
try {
Thread.sleep(1);
a = a;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
您将获得如下日志:
11-04 08:27:51.810 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:1 11-04 08:27:51.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:2 11-04 08:27:54.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:3 11-04 08:27:54.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:4 11-04 08:27:54.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:1 11-04 08:27:54.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:2 11-04 08:27:57.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:3 11-04 08:27:57.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:4 11-04 08:27:57.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:1 11-04 08:27:57.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:2 11-04 08:28:00.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:3 11-04 08:28:00.815 14909-14942 / nl.buroboot.danielvandenberg.bootsnelleinvoer D / ThreadTest:4
请注意,那里没有一个5。这是因为UI线程太忙而无法执行堆栈。我猜这正是你发生的事情。
我建议修复此问题:Breakpoints。试着弄清楚你的UI线程是否挂起,如果是这样的话,并解决它。此代码可以正常运行。
答案 1 :(得分:-1)
由于投票率下降:请注意下面的源代码中的评论,他们会告诉您一切是如何运作的。
private void waitForWifi() {
mProgressDialog = new Dialog.Builder(MainActivity.this).show();
// creating a thread to wait until the connection is established
Thread t = new Thread() {
@Override
public void run() {
try {
while (!Utils.isConnected(MainActivity.this)) {
// we wait until condition is matching to fit our needs
Thread.sleep(300);
// Do NOT(!) use "sleep(1);" <- pure waste of battery
// and won't in 99,9% of the situa. make your app better!
}
mProgressDialog.dismiss();
// perform a new action
};
t.start();
}