Gregor Menih
Gregor Menih

Reputation: 5126

Passing variable into methods (Java)

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

Upvotes: 0

Views: 1569

Answers (4)

stacker
stacker

Reputation: 68962

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();

Upvotes: 1

alf
alf

Reputation: 8513

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.

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94645

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.

Upvotes: 3

user882347
user882347

Reputation:

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);
}

Upvotes: 0

Related Questions