[Solved] Run java function in thread


As a partial solution for the functions / methods (if they don’t need arguments) you can use Threads or an ExecutorService and method references.

If you need arguments you will have to write lambda expressions – see the method t3 and it’s start for an example.

public class Test {

  public void t1() {
    System.out.println("t1");
  }

  public void t2() {
    System.out.println("t2");
  }

  public void t3(int n) {
    System.out.println("t3:"+n);
  }

  public static void main(String[] args) throws InterruptedException {
    Test test = new Test();
    Thread t = new Thread(test::t1);
    t.start();
    ExecutorService es = Executors.newFixedThreadPool(5);
    es.submit(test::t2);
    es.submit(() -> test.t3(99));
    es.shutdown();
    es.awaitTermination(1, TimeUnit.SECONDS);

  }
}

3

solved Run java function in thread