-1

我有两节课。

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 
4

2 回答 2

0
import tasks;

应该出现在顶部CS508.java

于 2013-07-08T20:56:36.083 回答
0

作为一名 Java 程序员,你要遵循两条规则:用组织你的应用程序,并且类名是驼峰式的。

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.

样本输出:

fuh% java com.stackoverflow.CS508                                        



asdf will sleep for 2684 Milliseconds.
asdf thread has finished
于 2013-07-08T21:10:20.450 回答