방금 인터넷에서 일부 코드를 시도하고 실행했지만 내 에뮬레이터를 차단했습니다. 코드는 다음과 같습니다.이 앱이 차단되는 이유가 무엇인가요?
public void getcontents()
{
HttpConnection c = null;
InputStream is = null;
StringBuffer sb = new StringBuffer();
try
{
c = (HttpConnection)Connector.open("http://www.java-samples.com",Connector.READ_WRITE, true);
c.setRequestMethod(HttpConnection.GET); //default
is = c.openInputStream(); // transition to connected!
int ch = 0;
for(int ccnt=0; ccnt < 150; ccnt++) { // get the title.
ch = is.read();
if (ch == -1){
break;
}
sb.append((char)ch);
}
}
catch (IOException x){
x.printStackTrace();
}
finally{
try{
is.close();
c.close();
} catch (IOException x){
x.printStackTrace();
}
}
System.out.println(sb.toString());
}
OK 명령으로 함수를 호출했습니다.
프로세스를 죽일 때까지 에뮬레이터가 차단되었습니다. 어떻게 해결할 수 있습니까?
Java에서 스레드를 구현하는 방법을 모르겠지만 제 생각에 프로그램이 스트림의 데이터를 기다리고 있습니다. 이 작업이 다소 시간이 걸리거나 기다리면 함수가 실행될 별도의 스레드가 만들어집니다. 그래서 getContents가 끝나기를 기다리지 않고 프로그램이 계속 실행됩니다. 그러나 이것은 단지 추측입니다. – PeeHaa