Help coderanch get a
new server
by contributing to the fundraiser
  • 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: 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() {
try {sleep(10000);} 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);
}}

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

a. Prints a number greater than or equal to 0
b. The number printed must always be greater than 10000
c. This program will run for at least ten seconds
d. Compile-time error
e. Run-time error



can nayone explain me the answeres of above code(Ans are A,C)?
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can nayone explain me the answeres of above code(Ans are A,C)?


Yes, but it would save a great deal of time if you let us know specifically what you find confusing about the answer; i.e., what it is about the answer you don't understand.

Resources for learning about threads:
Java's Thread Tutorial
JavaWorld: Introduction to Java threads
IBM: Introduction to Java threads
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.currentTimeMillis()-->Returns the current time in milliseconds

Thread.sleep(long millis)-->
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds

-->'main' thread is started.
-->Current time in milliseconds is stored in the variable 'startTime'
-->thread 'a1' is started. ( here two threads 'main','a1' are running )
-->S.o.p statement is executed.
value of
System.currentTimeMillis() - startTime
will be always 0 or greaterthan zero.
-->'a1' thread sleeps for 10seconds
(1sec=1000 milliseconds, 10,000sec = 10sec)
-->No more statements to be executed..hence both the threads will be terminated.

To understand

System.currentTimeMillis() - startTime
will be always 0 or greaterthan zero.
try the following code

class A extends Thread {

public void run()
{
try {sleep(10000);} catch (InterruptedException ie){}
}

public static void main(String[] args)
{

A a1 = new A();
long startTime = System.currentTimeMillis(); //start time

a1.start();
// replaced System.out.println(System.currentTimeMillis() - startTime); with the following 2 statements

long T2 = System.currentTimeMillis();
System.out.println( startTime+" "+T2+" " + (T2-startTime));

}
}

(a) output is greater than or equal to 0 ( correct)
(b) here Time difference will not be greater than 10000, because both the threads are running (Parallel execution)
if 'main' thread waits until 'a1' finishes, time differe can be >=10000.
Hence (b) is wrong
(c) Here thread 'a1' is sleeping for 10 sec. ( correct)
(d) No syntax errors so, (d - wrong)
(e) No Runtime error ( InterruptedException is checked exception, here try-catch block is used) ( so (e) - wrong )

Answers: A, C

Hope this helps you

Regards
Naresh
 
achayya matta
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Naresh,wonderful explanation!
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic