我有一個人的陣列列表ArrayList<Person>
。每個人隨機獲取它的名字和年齡。在Person
ArrayList
我用lambda表達式過濾了18歲。如何爲陣列列表的每個人創建一個線程
我的問題是我怎樣才能爲Arraylist
的每個過濾Person
創建Thread
。
我的代碼:
public class Person implements Runnable
{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void run() {
// to fill up later
}
}
public class Main {
private static int nameCount = 1;
public static List<Person> list;
public static void main(String[] args) {
list = new ArrayList<Person>();
for (int i = 0; i < 5; i++) {
list.add(new Person(getPersonName(), getPersonAge());
}
list.removeIf(p -> p.getage().equals(18));
for (Person person : list) {
System.out.println(persion);
}
}
public static String getPersonName() {
String nameto = "PERSON" + String.valueOf(nameCount);
nameCount++;
return nameto;
}
public static int getPersonAge() {
Random ran = new Random();
int ageperson = ran.nextInt(25) + 1;
return ageperson;
}
//how to make an instance of Thread after filtering the person
}
SO的圖像編碼一般不太好。 – Timo
歡迎來到SO :)關注@TimoSta建議,請修改您的問題並複製/粘貼您的代碼,而不是發佈屏幕截圖。 –
'Person implements Runnable'的想法聽起來很奇怪。 「運行」一個人意味着什麼?當你想在後臺線程中對一個人做更多的事情時,這意味着什麼? –