Reputation: 243
there is an list of student entity.
List<Student> student = new Arraylist<Student>();
where
public student{
private id;
private name;
private class;
//setter and getter
}
By the foreach loop:
for(Student std : student){
System.out.println(std.getName());
}
above is the normal way. But how to print them with multithreading? three student details together print. means taking three threads toghter
Upvotes: 0
Views: 59
Reputation: 140417
A simple solution:
studentList.parallelStream().forEach(System.out::println);
This turns your list into a stream, and for each element in that stream, System.out.println() is invoked.
The non-stream solution is of course much more complicated. It would required you to define multiple threads, including a "pattern" how these threads work on that shared list.
For doing it with "raw threads": that is simply, straight forward stuff: you have to "slice" your data into buckets, and then define threads that work different buckets. See here as starting point.
Upvotes: 1
Reputation: 12347
This doesn't serve any practical purpose.
for(Student std:student){
new Thread(()->{
System.out.println(std.getName);
System.out.println(std.getName);
}).start();
}
This is also worse than the answer above.
Upvotes: 1