简体   繁体   中英

int set to zero generates new int by the same name tho

When I create an int COun that increments with the following function, once it gets to desired int to set it back to zero, the COun is set to zero and starts incrementing again until 10 but it increments another int, by the same name I assume. Why does it do that?

Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask()
    {
        public void run()
        {
           // Your code

            counterr++;
            System.out.println("COun "+counterr);
           // System.out.println("Refresh?  "+refresh);
             if(counterr>10){
                 json = jParser.makeHttpRequest(url_all_products, "GET", params);
                 Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
                 startActivity(i);
                 counterr=0;

             }
        }
    }, delay, period);

When you print out COun, you can tell it starts by just incrementing one COun, but then every time if sentence kicks in, it generates another COun counter.

12-20 18:50:16.170: I/System.out(10465): COun 1

12-20 18:50:17.071: I/System.out(10465): COun 2

12-20 18:50:18.082: I/System.out(10465): COun 3

12-20 18:50:19.083: I/System.out(10465): COun 4

12-20 18:50:20.084: I/System.out(10465): COun 5

12-20 18:50:21.085: I/System.out(10465): COun 6

12-20 18:50:22.086: I/System.out(10465): COun 7

12-20 18:50:23.077: I/System.out(10465): COun 8

12-20 18:50:24.078: I/System.out(10465): COun 9

12-20 18:50:25.079: I/System.out(10465): COun 10

12-20 18:50:26.080: I/System.out(10465): COun 11

12-20 18:50:27.071: I/System.out(10465): COun 1

12-20 18:50:28.082: I/System.out(10465): COun 2

12-20 18:50:29.073: I/System.out(10465): COun 3

12-20 18:50:30.083: I/System.out(10465): COun 4

12-20 18:50:31.084: I/System.out(10465): COun 5

12-20 18:50:31.405: I/System.out(10465): COun 1

12-20 18:50:32.085: I/System.out(10465): COun 6

12-20 18:50:32.406: I/System.out(10465): COun 2

12-20 18:50:33.086: I/System.out(10465): COun 7

12-20 18:50:33.407: I/System.out(10465): COun 3

12-20 18:50:33.407: I/System.out(10465): COun 8

12-20 18:50:33.407: I/System.out(10465): COun 4

12-20 18:50:33.407: I/System.out(10465): COun 9

12-20 18:50:33.407: I/System.out(10465): COun 5

and it goes on weirdly like that, when either one becomes 10, it generates counting another one, along with the original, and on and on...

If you want to execute it once then you should call

schedule(TimerTask task,Date time)

or

schedule(TimerTask task, long delay)

scheduleAtFixedRate(TimerTask task,long delay,long period) is when you need it to run repeatedly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM