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
It is very simular. Instead of deligates java has a
Runnableinterface and instead of C#sNameproperty java hassetNamemethod. Other than that the differences are minor:As for your
objvariable, I do not know what it is, you can try to declare itfinal. Note however thatfinalreferences cannot be changed but in your case it might just work.