Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class ThreadStopTest {
  4.  
  5. public static void main(String[] args) {
  6. Thread unstoppableThread = new Thread(new Runnable() {
  7. @Override
  8. public void run() {
  9. while (true) {
  10.  
  11. final byte[] content = new byte[1000];
  12.  
  13. for (int i = 0; i < 3000000; i++) {
  14. // do nothing -> just to pollute memory
  15. // and perdoem a decently expensive operation
  16. StringBuilder builder = new StringBuilder();
  17.  
  18. int start = new Random().nextInt(content.length - 10);
  19.  
  20. builder.append(new String(content).substring(start, start + 10));
  21. builder.toString();
  22.  
  23. }
  24.  
  25. System.out.println("and another iteration...");
  26. }
  27. }
  28. });
  29.  
  30. unstoppableThread.start();
  31.  
  32. unstoppableThread.interrupt();
  33. try {
  34. unstoppableThread.join();
  35. }
  36. catch (InterruptedException e) {
  37. e.printStackTrace();
  38. }
  39.  
  40. System.out.println("FINISHED");
  41.  
  42.  
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement