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

时间:2016-05-26 16:34:57

标签: java multithreading

我制作了一个程序来计算两个数字的和,差,产品和商。我使用线程按如下方式执行此操作

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

1 个答案:

答案 0 :(得分:1)

使用伪代码,您可以使用实现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();
    }

}