简体   繁体   English

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

[英]how to do different things in different threads?

I have made a program that computes the sum, difference, product and quotient of two numbers. 我编写了一个程序,用于计算两个数字的总和,差,乘积和商。 I used threads to do this as follows 我使用线程如下

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

Now what I want to know is that I needed to make a separate class for every operation I wanted to run in a separate thread so is there a way I can do it in one class and still have separate threads for every operator? 现在我想知道的是,我需要为要在单独线程中运行的每个操作创建一个单独的类,那么有没有办法在一个类中做到这一点,并且仍然为每个运算符提供单独的线程?

Edit 1 Pseudocode: 编辑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
}

Basically, what I want is one class to have all the run functions(or whatever they are called) so it becomes less of a code to write and easier to start the threads. 基本上,我想要的是一个具有所有运行函数(或任何调用它们的函数)的类,这样一来,编写代码变得更少,并且更容易启动线程。

Any help is highly appreciated. 非常感谢您的帮助。

Regards 问候

Pranav Rastogi 普拉纳夫·拉斯托吉(Pranav Rastogi)

With your pseudocode, you can use inner classes implementing Runnable. 使用伪代码,您可以使用实现Runnable的内部类。 One Runnable for each task. 每个任务一个Runnable

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

And use a main Runnable to start the global tasks. 并使用主Runnable启动全局任务。 This will be your resulting class. 这将是你的结果课。 I guess the future use will be more complex 我想将来的使用会更复杂

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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