0% found this document useful (0 votes)
38 views

Mohammad Abubaqr: Submitted by

1) The key difference between start() and run() is that start() creates a new thread to execute the run() method, while run() executes on the same thread. 2) Implementing Runnable is generally better than extending Thread as it allows a class to extend another class. 3) The main difference between wait() and sleep() is that wait() releases the lock and must be called from a synchronized context, while sleep() only pauses the thread without releasing the lock.

Uploaded by

Mohammad Abubaqr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Mohammad Abubaqr: Submitted by

1) The key difference between start() and run() is that start() creates a new thread to execute the run() method, while run() executes on the same thread. 2) Implementing Runnable is generally better than extending Thread as it allows a class to extend another class. 3) The main difference between wait() and sleep() is that wait() releases the lock and must be called from a synchronized context, while sleep() only pauses the thread without releasing the lock.

Uploaded by

Mohammad Abubaqr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Submitted By:

Mohammad Abubaqr
Submitted To:
Shair Afghan
Roll No:
CS151012
Subject:
Advance Object Oriented Programming
Class:
BSCS 5th Semester
Topic:
Java 5 Short Questions.
1) What is the difference between start and run method in Java Thread?
This thread question is also ask as if start() method eventually call run() method then why do
you need to call start() method, why not call run() method directly. well, reason is that because
start method creates a new thread and call the code written inside the run method on a new thread
while calling run method executes that code on the same thread.

2) Which one is better to implement thread in Java ? extending Thread class or implementing
Runnable?
Essentially these are two way to implement Thread in Java, by extending java.lang.Thread class or
by implementing java.lang.Runnable interface.

By extending the class you are using your chance to extend one any only one class as Java does
not support multiple inheritances, by implementing a Runnable interface you can still extend another
class. So extending Runnable or even Callable is a better choice.

3) What is the difference between wait and sleep in Java?


The key point to mention while answering this question is to mention that wait will release the lock
and must be called from the synchronized context, while sleep will only pause the thread for some
time and keep the lock.

By the way, both methods throw IntrupptedException and can be interrupted, which can lead
to some follow-up questions like, can we awake a sleeping or waiting for a thread in Java.
4) How do you share data between two threads in Java?
You can share data between thread by using shared object or shared data structures like Queue.
Depending upon, what you are using, you need to provide the thread-safety guarantee, and one way
of providing thread-safety is using synchronized keyword.

If you use concurrent collection classes from Java 5 e.g. BlockingQueue, you can easily share data
without being bothered about thread safety and inter-thread communication.
5) What is a thread leak? What does it mean in Java?
Thread leak is when an application does not release references to a thread object properly. Due to
this, some Threads do not get garbage collected and the number of unused threads grows with time.
Thread leak can often cause serious issues on a Java application since over a period of time too
many threads will be created but not released and may cause applications to respond slow or hang.

You might also like