简体   繁体   中英

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

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.

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

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.

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