Java 在不同的线程上执行两种不同的接口实现

Java 在不同的线程上执行两种不同的接口实现,java,multithreading,future,runnable,callable,Java,Multithreading,Future,Runnable,Callable,我有一个带有方法的界面,比如说VegetableCreation: public void writeVeggieDataIntoFile(); 还有两个不同的类,分别称为Apple和Mango,它们实现了VegetableCreation 还有一个带有create()方法的工厂类VegetableFactory: public class VegetableFactory { public VegetableCreation create(String arg0) {

我有一个带有方法的界面,比如说
VegetableCreation

public void writeVeggieDataIntoFile();
还有两个不同的类,分别称为
Apple
Mango
,它们实现了
VegetableCreation

还有一个带有
create()
方法的工厂类
VegetableFactory

public class VegetableFactory {
    public VegetableCreation create(String arg0) {

        if (arg0.equals("Apple"))
           return new Apple();
        else if (arg0.equals("Mango")
           return new Mango();
        else {
           // create and return both apple and mango by spawning two different threads
           // so that the writeVeggieDataIntoFile();  gets invoked concurrently
           // for both apple and mango and two different file is created
        }
    }
}
基本上,我在这里试图实现的是,当我从客户机类的
main()
方法调用
VegetableFactory
类的
create()
方法,并将
“Apple”
“Mango”
以外的任何字符串值作为运行时参数传递。我想要两个不同的线程在每个
Apple
Mango
对象上工作,并在每个
writeVeggieDataIntoFile()
方法上同时工作

如对设计策略或使用哪些并发API等有任何建议,将不胜感激


附言:我应该称之为水果**,而不是蔬菜*

首先,我希望你们的工厂有静态创建。然后在水果的createdo项实例中创建水果线程。否则,如果项目实例为蔬菜,则执行蔬菜线程。

首先,我会让您的工厂进行静态创建。然后在水果的createdo项实例中创建水果线程。否则,如果项目实例为蔬菜,则执行蔬菜线程。

查看复合模式,然后构建一个CompositeVegetable,当被告知“做它的事情”时,它会启动两个线程,一个做一件事,另一个做另一件事

public class BothVegetable implements Vegetable {
    public void writeVeggieDataInfoFile() {
        Thread thread1 = new Thread(new AppleRunnable());
        Thread thread2 = new Thread(new MangoRunnable());
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
    }
}

// and in your factory

    return new CompositeVegetable();

对我来说,你的蔬菜看起来像水果

查看复合模式,然后构建一个CompositeVegetable,当被告知“做它的事情”时,它会启动两个线程,一个做一件事,另一个做另一件事

public class BothVegetable implements Vegetable {
    public void writeVeggieDataInfoFile() {
        Thread thread1 = new Thread(new AppleRunnable());
        Thread thread2 = new Thread(new MangoRunnable());
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
    }
}

// and in your factory

    return new CompositeVegetable();

对我来说,你的蔬菜看起来像水果

我希望从一家名为
蔬菜工厂
/
水果工厂
/
水果工厂
,而不是
蔬菜工厂
/
水果工厂

我会避免在工厂方法中创建线程和编写文件作为副作用

我还将
writeFruitDataIntoFile()
重命名/更改为
write(Writer out)
,因为您编写的内容由封装的接口或类通知,写入的位置由其方法参数通知

如果确实需要并发处理,请在
write(…)
中创建一个线程,并让工厂方法简单地返回一个
结果

优点是:

  • 对象创建时无需记录的副作用
  • 线程仅在需要时创建(在调用
    write()
    的情况下),而不是在每次创建
    对象时创建
  • 你不需要额外的课程
  • 这是一个干净的实现,每个了解工厂设计模式的人都能一目了然
见:

为了良好的OO实践,我会使用一个接口:

interface Fruit {

  void write(Writer out);

  ...
}  // Fruit
it的抽象实现:

public abstract class AbstractFruit implements Fruit {

  Data data;

  public void write(Writer out) {
    ...
  }

  ...
}  // AbstractFruit

为了方便起见,我会使用特定的
get…()
方法(通常是这样):

枚举

interface Fruit {

  enum Type {
    APPLE,
    MANGO 
  }

  void write(Writer out);

  ...
}  // Fruit

有关以下内容,请参阅API文档:

未检查的异常不需要在方法[…]抛出子句中声明


我希望从一家名为
蔬菜工厂
/
水果工厂
/
水果工厂
,而不是
蔬菜工厂
/
水果工厂

我会避免在工厂方法中创建线程和编写文件作为副作用

我还将
writeFruitDataIntoFile()
重命名/更改为
write(Writer out)
,因为您编写的内容由封装的接口或类通知,写入的位置由其方法参数通知

如果确实需要并发处理,请在
write(…)
中创建一个线程,并让工厂方法简单地返回一个
结果

优点是:

  • 对象创建时无需记录的副作用
  • 线程仅在需要时创建(在调用
    write()
    的情况下),而不是在每次创建
    对象时创建
  • 你不需要额外的课程
  • 这是一个干净的实现,每个了解工厂设计模式的人都能一目了然
见:

为了良好的OO实践,我会使用一个接口:

interface Fruit {

  void write(Writer out);

  ...
}  // Fruit
it的抽象实现:

public abstract class AbstractFruit implements Fruit {

  Data data;

  public void write(Writer out) {
    ...
  }

  ...
}  // AbstractFruit

为了方便起见,我会使用特定的
get…()
方法(通常是这样):

枚举

interface Fruit {

  enum Type {
    APPLE,
    MANGO 
  }

  void write(Writer out);

  ...
}  // Fruit

有关以下内容,请参阅API文档:

未检查的异常不需要在方法[…]抛出子句中声明


这很直截了当。你有什么策略?练习的目标是什么?学习低级线程处理?学习如何使用执行者?学习如何使用并行流?这看起来不像是一个典型的使用多线程的用例,所以选择你想要学习的东西(或者你的老师想要你学习的东西)。@JBNizet我需要这样做来编写两个不同的相当大的文件。苹果和芒果只是一个例子。如果我按顺序做,这个过程就太慢了。在现实生活中,我几乎没有做过太多的线程处理,所以我想在如何设计它时非常小心。如果你在你的方法中所做的只是写入一个文件,而两个文件都在同一个磁盘上,使它多线程可能会使它变慢,而不是变快。但您可以通过简单地使用并行流或带有两个线程的执行器来测试它。@shashwatZing如果您需要创建两个或更多实例,并且它们必须同时运行,您将需要使用线程。这非常简单。你有什么策略