What is multithreading? Explain the two ways of creating threads
in Java programs. Also explain difference
between notify() and notify All( ) methods.
Ans
Java provides
built-in support for multithreaded programming. A multithreaded
program contains two or more parts that can run concurrently. Each part of such
a program is called a thread, and each thread defines a separate path of
execution.
A multithreading is a
specialized form of multitasking. Multithreading requires less overhead than
multitasking processing.
I need to define
another term related to threads: process: A process consists
of the memory space allocated by the operating system that can contain one or
more threads. A thread cannot exist on its own; it must be a part of a process.
A process remains running until all of the non-daemon threads are done
executing.
Multithreading enables
you to write very efficient programs that make maximum use of the CPU, because
idle time can be kept to a minimum
Creating a Thread:
Java defines two ways
in which this can be accomplished:
- You can implement the Runnable interface.
- You can extend the Thread class, itself
notify():
this method should be called only when the current thread has already acquired
the lock on the object. If the wait set of the object is non-empty then a
thread from the set is arbitrarily chosen, removed and is re-enabled for thread
scheduling. Of course, the thread will not be able to proceed unless the
current thread releases the object’s lock.
notifyAll():
it’s same as the notify() method. The only difference is that in this case all
the threads from the non-empty wait set of the object are removed and are
re-enabled for thread scheduling in stead of only one thread from the wait set
being picked arbitrarily, removed, and re-enabled for thread scheduling as is
the case in notify() method.
'notify()'
method wakes up a single thread waiting on the object
and passes the control
of the monitor to it. So far so good and for
'notifyAll()'
it says that its will wake up all the threads waiting on the object and will
select a thread to pass control to it. Well as per
No comments:
Post a Comment