Syed Abdul Kather
Syed Abdul Kather

Reputation: 618

Array of threads in java

I need to know array of thread in java .Here is code for array of thread in C#

  Thread[] TCreate = new Thread[iThreadSize];
        for (int i = 0; i < TCreate.Length; i++)
        {
            TCreate[i] = new Thread(delegate()
                {
                    lst.Add(this.getResult(url));
                });
            TCreate[i].Name = "URL"+i;
            TCreate[i].Start();

        }
        for(int j=0;j<TCreate.Length;j++)
            while (TCreate[j].IsAlive)
            Thread.Sleep(10);

I need to know to do this in java .

MyTestClass qryCompoents=new MyTestClass();
for(int i=0;i<ThreadSize;i++)
        {

            switch(obj.getQueryLevelValue(i))  //Decide according to level
            {
            case 1:
                    qryCompoents.prepareAndProcess_I(obj.getQueryString(i),obj,i);
                    break;
            case 2:
                    qryCompoents.prepareAndProcess_II(obj.getQueryString(i),obj,i);
                    break;

            }               

        }

let me know if you need any details.

if i convert this

    Thread[] TCreate = new Thread[numExpression];

        for(int i=0;i<numExpression;i++)
        {
             TCreate[i] = new Thread(new Runnable() {
                    public void run() {
                        switch(obj.getQueryLevelValue(i))  //Decide according to level
                        {
                        case 1:
                                qryCompoents.prepareAndProcess_I(obj.getQueryString(i),obj,i);
                                break;
                        case 2:
                                qryCompoents.prepareAndProcess_II(obj.getQueryString(i),obj,i);
                                break;

                        }           
                    }
                });
                TCreate[i].setName("URL"+i);
                TCreate[i].start();         
        }
          for (int j = 0; j < TCreate.length; j++)
                while (TCreate[j].isAlive())
                     Thread.sleep(5);

Showing Error : Cannot refer to a non-final variable "obj" inside an inner class defined in a different method

thanks in Advance

Upvotes: 2

Views: 3083

Answers (3)

dacwe
dacwe

Reputation: 43504

It is very simular. Instead of deligates java has a Runnable interface and instead of C#s Name property java has setName method. Other than that the differences are minor:

Thread[] TCreate = new Thread[iThreadSize];
for (int i = 0; i < TCreate.length; i++) {

    TCreate[i] = new Thread(new Runnable() {
        public void run() {
            lst.Add(this.getResult(url));
        }
    });
    TCreate[i].setName("URL"+i);
    TCreate[i].start();
}

for (int j = 0; j < TCreate.length; j++)
    while (TCreate[j].isAlive())
         Thread.sleep(10);

As for your obj variable, I do not know what it is, you can try to declare it final. Note however that final references cannot be changed but in your case it might just work.

`final Object obj = ....;`

Upvotes: 4

Peter Lawrey
Peter Lawrey

Reputation: 533880

Using a ThreadPool you would do the following.

// only needed if you don't want to reuse the pool.
ExecutorService executor = Executors.newCachedThreadPool();

// ----
List<Future<Result>> results = new ArrayList<Future<Result>>();
for (int i = 0; i < iThreadSize; i++) 
    results.add(executor.submit(new Callable<Result>() {
        public Result call() {
            return getResult(url));
        }
    });

for(Future<Result> future: results)
    lst.add(future.get());
// ----

// only needed if you don't want to reuse the pool.
executor.shutdown();

Upvotes: 2

Thomas
Thomas

Reputation: 88757

Note that you can also use this to get all active threads of a group:

Thread[] activeThreads = new Thread[Thread.activeCount];
Thread.enumerate( activeThreads  );

Upvotes: 1

Related Questions