我刚刚开始使用线程,所以我有一个问题。在这种情况下,当我从 doInBackground() 调用 initOctoshape() 方法时,为什么会创建一个新线程(与后台线程不同)?谢谢!
public class PlayerActivity extends CustomActivity implements
ProblemListener{
public class PlayChannel extends AsyncTask<Channel, String, String> {
Channel channel;
PlayerActivity thisActivity;
@Override
protected String doInBackground(Channel... params) {
Thread.currentThread().getName();
channel=params[0];
thisActivity=channel.thisActivity;
initOctoshapeSystem(thisActivity);
return url;
}
}
public void initOctoshapeSystem(Activity thisActivity) {
os = OctoStatic.create(thisActivity, thisActivity, null);
os.setOctoshapeSystemListener(new OctoshapeSystemListener() {
@Override
public void onConnect() {
Thread.currentThread().getName();
mStreamPlayer = setupStream(OCTOLINK);
mStreamPlayer.requestPlay();
}
});
}
public StreamPlayer setupStream(final String stream) {
StreamPlayer sp = os.createStreamPlayer(stream);
sp.setListener(new StreamPlayerListener() {
@Override
public void gotUrl(String url) {
}
});
return sp;
}
}
最佳答案
AsyncTask
的 doInBackground(Params...)
方法始终在后台线程上执行,并且由于您调用了 initOctoshapeSystem(thisActivity);
从这个方法中,它也将在此后台线程上执行。这就是 AsyncTask
的行为。
所以,
a new thread (different from the background thread) gets created when I call the method initOctoshape() from doInBackground()
不正确。您对 initOctoshape()
的调用仅遵循驱动 doInBackground()
的线程。
关于java - AsyncTask 中的新线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40696718/