Search code examples
javaoverridingrunnable

Runnable not work


Hi i create a test java code but class runnable not work. the code inside runnable builder work but override run not work

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

public class Test
{
    //timer for change zone
    private Timer timeZoneChange;

    Test()
    {
        timeZoneChange = new Timer();

        timeZoneChange.schedule(new TimeForChangeZone(), 1 * 1000);
    }


    public class TimeForChangeZone extends TimerTask
    {
        @Override
        public void run()
        {
            System.out.println("testing..");
            new UpdatePvpFlagZone(false);
            //change zone flag
            timeZoneChange.schedule(new TimeForChangeZone(), 1 * 60000);
        }
    }

    public class UpdatePvpFlagZone implements Runnable
    {
        //remove pvp flag
        private boolean removePvpFlag;

        public UpdatePvpFlagZone(boolean removePvpFlag)
        {
            removePvpFlag = removePvpFlag;
        }

        @Override
        public void run()
        {
            for (int i = 0; i<10; i++)
            {
                if (i == 5) 
                    break;
                //update flag
                if (!removePvpFlag) 
                {
                    if (i == 6)
                        break;  
                    System.out.println(i);
                    break;
                }
                //remove flag
                if (i == 7)
                    break;
                System.out.println(i);
            }
        }
    }

    public static void main(String[] args)
    {
        new Test();
    }
}

console print this but inside override run not print.

> java Test
testing..

Solution

  • On this line new UpdatePvpFlagZone(false);, The constructor is called, but the runnable has yet to be started.

    Start the runnable like so:

    UpdatePvpFlagZone upfz new UpdatePvpFlagZone(false);
    upfz.run();