• 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

Thread2

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A extends Thread {
public void run() {
synchronized (this) {
try {wait(5000);} catch (InterruptedException ie){}
}}
public static void main(String[] args) {
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
System.out.print(System.currentTimeMillis() - startTime + ",");
try {a1.join(6000);} catch (InterruptedException ie) {}
System.out.print(System.currentTimeMillis() - startTime);
}}

What are the possible results of attempting to compile and run the program?

a. The first number printed is greater than or equal to 0
b. The first number printed must always be greater than 5000
c. The second number printed must always be greater than 5000
d. The second number printed must always be greater than 6000
e. The synchronized block inside the run method is not necessary
f. Compile-time error
g. Run-time error


Ans:A,C

anyone plz explain me this thing?
 
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
As a learning exercise, it may be helpful if you went through the possibilites yourself. Many of the choices are obvious -- try to eliminate them. Then tell us which choice you believe is different from the answer, and why.

BTW, I am not convinced that choice C is correct.

[EDIT] Let me qualify. Choice C should be correct if that is the only code running in the JVM.

Henry
[ November 29, 2005: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason for C is

1. The thread executing the main method can wait for atmost 6000 ms for thread A to finish. Thread A will be in wait status for atmost 5000 ms as per the contract of wait(long ms) method. In this case, Thread A will wait for 5000 ms , since there is no other thread which can notify it. So Thread A will take atleast 5000 ms to complete. The thread which is executing main will wait for atmost 6000 ms. If thread A finishes before 6000 ms , then its fine. If it is not, main thread will wait for 6000 ms and then will finishes.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i ran the above code for 5 times and got the output as
1. 0,4988
2. 0,4989
3. 0,4988
4. 0,4988
5. 0,4988

Regards,
G. Vinodh Kumar.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure how did u get that arrive at that answer ?
A and C seems to be right to me .
the main thread waits atleast 5000 milli secs , until the first thread completes.
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramakrishna,

Vinodh is right.. The main thread not necessarily has to wait for 5000 ms all the times. The reason is, if wait(5000) is eexcuted before a1.join(6000), then the remaining milliseconds that a1 will take will be less than 5000, because it already started executing wait method.

So, it not "must".

Lakshmanan
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Plz Any one Explain this concept Clearly...

Thanks in advance.

Regards,
Hari Krishna.
 
reply
    Bookmark Topic Watch Topic
  • New Topic