实现Runnable抽象类 - 多线程

时间:2016-01-12 05:51:10

标签: java multithreading

我想调用两个具有不同参数的构造函数,这些构造函数又运行其各自的线程,但问题是它只允许一个run()方法,无论如何我可以在一个方法中运行两个run()方法类??或任何其他解决这种情况的方法?

class Threading implements Runnable {

    Thread t;
    Thread t1;
    String name;

    Threading(String s) {
        name = s;
        t = new Thread(this, name);
        System.out.println("Constructor 1: " + t);
        t.start();
    }

    public void run() {
        try {
            for (int i = 5; i > 0; i--) {
                System.out.println("Constructor 1: " + i);
                Thread.sleep(2000);
            }
        } catch (Exception e) {
        }
        System.out.println("Constructor1 exiting");
    }

    Threading(int a, int b) {
        t1 = new Thread(this, "java2 Thread");
        System.out.println("Constructor 2: " + t1);
        t1.start();
    }

    public void run() {
        try {
            for (int i = 5; i > 0; i++) {
                System.out.println("Constructor 2: " + i);
                Thread.sleep(3000);
            }
        } catch (Exception e) {
        }
        System.out.println("Constructor2 exiting");
    }
}

MultipleThread

class MultipleThread {
    public static void main(String[] args) {
        new Threading("java thread"); // Constructor 1
        new Threading(1, 2); // Constructor 2

        try {
            for (int i = 5; i > 0; i--) {
                System.out.println("Print: " + i);
                Thread.sleep(1000);
            }
        } catch (Exception e) {
        }
        System.out.println("Exit Main");
    }
}

2 个答案:

答案 0 :(得分:0)

没有理由使用相同的Runnable课程来实施Threading。您可以在每个构造函数中匿名实现它们:

Threading(String s) {
    name=s;
    t=new Thread(new Runnable() {
        @Override
        public void run() {
            // ...
        }
    , name);
    System.out.println("Constructor 1: "+t);
    t.start();
}

答案 1 :(得分:0)

你可以有一个名为MyThread的第三个类,它实现了Runnable接口,并且有一个带参数的构造函数,以保存构造函数创建它:

public class MyThread implements Runnable{
    private String calledFrom;
    public MyThread(String calledFrom){
        this.calledFrom = calledFrom;
    }
    public void run(){
        try {
            for (int i = 5; i > 0; i++) {
                System.out.println(calledFrom + ": " + i);
                Thread.sleep(3000);
            }
        } catch (Exception e) {
        }
        System.out.println(calledFrom+" exiting");            
    }
}

然后以不实现Runnable接口的方式更改Threading类。现在在两个构造函数中,您可以使用不同参数的MyThread类的不同实例:“构造函数1”“构造函数2”

class Threading{
    ...
    Threading(String s) {
         name = s;
         //t = new Thread(this, name);
         MyThread mt = new MyThread("Constructor 1");
         System.out.println("Constructor 1: " + t);
         //t.start();
         mt.start();
    }

    Threading(int a, int b) {
         //t1 = new Thread(this, "java2 Thread");
         MyThread mt = new MyThread("Constructor 2");
         System.out.println("Constructor 2: " + t1);
         //t1.start();
         mt.start();
    }
}

“构造函数2”“构造函数1”传递给MyThread的构造函数只是一个示例。您可以将一些真实有用的参数传递给MyThread,这有助于您通过参数的类型或值来分隔实现。

例如,您可以在MyThread类中使用两个逻辑方法:logicMethodA()logicMethodB(),并根据构造函数的参数值决定调用run()方法中的哪一个。

希望这会有所帮助。