Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;

import java.util.Random;

public class Main {

    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();;
    Random randomnum = new Random();

    public void moveNow() {
        int moveX = randomnum.nextInt(5);
        int moveY = randomnum.nextInt(5);
        System.out.println("Moving " + moveX + ", " + moveY);
    }

    public void startRandomMover() {
        executor.scheduleAtFixedRate(new Runnable() {
            public void run() {
                moveNow();
            }
        }, 0, 1, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        new Main().startRandomMover();
    }
}