gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:53:10 24 4
gpt4 key购买 nike

我编写了一个程序来计算两个数字的和、差、乘积和商。我使用线程来执行此操作,如下所示

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

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

编辑 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
}

基本上,我想要的是一个类拥有所有运行函数(或它们的名称),这样它就不再需要编写代码,并且更容易启动线程。

非常感谢任何帮助。

问候

普拉纳夫·拉斯托吉

最佳答案

通过伪代码,您可以使用实现 Runnable 的内部类。每个任务都有一个Runnable

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

并使用主 Runnable 来启动全局任务。这将是您的 final类。估计以后的使用会更加复杂

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

}

关于java - 如何在不同的线程中做不同的事情?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com