• 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

Threads:

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1: public class Threadclass {


public static void main(String args[])throws InterruptedException{
printall(args);

}

public static void printall(String[] lines)throws InterruptedException{

for(int i=0;i<lines.length;i++){

System.out.println(lines[i]);
Thread.currentThread().sleep(1);
}
}

}

what changes can make my print statement executed .

2 ublic class Threadclass {


public static void main(String args[]){
Threadclass t=new Threadclass();
System.out.println("1");

synchronized(args){

System.out.println("2");
try{
args.wait();
args.notify();
}catch(InterruptedException e){}
}
System.out.println("3");
}



}

how can I have 1 2 3 as output

[ January 16, 2007: Message edited by: Kasak Tahilramani ]
[ January 16, 2007: Message edited by: Kasak Tahilramani ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You do know that the call to args.wait() will cause the calling thread to wait, right? It won't come back until some other thread sends a notification on args -- or the thread gets interrupted.

Henry
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ad 1) Ever tried to run your program with some command line arguments?

ad 2) As Henry Wong said: Execution of args.wait() will cause the (main-)thread to wait until he's notified (but no other thread will do here).
Try args.wait(2000) in example to see the "3", too.

(Edit: Name corrected [sorry, Henry])
[ January 16, 2007: Message edited by: Anton Uwe ]
 
Kasak Tahilramani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you provide the solution for 1st one.Any tutorials for thread other than KB?.
 
reply
    Bookmark Topic Watch Topic
  • New Topic