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

Thread

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Devaka Coorays Exam Simulator.




I dont undersatnd why its printing 000000??

When I use a single Thread instance
ThreadClass tc1=new ThreadClass(2);
the value is as expected 2.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

x is static and here are the steps taken

1.three constructors will run first when create them

2. since x is static the last constructor run by new ThreadClass(0),so x value will be 0 forever.

3.ex1.start();// here s=s+x will be evaluated to s="0"

4.doDelay(100) will delay the process in order to delay the main thread which prints s value.

5.same thing happen for all threads

6.when you tried ThreadClass ex1=new ThreadClass(2), static variable x will get 2 since no more constructors.


hope this helps

Thanks
Preetha
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why is it printing 6 times?
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you try this program?
you will get output as
0
00
000 not as 000000
[ December 28, 2008: Message edited by: Preetha Arun ]
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this ..you will get 000000 as output

public class ThreadClass extends Thread{

public ThreadClass(int x){
this.x=x;
}

static String s="";
static volatile int x=0;

public static void doVIP(){
s=s+x;
try{
Thread.sleep(10);
System.out.print(s);
}catch(Exception e){
//s=s+x;
}
}

public void run(){
synchronized(s){
doVIP();
}
}

public static void doDelay(long g){
try{Thread.sleep(g);}catch(Exception e){}
}

public static void main(String args[]){

ThreadClass tc1=new ThreadClass(2);
ThreadClass tc2=new ThreadClass(1);
ThreadClass tc3=new ThreadClass(0);

tc1.start(); doDelay(100);
tc2.start(); doDelay(100);
tc3.start(); doDelay(100);

}
}
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arun is right here.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic