我有一個接口說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