我刚刚开始使用线程,所以我有一个问题。在这种情况下,当我从doInBackground()调用方法initOctoshape()时,为什么会创建一个新线程(与后台线程不同)?日Thnx!
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;
}
}
答案 0 :(得分:1)
doInBackground(Params...)
的{{1}}方法总是在后台线程上执行,并且由于您从此方法调用AsyncTask
,它也将在此后台线程上执行。这就是initOctoshapeSystem(thisActivity);
的行为。
所以,
当我从doInBackground()调用方法initOctoshape()时,会创建一个新线程(与后台线程不同)
不正确。您对AsyncTask
的来电仅跟随驱动initOctoshape()
的线程。