나는 두 개의 클래스가 있습니다.기호 작업을 찾을 수 없습니다. 자바 스레딩에서
tasks.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 아래에 주어진 cs508.java을 컴파일하는 동안 찾을 수없는 오류 기호 작업을 얻고 다른 클래스 에 tasks.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
참고. 또한 "기본"(이름 지정되지 않은) 패키지를 사용하는 대신 클래스를 사용자 정의 패키지로 구성하는 것이 가장 좋습니다. 패키지 이름 (예 :'your/custom/pkg')과 일치하는 디렉토리 구조에 클래스를 넣고 클래스의 맨 위에'package your.custom.pkg;'를 추가하면됩니다. – superEb
두 클래스 모두에 대해'package' 문을 언급하십시오. 이것은 모든 import 문 위의 모든 파일의 맨 위에 있습니다. –