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

1
  • 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! Commented Aug 8, 2018 at 13:02

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Still you got my vote ... just for that last statement ;-)
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.

5 Comments

Hi I want that non stream solution. Any link will be helpful
@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 ...
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
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.
@Bachas But as written, I enhanced my answer with that link you asked for, so please consider accepting/upvoting my input at some point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.