简体   繁体   中英

Creating two different threads

I'm new to threads and I have a question.

I need to create two different threads.

In the first thread, I need to read a file and copy it into another file.

In the second thread, I need to put numbers in ascending order.

I have my code for the first thread info:

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);
      }
   }
}

I have my code for the second thread info:

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]);
    }
}

You got it wrong you need to extend the Thread Class in Ascending and FileCopy.

Override the run method and do your operations there. From a third class, or one of those classes implement a main method which creates two threads and calls start() on them, otherwise speaking:

//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]);
    }

}

The short answer is to take a look at this documentation http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html

Basically you will need to either extend Thread or implement Runnable in both classes, then move the work you are doing into a method with the signature

public void run()

If you extend Thread you can just create a new instance of your class and call .start(), and if you implement Runnable you can create a new instance of Thread, passing an instance of your class to it and then call start on that. Since neither of your class extend anything, extending Thread is probably your easiest bet.

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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM