有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java创建两个不同的线程

我是线程新手,我有个问题

我需要创建两个不同的线程

在第一个线程中,我需要读取一个文件并将其复制到另一个文件中

在第二个线程中,我需要按升序排列数字

我有第一个线程信息的代码:

package java10;

import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;

class FileCopy
{
   public static void main(String[] args) 
   {
      try 
      {
         File fileIn  = new File("C:/Users/dis_YO_boi/Documents/Abhishek.txt");
         File fileOut = new File("C:/Users/dis_YO_boi/Documents/Mallela.txt");

         FileInputStream streamIn   = new FileInputStream(fileIn);
         FileOutputStream streamOut = new FileOutputStream(fileOut);

         int c;
         while ((c = streamIn.read()) != -1) 
         {
            streamOut.write(c);
         }

         streamIn.close();
         streamOut.close();
      }
      catch (FileNotFoundException e) 
      {
         System.out.println("FileCopy: " + e);
      } 
      catch (IOException e) 
      {
         System.out.println("FileCopy: " + e);
      }
   }
}

我有第二个线程信息的代码:

package java10;

public class Ascending {

    public static void main(String[] args) {
        int nums[]={-1,23,50,-100,34};
        //print the values before ordering
        for (int i=0;i<nums.length;i++)
            System.out.println(nums[i]);

        for(int i=0;i<nums.length-1;i++){
            for(int j=i+1;j<nums.length;j++){
                if(nums[i]>nums[j]){
                    int temp=nums[i];
                    nums[i]=nums[j];
                    nums[j]=temp;

                }
            }
        }

    System.out.println("___________________________");
    for (int i=0;i<nums.length;i++)
        System.out.println(nums[i]);
    }
}

共 (2) 个答案

  1. # 1 楼答案

    简单的回答是看一下这个文档http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html

    基本上,您需要在这两个类中扩展Thread或实现Runnable,然后将您正在做的工作转移到一个带有签名的方法中

    public void run()
    

    如果扩展线程,只需创建类的新实例并调用。start(),如果实现Runnable,可以创建一个新的Thread实例,将类的实例传递给它,然后调用start。因为你们两个班都没有扩展任何东西,所以扩展线程可能是最容易的选择

    public class FileCopy extends Thread
    {
      public void run()
      {
        // here you'd put your code
      }
    }
    
    public class Ascending extends Thread
    {
      public void run()
      {
        // again, you'd move your code here
      }
    }
    
    public class MyThreadRunner
    {
      public static void main(String[] args)
      {
        FileCopy filecopy = new FileCopy();
        Ascending ascending = new Ascending();
    
        filecopy.start();
        ascending.start();
      }
    }
    
  2. # 2 楼答案

    你搞错了,需要在Ascending和FileCopy中扩展Thread类

    重写run方法并在那里执行操作。 从第三个类或其中一个类实现一个main方法,该方法创建两个线程并对它们调用start(),否则:

    //Class FileCopy with a main method
    package java10;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    
    class FileCopy extends Thread {
    
       public static void main(String[] args){
          FileCopy fileCopy = new FileCopy();
          Ascending ascending = new Ascending();
          fileCopy.start();
          ascending.start();
          ascending.join();
          fileCopy.join();
       }
    
       @Override
       public void run() {
          try {
    
             File fileIn  = new File("C:/Users/dis_YO_boi/Documents/Abhishek.txt");
             File fileOut = new File("C:/Users/dis_YO_boi/Documents/Mallela.txt");
    
             FileInputStream streamIn   = new FileInputStream(fileIn);
             FileOutputStream streamOut = new FileOutputStream(fileOut);
    
             int c;
             while ((c = streamIn.read()) != -1) 
             {
                streamOut.write(c);
             }
    
             streamIn.close();
             streamOut.close();
          }
          catch (FileNotFoundException e) 
          {
             System.out.println("FileCopy: " + e);
          } 
          catch (IOException e) 
          {
             System.out.println("FileCopy: " + e);
          }
       }
    }
    
    //Class Ascending
    
    package java10;
    
    public class Ascending extends Thread {
        @Override
        public void run(){
            int nums[]={-1,23,50,-100,34};
                //print the values before ordering
                for (int i=0;i<nums.length;i++)
                    System.out.println(nums[i]);
    
                for(int i=0;i<nums.length-1;i++){
                    for(int j=i+1;j<nums.length;j++){
                        if(nums[i]>nums[j]){
                            int temp=nums[i];
                            nums[i]=nums[j];
                            nums[j]=temp;
    
                        }
                    }
                }
    
           System.out.println("___________________________");
           for (int i=0;i<nums.length;i++)
              System.out.println(nums[i]);
        }
    
    }