java - 我可以知道java中Thread可运行类的属性吗?

标签 java multithreading attributes

这个问题可能以前已经被问过,但我在我的搜索机制中找不到任何东西。我正在尝试在数组列表中创建多个线程,但我想从数组列表中检索它们并通过我在代码中使用的 w1 属性过滤它们。有什么想法吗?

    w1 = new FirstWorker(ProductsList, OrdersList, s);
    FirstWorkerThread = new Thread(w1);
    ThreadArrayList.add(FirstWorkerThread);
    //I know i cant do the code below but i want to do that how ?
    for(Thread x : ThreadArrayList){
    x.ProductsList
    }

这是 FirstWorker 类

import java.lang.String;
import java.util.HashMap;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author Dimitris
 */
public class FirstWorker extends Thread implements Runnable  {

    private OrderList orderlist;
    private ProductList productlist;
    private String Worker;
    boolean Stop;
    private int speed = 1000;

    public FirstWorker(ProductList productlist, OrderList orderlist, String Worker) {
        this.productlist = productlist;
        this.orderlist = orderlist;
        this.Worker = Worker;
        this.Stop = true;
    }

    public void run() {
        if (Stop == true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }

            while (orderlist.returnLengthofOrder() != 0) {

                if (Thread.interrupted()) {
                    System.out.println("I am in the thread inturrupt");
                    // We've been interrupted: no more crunching.
                    return;
                }

                if (orderlist.getDone() == true) {
                } else if (orderlist.getDone() == false) {
                    orderlist.setDoneTrue();

                    orderlist.Purchased(Worker);
                    orderlist.setDoneFalse();

                    try {
                        Thread.sleep(this.speed);
                    } catch (InterruptedException e) {
                        return;
                    }


                }
            }

        }




    }

    public void setWork() {
        Stop = false;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
}

最佳答案

如果你想访问Runnable的成员变量,你应该扩展Thread而不是实现Runnable。另外,不要扩展 Thread 并实现 Runnable。选择一个。

public class MyThread extends Thread
{
    public int myarg;
    public void run()
    {
    }
}

public void useThread(int inputArgs[])
{
    ArrayList<MyThread> threadArray = new ArrayList<MyThread>();
    for (int arg : inputArgs)
    {
        MyThread temp = new MyThread(arg);
        temp.start();
        threadArray.add(temp);            
    }
    for (MyThread t : threadArray)
        System.out.println(t.myarg);

}

关于java - 我可以知道java中Thread可运行类的属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9847366/

相关文章:

java - HttpServletRequest无法执行getParameter操作

java - 在Selenium Java中从列表中选择随机元素并单击它,但getAttribute不等于0

Java:交换两行3维数组

python - 为什么只有一个工作线程的 ThreadPoolExecutor 仍然比正常执行速度更快?

c# - 是否可以获取对象的属性和关联属性?

c++ - 函数属性是否继承?

Java TreeMap 比较器

multithreading - Delphi 位图中的异步线程绘制

model-view-controller - 使用 spring 3 注释,jsr303 既没有获取 BindingResult 也没有获取 bean 名称 'dataForm' 的普通目标对象

C++ std::shared_ptr 递增和递减引用计数的竞赛