-
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.
Code:
//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
-
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.
~evlich
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks