Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Task implements Runnable {
  4. public void run() {
  5. try {
  6. Thread.sleep(1000);
  7. } catch (Exception e) {
  8. e.printStackTrace();
  9. }
  10. }
  11. }
  12.  
  13. public class CurtisLeeOSProj {
  14. //counts number of threads
  15. private static int counter ;
  16. public static void main(String[] args) {
  17. //ArrayList to store threads in
  18. List threadList = new ArrayList();
  19. long starting_mem = Runtime.getRuntime().freeMemory();
  20. //Display initial system memory
  21. System.out.printf("Starting Memory = %d kilobytes",starting_mem/(1024));
  22. //While loop in a try catch statement that creates thread until memory runs out
  23. while (true) {
  24. try {
  25. Task task = new Task();
  26. Thread worker = new Thread(task);
  27. worker.start();
  28. threadList.add(task);
  29. Thread.currentThread().interrupt();
  30. counter++;
  31. //ends loop when memory runs out
  32. } catch (OutOfMemoryError e) {
  33. //display ending system memory
  34. long endMem = Runtime.getRuntime().freeMemory();
  35. System.out.printf("Ending Memory = %d kilobytes\n", (endMem/1024));
  36. System.out.printf("Remaining memory = %d kilobytes\n",(starting_mem - endMem)/(1024));
  37. System.out.printf("Threads Created = %d\n" , counter);
  38. break;
  39. }
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement