dcsimg
Is a 'variable' in the run() method of a Threaded class is sharable?
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sabu_vs
Posted On:   Friday, November 12, 2004 01:41 AM

If the run() method of the threaded class contains variable, will all the the threads shares the same variable or use different copies of the variable?

thanks

sabu

Re: Is a 'variable' in the run() method of a Threaded class is sharable?

Posted By:   Marian_Olteanu  
Posted On:   Saturday, November 27, 2004 12:20 AM

Each thread will have its own set of local variables, no matter what method or what class you are reffering to. Therefore, for the run() method, same thing happens.

Re: Is a 'variable' in the run() method of a Threaded class is sharable?

Posted By:   Pavel_Tavoda  
Posted On:   Thursday, November 25, 2004 08:32 AM

Variable only point to value. However if you would like to have shared variable you can use "static" variable:

private static String aa="Leco"

And all instances will share this variable.
If you want more fain grain control other mechanism have to be applied.

Re: Is a 'variable' in the run() method of a Threaded class is sharable?

Posted By:   Anonymous  
Posted On:   Wednesday, November 24, 2004 11:24 PM

According to this program,i found that the variable which is defined inside the method or declared global also is not shared.

public class test extends Thread
{
int i;
int var;
public test(int i)
{
super("testthread");
this.i=i;
this.start();
}
public void run()
{
var=10;
System.out.println("variable var in thread"+i+":"+(var-2));
try{
this.sleep(10000);
this.join();
}catch(Exception e){}
}
public static void main(String args[])
{
test t1=new test(1);
test t2=new test(2);
test t3=new test(3);
test t4=new test(4);
}
}

Re: Is a 'variable' in the run() method of a Threaded class is sharable?

Posted By:   Zac_Jacobson  
Posted On:   Friday, November 12, 2004 03:09 PM

Try it, and find out.
About | Sitemap | Contact