java 8 的多线程示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39515447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:26:00  来源:igfitidea点击:

Example of multithreading of java 8

javamultithreadinglambda

提问by Frank Pentangeli

I need a Java 8 example of multi-threading.

我需要一个 Java 8 多线程示例。

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.

在下面的示例中,我遇到了 Thread.currentThread().getName() 问题,我需要使用 lambda 表达式。

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());
    }
}

回答by JynXXedRabbitFoot

The Lambda can be easily accomplished with an IntStream.

Lambda 可以通过 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());
  }
}

回答by Jhonny007

For lambda I suggest:

对于 lambda 我建议:

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

或者如果你想全力以赴使用 lambda

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())));
    }
}

回答by Sam

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

希望这可以帮助某人。Thread创建了一个 3 s的池并并行运行。

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();

}

}

}