• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Thread....

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i have a doubt in Threads.....
Do v really need
classinstance.start() to start a run() of thread when the class extends Thread.Or u can call start() directly without the instance of class.

One more if v have static instance variable then if v make a chance to the variable inside a method does it reflect on the original variable value since static has only one copy......

Pls explain this problem which deal with wat i have asked...



Output
Compilation and probably output of "Techno" but possible output of "Techno 0 1 2 3"

Edited by Corey McGlone: Added CODE Tags
[ January 26, 2005: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramya,

This is probably just a personal peeve of mine, but I try to discourage people from using "IM speak" when posting here on the Ranch. It's obvious that English is not your primary language and, honestly, I'm having a hard time understanding your question(s). By adding "IM speak" to your post, you're simply making it even harder for me to give you any useful answer. Please, in order to "help us help you," try to format your questions properly.

Do v really need
classinstance.start() to start a run() of thread when the class extends Thread.Or u can call start() directly without the instance of class.



You can simply call start() if you are within a class that extends Thread. Often, however, you spawn threads from some other class, not the class that implements the Thread.

One more if v have static instance variable then if v make a chance to the variable inside a method does it reflect on the original variable value since static has only one copy......



When it comes to static variables, there is always just one variable per class. That variable is shared over all instances of that class. So, if one instance of a class changes a static variable, it is changed for all instances of that class (and even for anyone outside the class that uses that variable).

For your code sample, I believe possible outputs include:

Techno
Techno 0
Techno 0 1
Techno 0 1 2
Techno 0 1 2 3

What the output is depends entirely upon when the threads execute and when they lose control of the processor/terminate.
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Corey

sorry for not framming the question correctly
my doubt is not yet cleared.

static String sName = "Techno",

Now this sName is modified to sName =sName+"Park" in the method

public void name(String sName)so now sName = Techno Park,
but y in the output v are getting only Techno
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramya JP:

Now this sName is modified to sName =sName+"Park" in the method



That is not true. In that method, the parameter is named "sName", which happens to be the same as your static member variable. By doing that, you have "shadowed" the static member variable. Any changes that you do to "sName" will happen to the local parameter, not the static member.

Try changing that method to be something like this:

Tux.sName = sName + " park";

And I think you'll see what you're expecting.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramya,
I agree with Corey , it would be nice if you could spell out the whole words.


but y in the output v are getting only Techno



but why in the output we are getting only Techno.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CODE BLOCK 1:
==========================================
public class Tux extends Thread{

static String sName = "Techno";

public static void main(String argv[]){
Tux t = new Tux();
t.name(sName);
System.out.println(sName);
}

public void name(String sName){
sName = sName + " park";
start();
}

public void run(){

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

==========================================

CODE BLOCK 2:
==========================================
public class Tux extends Thread{

static String sName = "Techno";

public static void main(String argv[]){
Tux t = new Tux();

// by now JVM will have 2 Threads running(as far as execution of Tux is
// concerned ), for 'main' & 't' respectively,
// as we know Thread by default have '5' as its Priority.
// now both thread has same priority (as 5), 't' is child of main and is
// dependent on 'main'. So as soon as 'main' exist ( with 't' still not
// having any higher prority, higher than 5) will kill 't' as the
// consequence.

// any priority higher than 5 for thread 't' will increase its probability
// to execute before 'main'

t.setPriority(6) ; // <inserted statement>
t.name(sName);
System.out.println(sName);
}

public void name(String sName){
sName = sName + " park";
start();
}

public void run(){

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

hope comments makes the code self explanatory.

thanks,
[ January 27, 2005: Message edited by: navneet shrivastava ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic