Adam Howard
Adam Howard

Reputation: 301

Concurrent Method

I have a class that has one method that serves the most work. I want to make multiple calls to this method, running it concurrently (to have multiple searches simultaneously). The method I want to call uses my class's local attributes so I can't simply create a new class with this method in it as it won't be able to access my other class' attributes as they have different memory space.

Example:

class mainWork { 
    static int localInt;
    static String testString;

    public static void main(){
        new Runnable(){
            public void run(){
                doWork();
            }
        }.run();
    }

    public void doWork(){
        localInt = 1;
        testString = "Hi";
    }
}

Creating an anonymous Runnable inner class does not work as that thread can't access mainWorks attributes. If I create a separate class, extend Thread I have the same problem. Is there a way (maybe not using threads at all) I can call a method that will still have access to the attributes from the class that is calling it while running simultaneous calls? I want to call doWork many times at once to speed up an operation. Tasks maybe?

Upvotes: 0

Views: 325

Answers (2)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59978

You have a many problems in your code :

  • Wrong main method declaration
  • To call a method you have to use () -> doWork();
  • You have to use ; in the end of your statement new Runnable() {...};
  • You can't assign a String to a int testString = "Hi";

Your code should look like :

int localInt;
String testString;

public static void main(String[] args) {

    new Runnable() {
        public void run() {
            MainWork a = new MainWork(); 
            a.doWork();
        }
    };

}

public void doWork() {
    localInt = 1;
    testString = "Hi";
}

Until now your program will compiled, but do nothing, to start your thread you have to use :

Runnable r = new Runnable() {
    @Override
    public void run() {
        MainWork a = new MainWork(); 
        a.doWork();
    }
};

new Thread(r).start();

Another point, don't use lower letter in first name of your class mainWork instead you have to use MainWork

Upvotes: 1

Guilherme
Guilherme

Reputation: 623

Inspired by your question, I made this short example for deal with parallel processing without concerning about threads, this is a "non blocking code" that can be easier to implement.

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 *JAVA 8 introduces a new concept for dealing with concurrencies  
 *CompletableFuture
 */

class MainWork {
    static int localInt;
    static String testString;

    public static void main(String args[]) throws IOException {

        CompletableFuture.supplyAsync(MainWork::doWork).thenAccept(System.out::println);
        System.in.read(); //this line is only to keep the program running
    }

    public static List<String> doWork() {
        localInt = 100;
        testString = "Hi";

        List<String> greetings = new ArrayList<String>();
        for (int x = 0; x < localInt; x++) {

            greetings.add(testString);
        }
        return greetings;
    }
}

Upvotes: 0

Related Questions