Hey guys. I was told to do an assignment, to demonstrate how a high priority thread gives a low priority thread a chance to run using the sleep function. The assignment has already been submitted and already graded. The following code when compiled not only creates a ThreadSleep.class file, but also creates a LowPriorityThread.class and a HighPriorityThread.class file.

The questions i pose:

why the compilation of external LowPriorityThread and external HighPriorityThread?

What is the role of the external HighPriorityThread.class and external LowPriorityThread.class?

Are these external class files necessary?

I would appreciate some comments on this.

//Importing libraries
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//Declarations
public class ThreadSleep extends JFrame {
   private HighPriorityThread high;
   private LowPriorityThread low;
   private JTextArea output;

   //Initializing
   public ThreadSleep()
   {
      super( "ThreadSleep" );
      output = new JTextArea( 10, 20 );
      getContentPane().add( output );
      setSize( 200, 250 );
      setVisible( true );

      //Starting the threads
      high = new HighPriorityThread( output );
      high.start();

      low = new LowPriorityThread( output );
      low.start();
   }
   //Main function
   public static void main( String args[] )
 {
    ThreadSleep app = new ThreadSleep();
    app.addWindowListener(
       new WindowAdapter() {
          public void windowClosing( WindowEvent e )
          {
             System.exit( 0 );
          }
       }
    );
 }
}
//Creating HighPriorityThread subclass
class HighPriorityThread extends Thread {
   private JTextArea display;

   public HighPriorityThread( JTextArea a )
   {
      display = a;
      setPriority( Thread.MAX_PRIORITY );
   }
   //Implementing "sleep"
   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( "This is a High Priority Thread!\n" );
      }
   }
}
//Creating LowPriorityThread subclass
class LowPriorityThread extends Thread {
   private JTextArea display;

   public LowPriorityThread( JTextArea a )
   {
      display = a;
      setPriority( Thread.MIN_PRIORITY );
   }

   public void run()
   {
      for ( int y = 1; y <= 5; y++ )
         display.append( "This is a Low Priority Thread!!!\n" );
   }
} //End of ThreadSleep program

Yep, they are necessary, basically, every time you make subclasses in Java, every one will have a separate file...
For example, I made a JFontChooser identical to the one in windows, I get these files

JFontChooser$1.class
JFontChooser$ButtonListener.class
JFontChooser$ListListener.class
JFontChooser.class

As you can guess, I have 2 subclasses, one for listening to button events, ad the other for listening to list events.

Thanks alot, darklord. that helped me understand it a bit better. so am I to understand that since those two functions were implemented as subclasses within the main program, they produce the external files?

would there have been another way of doing it better without externalising those files?

Thanks again.

Since those were meant to be 2 separate Threads, I think the idea of 2 subclasses was the best approach... so dont worry about the external files, I know they might seems "anthiestetic", but its was just the way to go.

cheers

There's no way of getting around the files created from inner/sub/external classes. Splitting up into classes like you've done is good. It's better OO design to split the functionality into different classes.

Thanks for the feedback, guys. Servercrash, thanks for responding. Apparently I found out that he (my instructor) meant my driver file wasn't detailed enough as it did not specify the purpose of those two class files. He's a bit of a hardass you see.

Guys thanks for the feedback, quiet encouraging and very supportive. This AdvancedJava course i'm taking is a very difficult course for me, and Its been about 3 years since my JuniorJava classes and i'm very rusty so i do appreciate you guys helping me out.

thanks,

sprx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.