Java - Thread.setPriority()错误:找不到符号(Thread和setPriority之间的句点)

时间:2014-08-07 18:31:05

标签: java multithreading thread-priority

我正在学习Java 2课程,我们的任务是展示如何为多个线程分配不同的优先级。

我正在尝试操作本书中的示例代码以查看它是否有效并且我收到编译器错误:找不到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

我的主要问题是:为什么我收到“无法找到符号”错误?如果我尝试

,就会发生这种情况
task1.setPriority(MIN_PRIORITY);

task1.setPriority(1);

我意识到一些代码可能是错误的,但除了TaskExecutor.java之外它编译正常,它会抛出我上面提到的错误的三个实例,每个'任务'一个。我是一个视觉学习者,如果我可以玩一些代码并弄清楚它们如何组合在一起,我可以学得更好,如果这证明我的任何错误,哈哈。

3 个答案:

答案 0 :(得分:3)

您的PrintTask实施Runnable。您想调用Thread中定义的方法。

您无法访问Executor内运行的线程,因此您无法设置其优先级。改为定期Threads,您可以设置优先级。

答案 1 :(得分:1)

由于PrintTask实现Runnable并且它不扩展Thread类,您无法调用Thread类方法

你可以做这样的事情

PrintTask task1 = new PrintTask( "task1" );
Thread th=new Thread(task1);
th.setPriority(Thread.MIN_PRIORITY);

答案 2 :(得分:0)

XXX _PRIORITY字段是 Thread 类的静态字段,因此必须像这样访问;线程。 XXX _PRIORITY。您使用点(。)来访问类和实例(对象)的字段。您要做的是访问PrintTask实例的 XXX _PRIORITY字段(taskX。 XXX _PRIORITY),但PrintTask没有定义具有这些名称的任何字段,因此你得到例外:)同样适用于方法,必须从线程实例 random_thread .setPriority(some_number)访问setPriority(int i)。希望你能理解,如果不是只是问我!