2013-07-08 5 views
-1

나는 두 개의 클래스가 있습니다.기호 작업을 찾을 수 없습니다. 자바 스레딩에서

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 
+0

참고. 또한 "기본"(이름 지정되지 않은) 패키지를 사용하는 대신 클래스를 사용자 정의 패키지로 구성하는 것이 가장 좋습니다. 패키지 이름 (예 :'your/custom/pkg')과 일치하는 디렉토리 구조에 클래스를 넣고 클래스의 맨 위에'package your.custom.pkg;'를 추가하면됩니다. – superEb

+0

두 클래스 모두에 대해'package' 문을 언급하십시오. 이것은 모든 import 문 위의 모든 파일의 맨 위에 있습니다. –

답변

0
import tasks; 

S hould 당신이 자바 프로그래머로 수행 할 CS508.java

+1

클래스가 동일한 패키지에 있으면 필요하지 않습니다. – superEb

+0

'-cp : : <다른 classpaths>'? –

+0

not working ... 같은 오류가 발생했습니다. –

0

두 규칙의 상단에 나타납니다 : packages 및 클래스 이름은 camelcased입니다 귀하의 응용 프로그램을 구성 할 수 있습니다. 두 클래스는 동일한 패키지에있는 경우

Tasks.java

package com.stackoverflow; 

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 

CS508.java

package com.stackoverflow; 

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 
     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 

당신은 import로 가져올 필요가 없습니다;


다음과 같은 폴더 구조가있는 경우 :

/ 
-- com/ 
    -- stackoverflow/ 
     -- Tasks.java 
     -- CS508.java 

당신이 javac -d . com/stackoverflow/CS508.java com/stackoverflow/Tasks.java를 사용하여 자바 파일을 (올바른 것들에 대한 위의보고) 컴파일하고 java com.stackoverflow.CS508으로 main 메소드를 실행 할 수 있습니다.

샘플 출력 : public 클래스 파일과 동일한 이름을 가지고 있어야하며, 모두 대소 문자를 구분 자바 것을

fuh% java com.stackoverflow.CS508           



asdf will sleep for 2684 Milliseconds. 
asdf thread has finished 
+0

나를 위해 작동하지 않습니다. ( –

+0

내 질문을 편집했습니다. – cr0

관련 문제