Multithreading
Thread, thread refers to the smallest part of the
program. If two or more parts of the program run concurrently then this is
called multithreading. Each thread defines a separate path of execution. One
thread can pause without stopping other parts of program. In java “Thread” is a
predefined class which is used to create single or multiple thread and it is store
in Java.Lang package.
OS supports two types of tasking-
1) Process-based
multitasking
2) Thread-based
multitasking
When more than one program run concurrently then this concept is called Process-based multitasking. For example-
web browser and music running concurrently.
When one program perform more than one task then this concept is
called thread-based multitasking.
For example- formatting and printing at the same time on a text editor.
Some Methods of thread class.
1) currentThread()
: used to get the reference of current running thread. It is a static method.
2) setName( String name-of-thread) :
used to set the name of thread. It is a final method.
3) getName():
used to get the name of thread. It is a final method.
4) setPriority(int level) :
used to set the priority of thread. It is a final method.
5) getPriority() :
used to get the priority of thread. It is a final method.
6) join() : wait
for a thread to terminate or wait until a specified thread join in.
7) run() : starting
entry point of the thread.
8) start() :
used to implicitly invoke the run method (start a thread by calling its run
method).
9) sleep(int miliseconds) : used
to stop the execution of current thread specified by the parameter supplied to
it. It is a static method.
Main thread : When a java program starts up, one thread
starts running immediately called main thread. It executes when program begin
execution.
It is
the thread from which other child threads will be spawned. Often it must be the
last thread to finish execution because it performs various shutdown actions. Main thread is created automatically when
program is started.
Thread
priority decides,
when to switch from one thread to another thread.
1) A
thread can willingly relinquish control: this can be done by sleeping, blocking
or pending i/o.
2) A
thread can be pre-empted by a higher priority thread.
In java every thread belongs to a group and by default
they all belong to main group. Group of thread is a data structure that handles
collection of threads. Java always assign priority of each thread and priority
is an integer no. between 1to10. This priority is used by CPU to decide when to
switch from one running thread to another. This concept is context switch. This
is pre-emption technique.
States
of thread – there
are 6 states
1) Running
- as soon as CPU start execution of its body.
2) Ready
to run – as soon as thread gets CPU.
3) Suspend
– when execution is suspended by sleep method.
4) Block
State – when resources requires by thread are not available.
5) Terminate
– execution completed.
6) Resume
– after suspend, thread can resume.
Once a thread gets terminate then it can’t be resume. If
OS is pre-emptive type then higher priority threads gets more CPU time than
lower priority thread. By default priority of thread is always 5. Priority is
set using setPriority() method. Priority is also used to determine how one
thread should be treated with respect to other thread.
No comments:
Post a Comment