Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Task implements Runnable {
- public void run() {
- try {
- Thread.sleep(1000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public class CurtisLeeOSProj {
- //counts number of threads
- private static int counter ;
- public static void main(String[] args) {
- //ArrayList to store threads in
- List threadList = new ArrayList();
- long starting_mem = Runtime.getRuntime().freeMemory();
- //Display initial system memory
- System.out.printf("Starting Memory = %d kilobytes",starting_mem/(1024));
- //While loop in a try catch statement that creates thread until memory runs out
- while (true) {
- try {
- Task task = new Task();
- Thread worker = new Thread(task);
- worker.start();
- threadList.add(task);
- Thread.currentThread().interrupt();
- counter++;
- //ends loop when memory runs out
- } catch (OutOfMemoryError e) {
- //display ending system memory
- long endMem = Runtime.getRuntime().freeMemory();
- System.out.printf("Ending Memory = %d kilobytes\n", (endMem/1024));
- System.out.printf("Remaining memory = %d kilobytes\n",(starting_mem - endMem)/(1024));
- System.out.printf("Threads Created = %d\n" , counter);
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement