找不到符号任务。在java线程中

找不到符号任务。在java线程中,java,multithreading,Java,Multithreading,我有两节课 已成功编译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 task

我有两节课

已成功编译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 

应该出现在
CS508.java的顶部。作为一名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
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
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
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
CS508.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
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方法

样本输出:

fuh% java com.stackoverflow.CS508                                        



asdf will sleep for 2684 Milliseconds.
asdf thread has finished

如果类位于同一个包中,则不需要这样做。
-cp.
?请注意,公共类必须与文件具有相同的名称,并且在Java中两者都区分大小写。最好将类组织到自定义包中,而不是使用“默认”(未命名)包。只需将类放在与包名匹配的目录结构中(例如,
your/custom/pkg
),然后添加
package-your.custom.pkg
到你的班级的顶端。请提及你的两个班级的
套餐
声明。它存在于所有导入语句之上的每个文件的顶部。