3

I need a Java 8 example of multi-threading.

I need to be able to manually select the number of threads.

In the example below I have a problem with Thread.currentThread().getName(), and I need to use a lambda expression.

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Thread { public static void main(String args[]) { ExecutorService service = Executors.newFixedThreadPool(10); for (int i =0; i<100; i++){ service.submit(new Task(i)); } } final class Task implements Runnable{ private int taskId; public Task(int id){ this.taskId = id; } @Override public void run() { System.out.println("Task ID : " + this.taskId +" performed by " + Thread.currentThread().getName()); } } 
3
  • 2
    Your main problem is you're naming your class Thread. so Thread.currentThread().getName is trying to find the method on your class, not on java.lang.Thread. And yes, the internet is full of multithreading examples that don't work. For the lambda part see How Runnable is created from Java8 lambda Commented Sep 15, 2016 at 16:04
  • What problem do you habe with Thread.currentThread().getName()? Commented Sep 15, 2016 at 19:02
  • just use java.lang.Thread.currentThread().getName() Commented Sep 15, 2016 at 19:38

3 Answers 3

10

The Lambda can be easily accomplished with an IntStream.

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.IntStream; public class ThreadLauncher { public static void main(String args[]) { ExecutorService service = Executors.newFixedThreadPool(10); IntStream.range(0, 100).forEach(i -> service.submit(new Task(i))); } } final class Task implements Runnable { private int taskId; public Task(int id) { this.taskId = id; } @Override public void run() { System.out.println("Task ID : " + this.taskId + " performed by " + Thread.currentThread().getName()); } } 
Sign up to request clarification or add additional context in comments.

Comments

6

For lambda I suggest:

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Main { public static void main(String args[]) { ExecutorService service = Executors.newFixedThreadPool(10); for (int i =0; i < 100; i++) { final int fi = i; service.submit(() -> System.out.println("Task ID : " + fi + " performed by " + Thread.currentThread().getName())); } } } 

or if you want to go all out on lambdas

public class Main { public static void main(String args[]) { ExecutorService service = Executors.newFixedThreadPool(10); IntStream.range(0, 100) .forEach(i -> service.submit(() -> System.out.println("Task ID : " + i + " performed by " + Thread.currentThread().getName()))); } } 

Comments

0

Hope this helps someone. A pool of 3 Threads are created and are run in parallel.

public class threadClass { ExecutorService executor = Executors.newFixedThreadPool(3); public void multiThread() { Runnable thread1 = () -> { // perform some operation System.out.println(Thread.currentThread().getName()); }; Runnable thread2 = () -> { // perform some operation System.out.println(Thread.currentThread().getName()); }; Runnable thread3 = () -> { // perform some operation System.out.println(Thread.currentThread().getName()); }; executor.execute(thread1); executor.execute(thread2); executor.execute(thread3); executor.shutdown(); } 

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.