Home > front end >  Java I need to get a variable from a different running class as that class update the vairable
Java I need to get a variable from a different running class as that class update the vairable

Time:12-08

I am looking for a way to get the value of a variable from another running class as the class is updating the variable

I basically trying to copy a file in chunks but what to update the progress on a different java class

So basically:

Class copyfile variable copied data --> check progress class variable show progress

I am new to java so I made a lot of mistakes already

public class Class_one
{

    public static void main(String[] args) throws InterruptedException {
        Class_three class_three = new Class_three();
        while(true) {
            Class_one class_one = new Class_one();
            long test = class_three.data();
            System.out.println(test);
        }

    }
}
public class Class_two {
    public static void main(String[] args) throws InterruptedException {
        Class_three class_three = new Class_three();
        class_three.generatea();
    }
}

public class Class_three {
    public static long a ;
    public long c ;
    public void generatea() throws InterruptedException {
        for (long b = 0 ; b < 100000000000L; b  ){

            Thread.sleep(1000);
            a = b;
            System.out.println("generatea : "   a);


            //this.c  ;
        }

    }
    public  long data() throws InterruptedException {

         long b = a;
        System.out.print("a : "   "\tb : "   b);
        return b;
    }

}

So Class_one need to be able to get Class_three value of global a but Class_two is running Class_Three

Hope it makes sense

CodePudding user response:

what about with thread?

class Test {
    public static void main(String[] args) {
        Class_three class_three = new Class_three();
        new Thread(() -> {
            while (true) {
                long test = 0;
                try {
                    test = class_three.data();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(test);
            }
        }).start();

        new Thread(() -> {
            try {
                class_three.generatea();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }
}

CodePudding user response:

From your description it is clear that you are not sure who controls the progress. There are definitely several ways to solve your problem. Let's say your classes are named Copy and Observer.

Method 1: Copy controls and receives an instance of Observer in the constructor. Copy has a copy loop that calls a method in Observer after each iteration. The progress could then be passed to the method in Observer.

Example:

// a test class with your main method
public class Test {
    public static void main(String[] args) {
        Observer observer = new Observer();
        Copy copy = new Copy(1000, observer);
        copy.start();
    }
}


// the oberver class
public class Observer {
    public void progress(int current, int blocks) {
        System.out.println(current   "/"   blocks);
    }
}


// the copy class
public class Copy {
    public final int blocks;
    private Observer observer;

    public Copy(int blocks, Observer observer) {
        this.blocks = blocks;
        this.observer = observer;
    }

    public void start() {
        for(int current = 1; current <= blocks; current  ) {
            observer.progress(current, blocks);
        }
    }
}

Method 2: Observer controls and receives an instance of Copy in the constructor. Observer has a loop that calls a method in Copy in each iteration as long as it returns True as a result. The progress could then be read from a variable, for example.

Example:

// a test class with your main method
public class Test {
    public static void main(String[] args) {
        Copy copy = new Copy(1000);
        Observer observer = new Observer(copy);
        observer.start();
    }
}


// the oberver class
public class Observer {
    private Copy copy;

    public Observer(Copy copy) {
        this.copy = copy;
    }

    public void start() {
        while (copy.next()) {
            System.out.println(copy.getCurrent()   "/"   copy.blocks);
        }
    }
}


// the copy class
public class Copy {
    public final int blocks;
    private int current = 1;

    public Copy(int blocks) {
        this.blocks = blocks;
    }

    public boolean next() {
        current  ;
        return current <= blocks;
    }

    public int getCurrent() {
        return current;
    }
}

Method 3: Observer runs in its own thread and gets an instance of Copy in the constructor. Copy has a copy loop and updates a variable with the progress after each iteration. Observer has an observer loop and reads the progress from the variable in Copy on each pass. When the progress is complete, the observer ends its loop.

As of this writing, a thread-based answer already exists.

  •  Tags:  
  • java
  • Related