java中的线程数组

时间:2011-06-27 09:08:06

标签: c# java multithreading

我需要知道java中的线程数组。这是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);

我需要知道在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;

            }               

        }

如果您需要任何详细信息,请与我们联系。

如果我转换此

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

显示错误:无法引用以不同方法定义的内部类中的非最终变量“obj”

先谢谢

3 个答案:

答案 0 :(得分:5)

非常类似。而不是分层,java有一个Runnable接口而不是C#s Name属性java有setName方法。除此之外,差异很小:

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

至于你的obj变量,我不知道它是什么,你可以尝试声明它final但请注意,final引用无法更改,但在您的情况下,它可能会起作用。

`final Object obj = ....;`

答案 1 :(得分:2)

使用ThreadPool,您将执行以下操作。

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

答案 2 :(得分:1)

请注意,您也可以使用它来获取组的所有活动线程:

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