달력

2

« 2025/2 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
인터럽트를 거는 것이 인터럽트 상태 변수의 값을 설정하는 것 말고는 아무런 실제적 효과가 없다.
블로킹 메소드에서 대기중인 스레드가 작업을 멈추도록 할 수 있긴 하지만, 이런 작업을 하고자 할 때에는 해당 스레드가 대기상태에 멈춰있는 이유가 무엇인지를 훨씬 정확하게 이해해야 한다.

java.io 패키지의 동기적 소켓 IO
java.nio 패키지의 동기적 IO
Selector를 사용한 비동기적 IO
락 확보 

public class ReaderThread extends Thread{

private final Socket socket;
private final InputStream in;
public ReaderThread(Socket socket) throws IOException {
// TODO Auto-generated constructor stub
this.socket = socket;
this.in = socket.getInputStream();
}
public void interrupt(){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
super.interrupt();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
byte[] buf = new byte[1024];
while(true){
try {
int count = in.read(buf);
if(count < 0)
break;
else if(count > 0)
processBuffer(buf, count);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

private void processBuffer(byte[] buf, int count) {
// TODO Auto-generated method stub
String reads = new String(buf, 0, count);
System.out.println(reads);
}


/* 소켓이 닫힐 때 해당 스레드에 인터럽트가 걸려 실행 중인 작업에 중단 요청이 있을 때 대응하는 속도를 크게 개선할 수 있다
    다시 말해서 응답 속도를 떨어뜨리지 않으면서 인터럽트에 대응하는 블로킹 메소드를 안전하게 호출할 수 있다. 대기 상태에 들어갈 수 있는 소켓 IO 메소드와 같은 기능도 호출할 수 있다. 

'JAVA이야기' 카테고리의 다른 글

[ThreadPool] 오래실행되는 작업  (0) 2011.09.19
[ThreadPool] 활용  (0) 2011.09.19
인터럽트 정책  (0) 2011.09.01
스레드 작업중단2  (0) 2011.09.01
스레드 중단 및 종료  (0) 2011.09.01
:
Posted by НooпeУ


Code Start Code End