"pool.execute" (Thread pool executer) usage in Java 7

Some piece of code from my project is as follows:

 pool.execute(() -> 
            {   
                boolean flag = true;
                while (flag)
                {
                    if (ev3ColorSensor.getColorID() == Color.BLACK)
                    {
                        LuuMa.setSwitching(true);
                        int randomInt = randomGenerator.nextInt(5) + 1;

                        if (randomInt == 1)
                        {
                            try {
                                pause();
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        else if (randomInt == 2)
                        {
                            fastOff();
                        }
                        else if (randomInt == 3)
                        {
                            intimidate();
                        }
                        else if (randomInt == 4)
                        {
                            try {
                                peekFastOff();
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                        else if (randomInt == 5)
                        {
                            psych();
                        }
                        else if (randomInt == 6)
                        {
                            try {
                                delay();
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }

                    LuuMa.setSwitching(false);
                }
            });

I'm working on a lejos ev3 project. And Lejos ev3 doesn't support Java 8. For this reason, I must use Java 7 to compile my project on the ev3 brick.

I need to use pool.execute for parallel processing. But Java 7 doesn't support it. How can I solve the problem, or which alternative can I use in Java 7?

Thanks,

Jon Skeet
people
quotationmark

You could just use an anonymous inner class instead - Java 7 doesn't support lambda expressions, but it certainly supports parallel execution.

pool.execute(new Runnable() {
    @Override public void run() {
        // Code here
    }
});

Obviously change Callable and run to whatever the suitable interface and method names are in this case - we don't know the type of pool in your code. If the type of pool isn't supported in Java 7, you could use something like ExecutorService.

people

See more on this question at Stackoverflow