• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

the static variable keep the orginal values,why?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Tux extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);

}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){

for(int i=0;i < 4; i++){
sName for(int i=0;i < 4; i++){
sName = sName + " " + i;

}
}
}
the static variable had been change in method piggy.but when I compile and execute the code .
result is:
vandeleur
not as I expect : vandeleur wiggy 0 1 2 3
Would you give something about it?
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not the static variable you change. Since the parameter to piggy has the same name as the static, it will use the parameter instead, as a new variable, due to the fact that the method is the closest scope.
change the code as follows, and you'll understand what happends (I hope):

ok?
/Mike
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this code, but the output was the same. Why?

Originally posted by Mikael Jonasson:
[B]It is not the static variable you change. Since the parameter to piggy has the same name as the static, it will use the parameter instead, as a new variable, due to the fact that the method is the closest scope.
change the code as follows, and you'll understand what happends (I hope):

ok?
/Mike[/B]


 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and I coded this and it gives .
vandaleur 0 1 2 3
at the end.
(I still haven't a clear language when I speak about Threads. Trying my best.)

The problem (I think) is that you cannot predict what Thread gets at what time CPU-time.
With start() you spawn a new Thread in runnable state. But you cannot predict, when CPU is given from new thread to the old Thread.
In the programs above the problem seems that main Thread terminates B E F O R E start is called.
So I took a static variable and a loop which tests if the Thread has run.
<code>
public class Tux extends Thread{
static String sName = "vandeleur";
static boolean threadIsRun;
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
while(!threadIsRun) {
System.out.println("thread has not yet been started");
}
System.out.println(sName);
}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){
System.out.println("in run method");
for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
threadIsRun = true;
}
}
</code>
ANOTHER POINT:
As a second step comment out the loop. You will see that the Thread actually runs. But after the MAIN THREAD HAS FINISHED.

ciao
Axel

[This message has been edited by Axel Janssen (edited July 05, 2001).]
[This message has been edited by Axel Janssen (edited July 05, 2001).]
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another thread concerning this same question:
http://www.javaranch.com/ubb/Forum24/HTML/010429.html
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, it seems you missed my point. When you changed the parametername and the asigmnet row to the same new variable, you are changing a copy of the original. If you change the parametername (to tName for instance) and keep the asignmentrow (sName=...) you will see that you get another output.
/Mike
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mikkael
o.k. my code results in something without wiggy, but it prints the 0 1 2 3.
You are definitedly right about the variable shadowing.
But I pointed out another problem concerning thread scheduling. As Aakanksha said. Your code does not give back the expected vandeleur wiggy 0 1 2 3 but only vandaleur. This is because the line the System.out.println at the end of the main method is reached by the parent Thread BEFORE the run method of the child Thread invoked by the start() in method piggy is reached.
This is the efect on this computer (windows).
think so, correct me if I am wrong.
Axel

[This message has been edited by Axel Janssen (edited July 05, 2001).]
 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are two thread in the progrm:the main thread,and the tux thread ,before we cant predict which thread will accomlish so the answer is no single!
As the original code i post it may be:
1 vandeleur
2 vandeleur 0 1 2 3
 
Aakanksha Arora
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it.
Thanks,
Aakanksha

Originally posted by Axel Janssen:
...and I coded this and it gives .
vandaleur 0 1 2 3
at the end.
(I still haven't a clear language when I speak about Threads. Trying my best.)

The problem (I think) is that you cannot predict what Thread gets at what time CPU-time.
With start() you spawn a new Thread in runnable state. But you cannot predict, when CPU is given from new thread to the old Thread.
In the programs above the problem seems that main Thread terminates B E F O R E start is called.
So I took a static variable and a loop which tests if the Thread has run.
<code>
public class Tux extends Thread{
static String sName = "vandeleur";
static boolean threadIsRun;
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
while(!threadIsRun) {
System.out.println("thread has not yet been started");
}
System.out.println(sName);
}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){
System.out.println("in run method");
for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
threadIsRun = true;
}
}
</code>
ANOTHER POINT:
As a second step comment out the loop. You will see that the Thread actually runs. But after the MAIN THREAD HAS FINISHED.

ciao
Axel

[This message has been edited by Axel Janssen (edited July 05, 2001).]
[This message has been edited by Axel Janssen (edited July 05, 2001).]


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the following code. It should solve your problem.

public class Tux1 extends Thread
{
static String sName = "vandeleur";
public static void main(String argv[])
{
Tux1 t = new Tux1();
t.piggy(sName);
System.out.println(sName);
}

public void piggy(String sName)
{
sName = sName + " wiggy";
setPriority(10);
start();
}

public void run()
{
for(int i=0;i < 4; i++)
{
for(int j=0;j < 4; j++)
{
sName = sName + " " + i +j;
}
}
}
}
In this modified version of your program we are setting the thread priority of the child thread to be greater that that of the main thread. This ensures that the child thread executes before the main one. So when the main thread is executed, it prints out the value of sName which has already been modified by the child thread.
Hope this helped and correct me if I'm wrong.
 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it ,thank very much!
good luck !
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic