Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. package com.javarush.test.level25.lesson07.task01;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.Socket;
  6.  
  7. /* Работать в поте лица!
  8. Реализуйте логику метода interrupt, который должен прерывать трэд предварительно закрыв используемые ресурсы
  9. Используйте метод super-класса в блоке finally
  10. */
  11. public class Solution extends Thread {
  12. private static final int BUFFER_SIZE = 2000; //2000 bytes
  13. private final Socket socket;
  14. private final InputStream in;
  15.  
  16. public Solution(Socket socket) throws IOException {
  17. this.socket = socket;
  18. this.in = socket.getInputStream();
  19. }
  20.  
  21. public void interrupt() {
  22. //implement logic here
  23. try
  24. {
  25. socket.close();
  26. }
  27. catch (IOException e)
  28. {
  29. e.printStackTrace();
  30. }
  31. }
  32.  
  33. public void run() {
  34. try {
  35. byte[] buf = new byte[BUFFER_SIZE];
  36. while (true) {
  37. int count = in.read(buf);
  38. if (count < 0) {
  39. break;
  40. } else {
  41. if (count > 0) {
  42. //process buffer here
  43. }
  44. }
  45. }
  46. } catch (IOException ignored) {}
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement