• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static string

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Tux1 extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux1 t = new Tux1();
t.piggy(sName);
System.out.prinln(sName);
}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){
for(int i=0;i < 4; i++){
sName = sName + " " + i;
}
}
}

Output is
vandeleur

I am confused the above result. Could any one explain about the output?
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes..
Strings are immutable and Since the sName formal parameter in method piggy() is just a copy of the sName field in class Tux, the original sName still refers to the original string "vandeleur".

The parameter in this method piggy hides the class member.
[ April 08, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
put this line of code in the piggy method right after you assign the new string to sName

or change the following line of code
to

I'd love to get a more detailed explanation from the gurus.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---------------------------------------------------------------------
Output is
vandeleur

I am confused the above result. Could any one explain about the output?

----------------------------------------------------------------------

While a child thread is started we cannot assure that always child thread execute immediately before main continues its code....

So makes little change.....

public class Tux extends Thread
{
static String sName = "vandeleur";
public static void main(String argv[]) throws InterruptedException
{
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);
}
public void piggy(String sName) throws InterruptedException
{
int i=10;
sName = sName + " wiggy";
start();
join(); //Added line
}
public void run()
{
for(int i=0;i < 4; i++)
{
sName = sName + " " + i;
}
}
}


and the desire output.............

vandeleur 0 1 2 3

i hope it clers U all
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your program main thread (main method) may complete its execution before the child thread start or complete its execution..

Thus the modification you are doing in child thread may not affect the println statement that is given in the main thread.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Main program ensures the child thread to be in atleast once in a running stage before main thread continues its next instruction

that one slot enough to complete its all works in our program
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry friends

in my last reply i told that child thread have given a one slot before main
gots another slot

but it is not true

When we use join, child thread after terminates only main thread works

Sorry for confusion.....
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi vidyasagar i didn't get the join method
can u explain me?

to my knowledge i understand that the child thread will be started once it invoked the start() method.The line that follows after the start() method will be carried on by main thread.(i.e)The join method will be invoked by main thread.by we didn't give any information to tell the main thread to join the child thread, thats were i am confused can just explain me about join method
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---------------------------

to my knowledge i understand that the child thread will be started once it invoked the start() method.The line that follows after the start() method will be carried on by main thread.(i.e)The join method will be invoked by main thread.by we didn't give any information to tell the main thread to join the child thread, thats were i am confused can just explain me about join method

---------------------------

Hi Paramesh

Whenever we calling a start method of Thread it makes a newly created Thread to runnable state (not Running state).To Make newly created Thread to be run we call a join() method to make it run, by stopping currently running Thread. Once the Thread started running it will finish all instruction and handover over to the calling thread.

In our program mention Above..... Since we didnot specify any object it takes this Object(Not this Thread be ..Careful) and started to run.

I hope now UR Clear.....
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i doubt in the connection between the join and running the thread.
any how
can u explain with some example if u don't mind
 
Baiju Scariah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A extends Thread{
// Some code here
main()
{
A a1 = newA();
A a2 = newA();
A a3 = newA();

a1.start();
a1.join();

a2.start();
a2.join();

a3.start();
a3.join();

System.out.println("main");
}
}

In the above example a1, a2 and a3 will be executed sequentially.

If join is called on one object then only after that object completes its execution main will continue its execution

- you know otherwise java will not guarantee about the order of execution
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic