我有两节课。
tasks.java 编译成功。我不知道如何将tasks.java包含到另一个类中我收到错误编译cs508.java时找不到符号任务
tasks.java 在下面给出
import java.util.Random;
public class tasks 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();
public tasks( String name )
{
taskName = name; // set task name
// pick random sleep time between 0 and 5 seconds
sleepTime = generator.nextInt( 5000 ); // milliseconds
} // end SimpleThread constructor
// 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 will sleep for %d Milliseconds.\n",
taskName, sleepTime );
Thread.sleep( sleepTime ); // put thread to sleep
} // end try
catch ( InterruptedException exception )
{
System.out.printf( "%s %s\n", taskName,
"terminated prematurely due to interruption" );
} // end catch
// print task name
System.out.printf( "%s thread has finished\n", taskName );
} // end method run
} // end class SimpleThread
enter code here
并且 cs508.java 在下面给出,这两个文件都位于同一目录中
import java.lang.Thread;
import java.util.Random;
public class CS508
{
public static void main( String[] args )
{
System.out.println();
// create each thread with a new targeted runnable
**//Error is here "Cannot find Symbol tasks"**
Thread thread1 = new Thread( new tasks( "asdf" ) );
System.out.println();
// start threads and place in runnable state
thread1.start(); // invokes run method
System.out.println();
}
// end main
} // end class CS508