Andrey Gaponenko
Andrey Gaponenko

Reputation: 1

Put class method into ThreadPoolExecutor in Java

I am new to Java and multithreading too. I've read about ThreadPool and saw that I can submit/execute lambda functions into ThreadPoolExecutor for example. But how can I do it for class methods in Java?

public class ParallelPointSystem{
    private ArrayList<Cluster> clusters;
    private ArrayList<Point> points;
    private int dimension;
    private int clusters_amount;
    private int iterations;

    private void attach_point(int i) throws Exception {
        double distance = Point.get_distance(this.points.get(i), this.clusters.get(0).get_current_center());
        int ind = 0;
        for(int j = 1; j < clusters_amount; j++){
            double dst = Point.get_distance(this.points.get(i), this.clusters.get(j).get_current_center());
            if(distance > dst){
                distance = dst;
                ind = j;
            }
        }
        this.clusters.get(ind).attach(this.points.get(i));
    }
}

I tried to wrap a method in a class that implements the Runnable interface, but is this the only way to do that? Am I doing it right?

 class Attach_Point implements Runnable{

    private int i;
    private ArrayList<Cluster> clusters;
    private ArrayList<Point> points;
    private int clusters_amount;
    public Attach_Point(int i, ArrayList<Cluster> clusters, ArrayList<Point> points, int cluster_amount){
        this.i = i;
        this.clusters = clusters;
        this.points = points;
        this.clusters_amount = cluster_amount;
    }

    @Override
     public void run(){
        double distance = Point.get_distance(this.points.get(i), this.clusters.get(0).get_current_center());
        int ind = 0;
        for(int j = 1; j < clusters_amount; j++){
            double dst = Point.get_distance(this.points.get(i), this.clusters.get(j).get_current_center());
            if(distance > dst){
                distance = dst;
                ind = j;
            }
        }
        try {
            this.clusters.get(ind).attach(this.points.get(i));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 }

Upvotes: 0

Views: 54

Answers (1)

DuncG
DuncG

Reputation: 15136

You don't actually need to implement Runnable. You only need to make sure to define a method on your class that matches the signature of public void run() and pass either lambda or method reference for your task.

For example this works:

class MyClass {
   public void doIt() {
       System.out.println("Hello "+this+" in "+Thread.currentThread());
   }
}

Example calls submitting above task:

ExecutorService pool = Executors.newFixedThreadPool(1);

// Submit as method reference:
pool.execute(new MyClass()::doIt);

MyClass obj = new MyClass();

// Submit as Runnable lambda:
pool.execute(() -> obj.doIt());

Obviously if you had MyClass implements Runnable and a run() method, simply use:

pool.execute(obj);

Upvotes: 2

Related Questions