1

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;
    }
}
dori naji
  • 980
  • 1
  • 16
  • 41
  • 1
    Friendly advice: use `theCamelCase` for variable names in Java, it makes code much easier to read, especially for other Java developers. – bezmax Mar 23 '12 at 22:39
  • I'm not 100% sure of what you're asking, but perhaps you want an ArrayList of Futures not threads. Or rather than an ArrayList, perhaps better a PriorityQueue. – Hovercraft Full Of Eels Mar 23 '12 at 22:45
  • Err, unless I am mistaken, this code is invalid syntactically (`x.ProductsList`? huh?). Not to mention that it's impossible to tell what you are trying to do if all the class definitions and the variable declarations are missing... – thkala Mar 23 '12 at 23:05
  • @thkala I think that's pseudo code – keyser Mar 23 '12 at 23:12
  • @keyser5053: that does not make what the OP wants any clearer :-/ – thkala Mar 23 '12 at 23:14

4 Answers4

2

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

}
Thomas
  • 5,074
  • 1
  • 16
  • 12
0

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.

sMoZely
  • 369
  • 1
  • 10
0

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...

Community
  • 1
  • 1
thkala
  • 84,049
  • 23
  • 157
  • 201
0

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

Ricardo Zuasti
  • 151
  • 1
  • 5