简体   繁体   English

如何在运行方法线程(Java)中获取类变量

[英]How can i get class variable in run method thread (Java)

My problem concern thread. 我的问题与线程有关。 i want to get var(class variable) from method run but can't get and i don't know what happed. 我想从方法运行中获取var(class variable),但无法获取,我也不知道发生了什么。

import java.util.ArrayList;

public class Interpreter_controller extends Thread{
    ArrayList<String> var;


    public Interpreter_controller(ArrayList<String> var) {
        this.var = var;
        System.out.println(this.var.isEmpty());
    }

    public void run() {
        System.out.println(this.var.isEmpty());
    }
}

when i start thread from 当我从开始线程

Interpreter_controller control = new Interpreter_controller(array_list_variable);
control.start();

Output is 输出是

false

true 真正

What should I do to get var from run method? 我应该怎么做才能从运行方法获得VAR?

New Edit 新编辑

I delete extends Thread then result is correct but i want to use thread in this class 我删除扩展线程然后结果是正确的,但我想在此类中使用线程

My goal is pass arraylist from another class to use in thread class(run method) 我的目标是从另一个类传递arraylist以在线程类中使用(运行方法)

My goal is pass arraylist from another class to use in thread class(run method) 我的目标是从另一个类传递arraylist以在线程类中使用(运行方法)

Take a look at this: 看看这个:

class Tmp implements Runnable {

  public Object runArg;

  public Tmp(Object arg) {
      runArg = arg;
  }

  public void run() {
      // thread job
  }
}

Use: 采用:

new Thread(new Tmp("Hello World")).start();

So you have to create a new class rather than using anonymous class. 因此,您必须创建一个新类而不是使用匿名类。

NOTE : Unless you are using J2ME, you should NOT use native Thread . 注意 :除非使用的是J2ME,否则不应使用本机Thread Many problems would occur with you. 您会遇到很多问题。 You should use ExecutorService by using Executors classes: 您应该使用ExecutorService使用Executors类:

private final ExecutorService pool = Executors.newCashedThreadPool();

...

pool.submit(new Tmp("HelloWorld"));

Hi Following will give you head start. 您好以下将使您领先。 Make sure list is synchronized. 确保列表已同步。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        Main m = new Main();
        m.execute();
    }

    public void execute() throws InterruptedException {
        List<String> list = new ArrayList<String>();
        list = Collections.synchronizedList(list);
        List<Thread> tlist = new ArrayList<Thread>();
        for (int i = 0; i < 5; i++) {
            Thread t = new SomeThread(list, i);
            t.start();
            tlist.add(t);
        }

        while (true) {
            int j = 0;
            for (int i = 0; i < tlist.size(); i++) {
                Thread.sleep(100);
                if (!tlist.get(i).isAlive()) {
                    j++;
                    for (String s : list) {
                        System.out.println(s);
                    }
                }
            }
            if (j == tlist.size()) {
                break;
            }
        }
    }

    public class SomeThread extends Thread {

        private List<String> list;
        private int number;
        private long sleepTime;

        public SomeThread(List list, int number) {
            this.list = list;
            this.number = number;
            sleepTime = new Random(number).nextInt(500);

        }

        @Override
        public void run() {
            try {
                Thread.sleep(sleepTime);
                list.add(number + " wake up from sleep");
            } catch (InterruptedException e) {
                System.out.println(e);
            }

        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM