将参数传递给 Java 线程 [英] Passing parameters to Java Thread

查看:53
本文介绍了将参数传递给 Java 线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了:怎么能我将参数传递给 Java 线程?

但我不知道具体如何使用它.所以我制作了简单的示例代码来节省您的宝贵时间:

But I don't know how exactly use that. So I made easy samle code to save your precious time :

class ThreadParam implements Runnable { 
static int c;

public ThreadParam(int a, int b){
    int c = a+b;
}

public void run(){
    System.out.println(c);
}

}

public class ThreadParamTest {
public static void main(String args[]){
    Runnable r = new ThreadParam(1000,2000);
    new Thread(r).start();  
}   
}

为什么这个结果是 0 ?我认为这应该是 3000.也许变量int c"不是调度到 run() 方法.我该如何解决这个问题?

Why is this result 0 ? I think that should be 3000. Maybe the variable "int c" isn't dispatch to run() method. How can I solve this issue?

推荐答案

我认为static int c"的选择是不正确的,因为这意味着 ThreadParam 的所有实例都将共享"(并且在这方面做得很差)一个共同的值对于 c.也就是说,如果您有 2 个单独的 ThreadParams 同时运行,其中一个可能为 C 提供错误"值.考虑...

I think the choice of "static int c" is incorrect as it means that all instances of ThreadParam will "share" (and poorly at that) a common value for c. That is to stay, if you have 2 separate ThreadParams going simultaneously, one of them is likely present the "wrong" value for C. Consider...

class BadThreadParam implements Runnable {
    static int c;

    public BadThreadParam( int a, int b ) {
        c = a + b;
    }

    public void run() {
        System.out.println( c );
    }
}

class ImmutableThreadParam implements Runnable {
    private final int c;

    public ImmutableThreadParam( int a, int b ) {
        c = a + b;
    }

    public void run() {
        System.out.println( c );
    }
}

public class BadThreadParamTest  {
    public static void main( String[] args ) {
        BadThreadParam shouldBe3 = new BadThreadParam( 1, 2 );
        BadThreadParam shouldBe5 = new BadThreadParam( 3, 2 );
        shouldBe3.run();  // Expect 3 but is 5.  WTF?
        shouldBe5.run();  // Expect 5.

        ImmutableThreadParam expect3 = new ImmutableThreadParam( 1, 2 );
        ImmutableThreadParam expect5 = new ImmutableThreadParam( 3, 2 );
        expect3.run();  // Expect 3.
        expect5.run();  // Expect 5.
    }
}

如果将c"设置为实例本地,则可以解决2 个单独的 ThreadParams 影响相同值"的问题.如果您将private int c"设为final,您就避免了同步的需要.如果您需要在运行中(或从外部)向下突变c",现在您正在进入同步世界...

If you make the "c" local to the instance, you overcome the "2 separate ThreadParams are affecting the same value" problem. If you make the "private int c" final, you are avoiding the need for synchronization. If you need to mutate "c" down in the run (or from the outside), now you are entering the world of synchronization...

class ThreadSafeMutableThreadParam implements Runnable {
    private int c;

    public ThreadSafeMutableThreadParam( int a, int b ) {
        c = a + b;
    }

    public synchronized void setC( int c ) {
        this.c = c;
    }

    public synchronized int getC() {
        return c;
    }

    public void run() {
        System.out.println( getC() );
    }
}

除此之外,tuxdna 在描述如何将参数传递给 Runnable"方面是正确的.Runnable 无关紧要;您正在将参数传递给一个类(但是您实现了这一点).如果您需要它们在 run() 中可用,则需要注意同步.

Other than that, tuxdna's is correct in describing how you "pass params to a Runnable". The Runnable is inconsequential; you are passing params to a class (however you achieve that). If you need them available down in a run(), you need to be aware of synchronization.

这篇关于将参数传递给 Java 线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆