gpt4 book ai didi

Java - Thread.setPriority() 错误 : cannot find symbol (the period between Thread and setPriority)

转载 作者:行者123 更新时间:2023-11-30 07:07:02 27 4
gpt4 key购买 nike

我正在上 Java 2 类(class),我们的作业是展示如何为多个线程分配不同的优先级。

我正在尝试操作书中的示例代码以查看它是否有效,但我遇到了编译器错误:无法在 TaskExecutor.java 中找到行的符号:

setPriority(task1.MIN_PRIORITY);
setPriority(task2.MAX_PRIORITY);
setPriority(task3.NORMAL_PRIORITY);

打印任务.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

任务执行器.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,它抛出了我上面提到的三个错误实例,每个“任务”一个。我是一个视觉学习者,如果我可以玩弄一些代码并弄清楚它们是如何组合在一起的,我可以学得更好,如果这证明我的任何错误是正确的,哈哈。

最佳答案

您的 PrintTask 实现了 Runnable。您想调用在 Thread 中定义的方法。

您无权访问在 Executor 中运行的线程,因此您无法设置它们的优先级。相反,创建常规的线程,您可以设置它们的优先级。

关于Java - Thread.setPriority() 错误 : cannot find symbol (the period between Thread and setPriority),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25189624/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com