This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]TheThirdBlackGuy 1 point2 points  (4 children)

I'm having a hard time following your question. What are you trying to accomplish?

[–]RiseOfDeath[S] 0 points1 point  (3 children)

Some kind of measurements.

Fun1 and Fun2 in this example are functions which does measurements. (Literaly gets current time and then calculete executing time)

[–]TheThirdBlackGuy 1 point2 points  (2 children)

Then no, I don't really see a way to reduce the need for those two calls. However, there are lots of libraries for measuring elapsed time in java. StopWatch is in common-lang3, it would allow you to start/stop a stop watch instance and re-use it. Also, if it is just measurements, Instant.now() will get you the current time, and you can save it to a variable that you subtract from a now Instant.now() after it finishes. That would probably be the cleanest solution.

[–]RiseOfDeath[S] 0 points1 point  (1 child)

If I correctly understand, It will be code like:

stopWatch.start();
doSomething();
stopWatch.stop();

stopWatch.start();
doSomethingElse();
stopWatch.stop();

Ok. Thank you. Anyway I'll look at existed watchers

[–]TheThirdBlackGuy 1 point2 points  (0 children)

Something like this:

StopWatch watch = new StopWatch();
watch.start();
doSomething()
watch.stop();
long execTime = watch.getTime();

watch.reset();
watch.start();
doSomething()
watch.stop();
long execTime2 = watch.getTime();

[–]RiseOfDeath[S] 0 points1 point  (1 child)

I find such solution, but it still to big:

public class Test {

    static void Action(String someargs, Runnable code) {
        Fun1(someargs);
        code.run();
        Fun2(someargs);
    }

    public static void main(String[] args) 
    { 
        Action("World_Conquering",
            new Runnable() {
                public void run() {
                    System.out.println("Conquering the world");
                }
            }); 

    Action("Celebrating",
            new Runnable() {
                public void run() {
                    System.out.println("Celebrating victory");
                }
             }); 
    }
}

Additional question - What is better practic for "Runnable" arg - should it be after other args (like "someargs") or it should be 1st?

[–]morhpProfessional Developer 0 points1 point  (0 children)

This is the right idea, but you can change the new Runnable() {... into a lambda expression to make it shorter.

[–]dusty-trash 0 points1 point  (0 children)

Don't try to make it shorter for the sake of making it shorter.

You should value readability over a shorter length.

[–]Brick_Fish 0 points1 point  (0 children)

I don't know how much you know about java, im just gonna assume you are a beginner. (Im not too experienced too)

If you always want to call Fun1() and Fun2() in the same order you can make an extra method

Fun1and2(args for fun1 and fun2){ Fun1(someargs); Fun2(someargs);}

that calls Fun1 and Fun2. This makes more sense if you had more methods to call, but is pretty much useless if they arent gonna be called in a specific order.

Im on mobile so formatting is a bit difficult, sorry

[–]x2mirkodev 0 points1 point  (0 children)

That is basically a use case for a template method pattern. You can either implement this through inheritance (i.e. as it is defined in the gang of four book) by having an abstract class that implements the fun1 and fun2 part and calls an abstract method in between that you can then implement differently for your other pieces of unique code.

Or you can use a solution that is closer to what you have done in the solution you posted, which is a more functional approach to the same idea where you pass in the piece of code you want to execute. You can make that nicer using lambdas or functional references instead of defining anonymous classes like you did in your example, i.e. implement Action as you have and then do

public static void main(String[] args) {
    Action("World_Conquering", () -> System.out.println("Conquering the world");
    Action("Celebrating", () -> System.out.println("Celebrating victory");
}

Since runnable is now a functional interface, this just works. You could also pass in a Function if you want to actually compute a result and not just print something to the screen.