如何在不同的线程中做不同的事情



我制作了一个程序,可以计算两个数字的和、差、乘积和商。我使用线程来做这件事,如下

import java.util.Scanner;
public class ThreadTest
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter 1st operand ::");
int a = kb.nextInt();
System.out.print("Enter 2nd operand ::");
int b = kb.nextInt();
Addition add = new Addition(a, b);
Subtraction sub = new Subtraction(a, b);
Multiplication mul = new Multiplication(a, b);
Division div = new Division(a, b);
Thread t1 = new Thread(add);
Thread t2 = new Thread(sub);
Thread t3 = new Thread(mul);
Thread t4 = new Thread(div);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Addition implements Runnable
{
int a, b;
public Addition(int a, int b)
{
this.a = a;
this.b = b;
}
public void run()
{
System.out.println("Addition :: " + (a+b));
}
}

class Subtraction implements Runnable
{
int a, b;
public Subtraction(int a, int b)
{
this.a = a;
this.b = b;
}
public void run()
{
System.out.println("Subtraction :: " + (a-b));
}
}
class Multiplication implements Runnable
{
int a, b;
public Multiplication(int a, int b)
{
this.a = a;
this.b = b;
}
public void run()
{
System.out.println("Multiplication :: " + (a*b));
}
}
class Division implements Runnable
{
int a, b;
public Division(int a, int b)
{
this.a = a;
this.b = b;
}
public void run()
{
System.out.println("Division :: " + (float)(a/b));
}
}

现在我想知道的是,我需要为我想在单独的线程中运行的每个操作创建一个单独的类,那么有没有一种方法可以在一个类中完成它,并且仍然为每个运算符创建单独的线程?

编辑1伪码:

class operators implements runnable{
public void run1(){
//some thing to do
}
public void run2(){
//some thing to do
}
}
public class Main{
public static void main(String args[]){
//make object of operator class
//make new thread for operator class' object
//thread.start() should start all the run(run1, run2) methods at once
}

基本上,我想要的是一个类拥有所有的运行函数(或它们被调用的任何函数),这样它就不那么需要编写代码,也更容易启动线程。

非常感谢您的帮助。

问候

Pranav Rastogi

使用伪代码,可以使用内部类实现Runnable。每个任务一个Runnable

Runnable add = new Runnable() {
public void run() {
System.out.println("Addition :: " + (a+b));
};
};   

并使用主Runnable启动全局任务。这将是您的最终课程。我想未来的使用将更加复杂

import java.util.Scanner;
public class ThreadTest
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter 1st operand ::");
int a = kb.nextInt();
System.out.print("Enter 2nd operand ::");
int b = kb.nextInt();
Operations op = new Operations (a,b);
Thread t1 = new Thread(op);
t1.start();
}
}
class Operations implements Runnable{
int a, b;
public Operations(int a, int b){
this.a = a;
this.b = b;
}
public void run() {
Runnable add = new Runnable() {
public void run() {
System.out.println("Addition :: " + (a+b));
};
};    
Runnable sub = new Runnable() {
public void run() {
System.out.println("Subtraction :: " + (a-b));
};
};
Runnable div = new Runnable() {
public void run() {
System.out.println("Division :: " + (float)(a/b));
};
};
Runnable mul = new Runnable() {
public void run() {
System.out.println("Multiplication :: " + (a*b));
};
};
Thread t1 = new Thread(add);
Thread t2 = new Thread(sub);
Thread t3 = new Thread(mul);
Thread t4 = new Thread(div);
t1.start();
t2.start();
t3.start();
t4.start();
}
}

最新更新