[英]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. 您弄错了,您需要在Ascending和FileCopy中扩展Thread类。
Override the run method and do your operations there. 覆盖run方法并在那里进行操作。 From a third class, or one of those classes implement a main method which creates two threads and calls start() on them, otherwise speaking:
从第三个类或其中一个类实现一个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]);
}
}
The short answer is to take a look at this documentation http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html 简短的答案是看一下本文档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 基本上,您将需要在两个类中都扩展Thread或实现Runnable,然后将正在执行的工作移到带有签名的方法中
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. 如果扩展Thread,则可以仅创建类的新实例并调用.start();如果实现Runnable,则可以创建Thread的新实例,将类的实例传递给它,然后在其上调用start。 Since neither of your class extend anything, extending Thread is probably your easiest bet.
由于您的任何一个类都不扩展任何内容,因此扩展Thread可能是您最简单的选择。
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.