• 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

Thread

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain the flow of this program?

class A1 extends Thread {
String[] sa;
public A1(String[] sa) {this.sa = sa;}
public void run() {
synchronized (sa) {
while (!sa[0].equals("Done")) {
try {sa.wait();} catch (InterruptedException ie) {}
}}
System.out.print(sa[1] + sa[2] + sa[3]);
}}
class B11 {
private static String[] sa = new String[]{"Not Done","X","Y","Z"};
public static void main (String[] args) {
Thread t1 = new A1(sa); t1.start();
synchronized (sa) {
sa[0] = "Done";
sa[1] = "A"; sa[2] = "B"; sa[3] = "C";
sa.notify();
}}}

Output: ABC
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: Consider the line t1.start(); in the main method of B11. What does this do? Will the instance of A1 run? If so, what will it do? If not, what will happen?

Reformatted with code tags...

[ June 20, 2005: Message edited by: marc weber ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic