Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量传递到方法(Java)_Java_Android_Variables_Methods - Fatal编程技术网

将变量传递到方法(Java)

将变量传递到方法(Java),java,android,variables,methods,Java,Android,Variables,Methods,我是Java新手,我有一个问题。我从Android教程中复制了一些代码,现在我想将一个整数变量传递到run()方法中,这样我就可以为每个循环递增它,然后在后台线程外捕获它。我该怎么做 int gg= 0; Thread background = new Thread(new Runnable() { public void run() { try {

我是Java新手,我有一个问题。我从Android教程中复制了一些代码,现在我想将一个整数变量传递到run()方法中,这样我就可以为每个循环递增它,然后在后台线程外捕获它。我该怎么做

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
intgg=0;
线程背景=新线程(newrunnable(){
公开募捐{
试一试{

while(pbardilog.getProgress()您不能为run()方法指定参数。您可以将int变量声明为字段,并在内部类中使用它

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

}

编辑:(来自@alf的建议)您可以将
volatile
与字段一起使用,以便所有其他线程可以立即看到更改的值。

您不能为run()方法指定参数。您可以将int变量声明为字段,并在内部类中使用它

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

}
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);
}
编辑:(来自@alf的建议)您可以将
volatile
与字段一起使用,以便所有其他线程可以立即看到更改后的值。

private volatile int gg;
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);
}
公共方法(){ 线程背景=新线程(newrunnable(){ @凌驾 公开募捐{ 试一试{ while(pbartialog.getProgress()
private volatile int gg;
公共方法(){
线程背景=新线程(newrunnable(){
@凌驾
公开募捐{
试一试{

虽然(pbardilog.getProgress()拥有自己的类并使用其构造函数传递计数器,但我还没有尝试过,但我会从以下内容开始:

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

拥有自己的类并使用其构造函数传递计数器,我没有尝试过,但我会从以下内容开始:

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

如果我是你,我会研究,即
incrementAndGet()
方法


设置
gg
字段确实会给线程访问
gg
的权限,
volatile
会使更改可见,但是由于您的意图不明确,我不能确定您是否没有其他线程增加相同的值:您没有原子性,因此只要有多个线程执行
gg++
,您可能会得到错误的结果。

如果我是您,我会研究,即
incrementAndGet()
方法



设置
gg
字段确实会给线程访问
gg
的权限,
volatile
会使更改可见,但是由于您的意图不明确,我不能确定您是否没有其他线程增加相同的值:您没有原子性,因此只要有多个线程执行
gg++
,您很可能会得到错误的结果。

捕获变量是什么意思?顺便说一句,我认为应该在UI线程上调用pbardilog.disclesh(),即在处理程序中。捕获意味着打印出来。任何要传递给内部类的操作都可以使用“final”完成(但是如果你想修改它,你必须将变量的值复制到另一个变量上)你说“捕获”变量是什么意思?顺便说一句,我认为应该在UI线程上调用pbardilog.disclesh(),即在Handler中。捕获是指打印出来。你想传递给内部类的任何事情都可以使用“final”来完成(但如果要修改,则必须将变量的值复制到另一个变量)你不能
++
一个最终变量,可以吗?我从来没有遇到过问题。我已经编辑了我的答案,以包含一个备选方案。调用此.gg=gg将不起作用,因为gg是最终变量。既然目标是更改其值,为什么要将其设为最终变量?你也不能++一个最终参数。现在它几乎是正确的。gg必须是可变的,或者是主thread可能总是看到初始值。使用公共字段是不好的做法。你不能
++
一个最终变量,可以吗?我从来没有遇到过问题。我已经编辑了我的答案,以包含一个备选值。调用这个.gg=gg将不起作用,因为gg是最终的。既然目标是更改它的值,为什么要将它设为最终变量?你不能更改它的值al参数也是。现在它几乎是正确的。gg必须是volatile,否则主线程可能总是看到初始值。使用公共字段是不好的做法。请注意,最好是
volatile
,以确保可见性。他必须使用volatile,否则计数器值没有意义。请注意,最好是
volatile
,以确保可见性易失性。他必须使用volatile,否则计数器值毫无意义。为什么要使用
Integer
而不是
int
?计数器应该是volatile的,或者应该始终从同步块访问。否则,主线程可能会看到10作为值,即使后台线程将其设置为1000000。@JBNizet感谢我修复了它,当我意识到我的失败时,我正在去工作的路上。幸运的是,我没有像我想象的那样被严重否决。为什么要使用
Integer
而不是
int
?计数器应该是易变的,或者应该总是从同步块访问它。否则,即使后台线程设置为t到1000000。@jbnize谢谢我把它修好了,当我意识到自己的失败时,我正在上班的路上。幸运的是,我没有像我想象的那样被严重否决。