0

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

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Bachas
  • 233
  • 6
  • 16
  • Unrelated: please read about java naming conventions. Class names go UpperCase, always. Also use meaningful names, such as calling a list of students, maybe `students`, and not `student`! – GhostCat Aug 08 '18 at 13:02

2 Answers2

1

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Hi I want that non stream solution. Any link will be helpful – Bachas Aug 09 '18 at 05:48
  • @Bachas The real problem here is: your requirements are very much unclear. Do you want all threads to print all elements, or do you want that the first thread prints the first 10 elements, the second the next 10? You see, you actually did **not** put much effort in your question. You shouldn't expect that anything comes back than that would require effort ... – GhostCat Aug 09 '18 at 06:30
  • actually in there is a method. in start of that method there is loop start. loop size can be 2,23,50 any size. normally single loop taking 12 min to execute. means more loop much time consuming. I want to execute this with multi threading at least four threads together. so the loop take less time – Bachas Aug 09 '18 at 07:38
  • See my updated answer. But then: you absolutely must **study** how multi threading works in Java. Honestly again: your question reads like "I want to build a skyscraper, now somebody tell me how to hold the shovel to dig the basement". You have to sit down and *learn* how multithreading works. There is no point in somebody giving you a bit of code that you don't understand! Of course you can start with looking at solutions from other people, but in the end, you will be writing code. You have to understand what you need. – GhostCat Aug 09 '18 at 07:44
  • @Bachas But as written, I enhanced my answer with that link you asked for, so please consider accepting/upvoting my input at some point. – GhostCat Aug 09 '18 at 07:45
1

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.

matt
  • 10,892
  • 3
  • 22
  • 34