DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2005

    Smile Help: need feedback on my Java assignment about thread sleep. It's already coded.

    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.*;
    public class ThreadSleep extends JFrame {
       private HighPriorityThread high;
       private LowPriorityThread low;
       private JTextArea output;
       public ThreadSleep()
          super( "ThreadSleep" );
          output = new JTextArea( 10, 20 );
          getContentPane().add( output );
          setSize( 200, 250 );
          setVisible( true );
          //Starting the threads
          high = new HighPriorityThread( output );
          low = new LowPriorityThread( output );
       //Main function
       public static void main( String args[] )
        ThreadSleep app = new ThreadSleep();
           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 ) {
                   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

  2. #2
    Join Date
    Aug 2003
    Java has to create a .class file for every single class, regardless of where it is defined. The .class file is the template for creating objects of the given type, if you dont' have a template then you can't make the objects. You will notice if you make inner classes they will also get .class files named outerclass$innerclass.class. Even if they are anonymous they will get them, named outerclass$1.class, outerclass$2.class, etc.. This is how Java programs that are contained in a single file will often explode to 20 or 30 .class files. I'm personally of the opionion that most classes should have their own file, except when closure is needed, but that is just my opionion.

    Hope this helps.

  3. #3
    Join Date
    Jun 2005
    Yes, that definitely helped a lot evlich. thanks for the info. It helps me understand the role of class files far better. Thanks alot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center