0

I'm new to Java, and I have a problem. I've copied some code off a tutorial for Android, and now I want to pass an integer variable into the method run(), so I can increment it for each loop and then catch it outside the background Thread. How would I do that?

int gg= 0;    
Thread background = new Thread(new Runnable() {
                    public void run() {
                        try {

                            while (pBarDialog.getProgress() <= 100) {

                                Thread.sleep(100);
                                gg++; // the increment here
                                progressHandler.sendMessage(progressHandler
                                        .obtainMessage());


                            }
                            if (pBarDialog.getProgress() == 100) {
                                pBarDialog.dismiss();

                            }

                        } catch (java.lang.InterruptedException e) {
                            // if something fails do something smart
                        }
                    }

                });
          //catch gg here
3
  • What do you mean by 'catching' the variable? Btw, I believe that pBarDialog.dismiss() should be called on the UI thread, namely in Handler. Commented Jan 13, 2012 at 8:00
  • By catching I mean printing it out. Commented Jan 13, 2012 at 8:02
  • anything you want to pass to an inner class can be done by using "final" (but then you must copy the value of the variable to another one if you want to modify it) Commented Jan 13, 2012 at 9:14

4 Answers 4

3

You can't specify argument to the run() method. You may declare int variable as field and use it in inner classes.

public class TestActivity extends Activity
{
   private volatile int no;
   .....

}

EDIT: (Suggestion from @alf) You can use volatile modifier with field so changed value can be seen immediately by all other threads.

Sign up to request clarification or add additional context in comments.

2 Comments

Note it should better be volatile to ensure visibility.
He MUST use volatile, else the counter value makes no sense.
1

Have your own class and pass the counter using its constructor, I haven't tried that, but I would start with something like that:

class MyThread implements Runnable {

   private volatile int counter;

   public MyThread( int counter ) {
       this.counter = counter;
   }

   public void run() {
   ...
   }

   public getCounter() {
      return counter;
   }
}

MyThread mt = new MyThread( 10 );
Thread t = new Thread( mt );
t.start();

// after some time
t.getCounter();

2 Comments

Why use Integer rather than int? The counter should be volatile, or it should always be accessed from a synchronized block. Else, the main thread could aways see 10 as the value, even if the background thread sets it to 1000000.
@JBNizet Thanks I fixed it, I was on my way to work as I recognized my failure. Fortunatly I wasn't that heavily downvoted as I thought.
0
private volatile int gg;

public void myMethod() {
    Thread background = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                while (pBarDialog.getProgress() <= 100) {
                    Thread.sleep(100);
                    gg++; // the increment here
                    progressHandler.sendMessage(progressHandler.obtainMessage());
                }
                if (pBarDialog.getProgress() == 100) {
                    pBarDialog.dismiss();
                }
            } catch (java.lang.InterruptedException e) {
                // if something fails do something smart
            }
        }

    });

    System.out.println(gg);
}

6 Comments

You cannot ++ a final variable, can you?
I've never had a problem with it. I've edited my answer to include an alternative.
Calling this.gg = gg won't work, since gg is final. Why do you make it final since the goal is to change its value?
You can't ++ a final argument either.
Now it's almost correct. gg must be volatile, or the main thread might always see the initial value. And using public fields is bad practice.
|
0

If I were you, I'd be looking into AtomicInteger, namely the incrementAndGet() method.

Making gg a field will indeed give the thread access to gg, and volatile will make the changes visible, but since your intentions are not clear, I cannot be sure that you don't have other threads incrementing the same value: you don't have atomicity, so as soon as you have more than one thread doing gg++, you're likely to get wrong results.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.