我有一个接口说VegetableCreation
与方法:有一个接口两种不同的实现在不同的线程中执行
public void writeVeggieDataIntoFile();
和两个不同的类名为Apple
和Mango
实现VegetableCreation
。
而且还有一个工厂类VegetableFactory
与create()
方法:
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使用等将受到高度赞赏。
P.S:我应该叫它水果**,而不是蔬菜*
这很简单。你有什么策略 – hhafeez
练习的目标是什么?学习低级线程处理?学习如何使用执行者?学习如何使用并行流?这看起来不像一个典型的使用多线程的用例,所以选择你想学的东西(或者你的老师希望你学习的东西)。 –
@JBNizet我需要这样做来编写两个不同的相当大的文件。苹果和芒果只是一个例子。如果我按顺序执行该过程,则速度太慢。我在现实生活中几乎没有做过太多的线程处理,所以想要真正小心我的设计。 – shashwatZing