0

I am now studying classes and inheritance in Java. I made a simple rpg game. and Now I try to use the multithreading, but it does not work. I want the output to come out every 30 seconds. "It's been 30 seconds since the game started." like this.. The numbers will grow over time. What should I do? Actually, I can't speak English well and it can be awkward.. I'll wait for your answer. Thank you!

//import java.util.Timer;
import java.util.TimerTask;

public class Timer extends Thread {

   int count = 0;

   Timer m_timer = new Timer();
   TimerTask m_task = new TimerTask() {

        public void run() {
            count++;
            System.out.println("It's been 30 seconds since the game started.");
        }

    };

   m_timer.schedule(m_task, 1000, 1000);
};

Main:

public class Main {

    public static void main(String[] args) {
        Timer m_timer = new Timer();
        m_timer.start();
    }

}
d.j.brown
  • 1,822
  • 1
  • 12
  • 14
sera
  • 73
  • 1
  • 9
  • 1
    I think if you are just studying classes and inheritance for the first time, I think an RPG game is too complicated for a beginner. Why do you need multi-threading? Is it part of a school assignment? – markspace Oct 15 '17 at 16:31
  • You should **never rely** on `Timer#schedule` being accurate, **it is not**. Use a hard comparison instead, `long start = System.currentTimeMillis();`, `long current = System.currentTimeMillis;` and `long duration = current - start;`. Don't use parallel threads in such an uncontrolled environment. You should first organize a well structure with a central **logic** (often called `tick`) and **render** method. There you can count game time and trigger other computation. – Zabuzard Oct 15 '17 at 16:39
  • yes... school assignment. I didn't make the game too difficult. simple game. I'm adding multithreading here.It's very difficult....:( – sera Oct 15 '17 at 16:42
  • I do not understand what you say. Could you explain it in code...? – sera Oct 15 '17 at 16:59
  • Several errors in your code. It does not even compile. Please provide a minimal, complete, and verifiable example of the code where you have a concrete problem. And ask a concrete question. "I'll wait for your answer." is definitely not enough to help you. – Heri Oct 16 '17 at 18:20

1 Answers1

0

If you're interested in learning about concurrency you could start by reading the Java Tutorial. I realize you said English is not your native language, but maybe you can follow the code presented in those tutorials.

It seems like you're just trying to implement a simple example so I'll offer the following code:

import java.util.Timer;
import java.util.TimerTask;

public class TimerMain {

    public static void main(String[] args) {
        Timer timer = new Timer();
        TimerTask task = new TimerTask(){
            private int count = 0;

            @Override
            public void run() {
                count++;
                System.out.println("Program has been running for " + count + " seconds.");
            }
        };
        timer.schedule(task, 1000, 1000);

        //Make the main thread wait a while so we see some output.
        try {
            Thread.sleep(5500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //Stop the timer.
        timer.cancel();
    }

}

As others have pointed out if you need a high degree of accuracy you should probably use a different approach. I found this question regarding timing accuracy.

D.B.
  • 4,523
  • 2
  • 19
  • 39