// Program demonstrates high priority threads
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Demo extends JFrame {
   private HighThread high;
   private LowThread low;
   private JTextArea output;
   public Demo()
   {
      super( "Demo" );
      output = new JTextArea( 10, 20 );
      getContentPane().add( output );
      setSize( 250, 200 );
      setVisible( true );
 
      high = new HighThread( output );
      high.start();
 
      low = new LowThread( output );
      low.start();
   }
   public static void main( String args[] )
   {
      Demo app = new Demo();
      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
   }
}
 
class HighThread extends Thread {
   private JTextArea display;
 
   public HighThread( JTextArea a )
   {
      display = a;
      setPriority( Thread.MAX_PRIORITY );
   }
 
   public void run()
   {
      for ( int x = 1; x <= 5; x++ )
         display.append( "High Priority Thread!!!\n" );
   }
}
class LowThread extends Thread {
   private JTextArea display;
 
   public LowThread( JTextArea a )
   {
      display = a;
      setPriority( Thread.MIN_PRIORITY );
   }
 
   public void run()
   {
      for ( int y = 1; y <= 5; y++ )
         display.append( "Low Priority Thread!!!\n" );
   }}
// Program demonstrates high priority threads
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Demo2 extends JFrame {
   private HighThread high;
   private LowThread low;
   private JTextArea output;
   public Demo2()
   {
      super( "Demo2" );
      output = new JTextArea( 10, 20 );
      getContentPane().add( output );
      setSize( 250, 200 );
      setVisible( true );
 
      high = new HighThread( output );
      high.start();
 
      low = new LowThread( output );
      low.start();
   }
   public static void main( String args[] )
   {
      Demo2 app = new Demo2();
      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
 
   }
}
class HighThread extends Thread {
   private JTextArea display;
   public HighThread( JTextArea a )
   {
      display = a;
      setPriority( Thread.MAX_PRIORITY );
   }
 
   public void run()
   {
      for ( int x = 1; x <= 5; x++ ) {
         try {
            sleep( ( int ) ( Math.random() * 200 ) );
         }
         catch ( Exception e ) {
            JOptionPane.showMessageDialog(
               null, e.toString(), "Exception",
               JOptionPane.ERROR_MESSAGE );
         }         
         display.append( "High Priority Thread\n" );
      }
   }
}
class LowThread extends Thread {
   private JTextArea display;
 
   public LowThread( JTextArea a )
   {
      display = a;
      setPriority( Thread.MIN_PRIORITY );
   }
 
   public void run()
   {
      for ( int y = 1; y <= 5; y++ )
         display.append( "Low Priority Thread!!!\n" );  }}