开发者

Java Threads (multithreading)

How can 开发者_运维知识库I give input to threads in java

eg:

class BTT extends Threads
{
  int ag1;
  int ag2;

  void build(int k)
  {
     void build(k+1);
  }
}

I want to set the values of ag1 and k in build different in different threads. How can i do that?


you treat your thread class as a simple POJO only (Java object) which can multiple values, i don't think that there should be such a big problem.

you can use something like the code below.

public class P1 extends Thread {

    private int age;

    public P1(int age) {
        this.age = age;
    }

    public void run() {
        System.out.println(this.age);
    }
    public static void main(String[] args) {
        P1 p1 = new P1(1);
        p1.start();
    }
}


well when you create your thread you can do things like

MyThread mt = new MyThread(agr1, arg2);

mt.setValue(k);

but Java Threads usually uses Runnables http://www.javabeginner.com/learn-java/java-threads-tutorial

in that case you can do the same

MyRunnable mr = new MyRunnable(arg1,arg2);
mr.setValue(k);

new Thread(mr).start();

Jason

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜