dori naji
dori naji

Reputation: 980

can i know the Thread runnable class attributes in java?

probability this question have been asked before but i cant find anything in my searching mechanism. I am trying to create a multiple threads, in an array list but i want to retrieve them from an arraylist and filter them by the attribute of w1 i used in my code. any ideas ?

    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
    }

this is FirstWorker class

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;
    }
}

Upvotes: 1

Views: 1536

Answers (4)

Ricardo Zuasti
Ricardo Zuasti

Reputation: 151

You should consider using the new java.util.concurrent package. What you are trying to do can be implemented a lot easier and intuitively with an ExecutorService and a collection of Callables.

Check out this sample and the Executors API (specifically the fixedThreadPool).

Upvotes: 0

thkala
thkala

Reputation: 86353

If you want to retrieve properties from the Runnable instance within a Thread object, I do not believe that is generally possible, since the Thread class does not have a method to return its target Runnable object.

That said, you can always extend the Thread class itself, which would allow you to use instanceof with the Thread instances themselves before casting and getting to whatever property you need.

Keep in mind, though, that extending Thread is not a recommended practice and if you are intending to get the result of some computation straight from the Runnable object, you could run into some severe trouble if you are not careful.

In any case, recent Java versions (i.e. 1.5+) offer substantial capabilities for concurrency and I suspect that your application would benefit from a re-design that uses them.

We might be able to help more if you explained what exactly you are trying to do in broader terms...

Upvotes: 0

Thomas
Thomas

Reputation: 5094

If you want to access a member variable of your Runnable, you should extend Thread instead of implementing Runnable. Also, don't extend Thread AND implement Runnable. Pick one.

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

}

Upvotes: 2

sMoZely
sMoZely

Reputation: 379

The simple answer with constructing a Thread with a Runnable is no.

The constructor for Thread that you are using accepts a Runnable ... I assume that FirstWorker implements the Runnable interface.

But looking at the API docs for Thread http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html there is no method to return the Runnable.

Without knowing more context about what your trying to do, the simplest approach might be to change FirstWorker to extend Thread then the loop you have would work.

That would probably work, but would need to know more about what your doing to reccomend anything else.

Upvotes: 0

Related Questions