Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Doubt in Threading

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends.I have a doubt with the following question.

code:
------------------------------------------------------------------
class Ant implements Runnable {
int i;
public void run () {
try {
Thread.sleep(1000);
i= 10;
} catch(InterruptedException e) {}
}
}

public class Testing {
public static void main (String args[]) {
try {
Ant a = new Ant();
Thread t = new Thread (a);
t.start();
//line 17
int j= a.i;
} catch (Exception e) {}
}
}
------------------------------------------------------------------------
Which statement at line 17 will ensure that j=10 at line 19?
A. a.wait();
B. t.wait();
C. t.join();
D. t.yield();
E. t.notify();
F. a.notify();
G. t.interrupt();
----------------------------------------------------------------------
The answer is C(Not sure if its correct).
When I run the code with C ,no output is produced.Which could be the correct answer and why?.Somebody help please!!
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry the code is this.
------------------------------------------------------------
class Ant implements Runnable {
int i;
public void run () {
try {
Thread.sleep(1000);
i= 10;
} catch(InterruptedException e) {}
}
}

public class Testing {
public static void main (String args[]) {
try {
Ant a = new Ant();
Thread t = new Thread (a);
t.start();
//line 17
int j= a.i;
//line 19
} catch (Exception e) {}
}
}
-------------------------------------------------------------------
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is C t.join()
An output statement System.out.println(j); at line 19 will display the result.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is t.join which is option C.

This command let the main Thread wait until completion of the t Thread.

Join means let the current thread wait to the end of the thread which has to be joined so that the current thread will run again when the joined thread has finished.
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic