Escolar Documentos
Profissional Documentos
Cultura Documentos
arjun.rokaya@gmail.com 2
we use multithreading than multiprocessing
because threads share a common memory
area.
arjun.rokaya@gmail.com 3
A process runs independently and isolated of
other processes. It cannot directly access shared
data in other processes. The resources of the
process, e.g. memory and CPU time, are allocated
to it via the operating system.
arjun.rokaya@gmail.com 4
It doesn't block the user because threads are
independent and you can perform multiple
operations at same time.
arjun.rokaya@gmail.com 5
A thread is a lightweight sub process, a
smallest unit of processing. It is a separate
path of execution.
arjun.rokaya@gmail.com 6
arjun.rokaya@gmail.com 7
A thread can be in one of the five states.
◦ New
◦ Runnable
◦ Running
◦ Non-Runnable (Blocked)
◦ Terminated
arjun.rokaya@gmail.com 8
arjun.rokaya@gmail.com 9
There are two ways to create a thread:
arjun.rokaya@gmail.com 10
Thread class provide constructors and
methods to create and perform operations on
a thread.
arjun.rokaya@gmail.com 11
Thread()
Thread(String name)
Thread(Runnable r)
Thread(Runnable r,String name)
arjun.rokaya@gmail.com 12
1. public void run(): is used to perform action for a thread.
2. public void start(): starts the execution of the thread.JVM calls the
run() method on the thread.
3. public void sleep(long miliseconds): Causes the currently executing
thread to sleep (temporarily cease execution) for the specified
number of milliseconds.
4. public void join(): waits for a thread to die.
5. public void join(long miliseconds): waits for a thread to die for the
specified miliseconds.
6. public int getPriority(): returns the priority of the thread.
7. public int setPriority(int priority): changes the priority of the thread.
8. public String getName(): returns the name of the thread.
9. public void setName(String name): changes the name of the thread.
arjun.rokaya@gmail.com 13
The Runnable interface should be
implemented by any class whose instances are
intended to be executed by a thread.
arjun.rokaya@gmail.com 14
start() method of Thread class is used to start a
newly created thread.
arjun.rokaya@gmail.com 15
arjun.rokaya@gmail.com 16
arjun.rokaya@gmail.com 17
arjun.rokaya@gmail.com 18
Thread scheduler in java is the part of the JVM
that decides which thread should run.
arjun.rokaya@gmail.com 19
Default priority of a thread is 5
(NORM_PRIORITY).
thread.setPriority( Thread.MAX_PRIORITY );
Thread.currentThread( ).getPriority( )
arjun.rokaya@gmail.com 20
arjun.rokaya@gmail.com 21
The sleep() method of Thread class is used to
sleep a thread for the specified amount of
time.
arjun.rokaya@gmail.com 22
class TestSleepMethod1 extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedException e){System.o
ut.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestSleepMethod1 t1=new TestSleepMethod1();
TestSleepMethod1 t2=new TestSleepMethod1();
t1.start();
t2.start();
}
}
arjun.rokaya@gmail.com 23
The join() method waits for a thread to die.
arjun.rokaya@gmail.com 24
arjun.rokaya@gmail.com 25
arjun.rokaya@gmail.com 26
The shutdown hook can be used to perform
cleanup resource or save the state when JVM
shuts down normally or abruptly.
arjun.rokaya@gmail.com 27
The JVM shuts down when:
arjun.rokaya@gmail.com 28
arjun.rokaya@gmail.com 29
Synchronization in java is the capability to
control the access of multiple threads to any
shared resource.
arjun.rokaya@gmail.com 30
When you have two threads that are reading
and writing to the same 'resource', say a
variable named foo, you need to ensure that
these threads access the variable in an atomic
way. Without the synchronized keyword, your
thread 1 may not see the change thread 2
made to foo, or worse, it may only be half
changed. This would not be what you
logically expect.
arjun.rokaya@gmail.com 31
arjun.rokaya@gmail.com 32
arjun.rokaya@gmail.com 33