I'm taking a Java 2 class and our assignment is to show how multiple threads can be assigned different priorities.
I'm trying to manipulate the example code in the book to see if it works and I'm getting a compiler error: cannot find symbol for lines in TaskExecutor.java:
setPriority(task1.MIN_PRIORITY);
setPriority(task2.MAX_PRIORITY);
setPriority(task3.NORMAL_PRIORITY);
PrintTask.java:
import java.util.Random;
import java.lang.*;
public class PrintTask implements Runnable {
private final int sleepTime; // random sleep time for thread
private final String taskName; // name of task
private final static Random generator = new Random();
// constructor
public PrintTask( String name ) {
taskName = name; // set task name
// pick random sleep time between 0 and 5 seconds
sleepTime = generator.nextInt( 5000 ); // milliseconds
}
// method run contains the code that a thread will execute
public void run() {
try { // put thread to sleep for sleepTime amount of time
System.out.printf(
"%s going to sleep for %d milliseconds.\n", taskName, sleepTime );
Thread.sleep( sleepTime ); // put thread to sleep
}
catch ( InterruptedException exception ) {
System.out.printf(
"%s %s\n", taskName, "terminated prematurely due to interruption" );
}
// print task name
System.out.printf(
"%s done sleeping\n", taskName );
} // end method run
} // end class PrintTask
TaskExecutor.java:
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.lang.*;
public class TaskExecutor {
public static void main( String[] args ) {
// create and name each runnable
PrintTask task1 = new PrintTask( "task1" );
PrintTask task2 = new PrintTask( "task2" );
PrintTask task3 = new PrintTask( "task3" );
// Set Priorities
setPriority(task1.MIN_PRIORITY);
setPriority(task2.MAX_PRIORITY);
setPriority(task3.NORMAL_PRIORITY);
/* compiler error: cannot find symbol
setPriority(task_.---_PRIORITY);"
^
*/
System.out.println( "Starting Executor" );
// create ExecutorService to manage threads
ExecutorService threadExecutor = Executors.newCachedThreadPool();
// start threads and place in runnable state
threadExecutor.execute( task1 ); // start task1
threadExecutor.execute( task2 ); // start task2
threadExecutor.execute( task3 ); // start task3
// shut down worker threads when their tasks complete
threadExecutor.shutdown();
System.out.println( "Tasks started, main ends.\n" );
} // end main
} // end class TaskExecutor
My main question is: Why am I getting the "cannot find symbol" error? It happens if I try
task1.setPriority(MIN_PRIORITY);
or
task1.setPriority(1);
as well.
I realize some of the code may be wrong, but it compiles alright except TaskExecutor.java, which throws up three instances of the error I mentioned above, one for each 'task'. I'm a visual learner and I can learn much better if I can play around with bits of code and figure out how they fit together, if that justifies any of my mistakes, haha.
As PrintTask
implements Runnable
and it doesnot extends Thread
class you cannot call Thread
class method
well you can do something like this
PrintTask task1 = new PrintTask( "task1" );
Thread th=new Thread(task1);
th.setPriority(Thread.MIN_PRIORITY);