Sie sind auf Seite 1von 21

Discovering Knowledge

CSC-210
Object-Oriented Programming

Lecturer
Sameena Javaid

https://sites.google.com/site/sameenajavaidcs

© Bahria University Department of Computer Science | Bahria University 1


Discovering Knowledge

THREADS

© Bahria University Department of Computer Science | Bahria University 2


Discovering Knowledge

Thread
• A thread is a part of execution in a program.

• The Java Virtual Machine allows an application


to have multiple threads of execution running
concurrently.

© Bahria University Department of Computer Science | Bahria University 3


Discovering Knowledge

Computer
Playing Music
Other Video Player
Tasks

My Super Browse
Computer Internet
NotePad

Downloading Playing
Files Game

© Bahria University Department of Computer Science | Bahria University 4


Discovering Knowledge

Music Player
Playing Managing
Songs Playlists

Music
Player
Downloading Accessing
Updates Internet

© Bahria University Department of Computer Science | Bahria University 5


Discovering Knowledge

Why do we use threads?


• Thread is a light weight
process as it shares the
same memory and the
resources of the
process in which it is
created.

© Bahria University Department of Computer Science | Bahria University 6


Discovering Knowledge

Thread – Life Cycle


New

Program starts
thread
Unlock signal Thread
completes task

Waiting Runnable Terminated

Await lock

Await sleep Interval express

Timed Waiting

© Bahria University Department of Computer Science | Bahria University 7


Discovering Knowledge

Thread – Methods

New Blocked

t.sleep(),
t.start() t.wait()

Run method
ends
Ready t.run() Running Terminated

© Bahria University Department of Computer Science | Bahria University 8


Discovering Knowledge

Thread Priorities
• When multiple threads are running the order of
execution of threads depend on priority given to
thread.
• Priority values ranges from 1 to 10.(default-5).
• setPriority(int priority) – method to set priority.

© Bahria University Department of Computer Science | Bahria University 9


Discovering Knowledge

Creation of threads in multiple ways

Threads can be created in two ways.


• Implementing Runnable interface.
• Extending Thread class

© Bahria University Department of Computer Science | Bahria University 10


Discovering Knowledge

Implementing Runnable Interface


public class ThreadClass implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
}
public class Main{
public static void main(String[] args) {
ThreadClass obj = new ThreadClass(); //object of class ThreadClass
Thread thread_runnable = new Thread(obj);
thread_runnable.start();
}
}

© Bahria University Department of Computer Science | Bahria University 11


Discovering Knowledge

Extending Thread Class


public class ThreadClass extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
}
public class Main{
public static void main(String args[]) {
ThreadClass obj = new ThreadClass(); //object of ThreadClass
Thread thread_extend = new Thread(obj);
thread_extend.start();
}
}

© Bahria University Department of Computer Science | Bahria University 12


Discovering Knowledge

Thread Class methods


• void start()
• int getPriority()
• void setPriority(int newpriority) • void run()

• void sleep(long millis) • void wait()


• void join(long millis) • void notify()
• void notifyAll()
• Thread currentThread()
• Thread getState()
• void stop()
• void setName(String name) • void yield()
• String getState()

© Bahria University Department of Computer Science | Bahria University 13


Discovering Knowledge

Multithreading
• When two or more threads are running in a
process simultaneously then it is called
Multithreading.

© Bahria University Department of Computer Science | Bahria University 14


Discovering Knowledge

Multithreading
public class One extends Thread{
Example
public void run()
{
System.out.println("This is first thread");
}
}

public class Second extends Thread{


public void run()
{
System.out.println("This is second thread");
}
}

© Bahria University Department of Computer Science | Bahria University 15


Discovering Knowledge

Multithreading Example
public class Main extends Thread{
public static void main(String[] args)
{
Thread thread1 = new Thread(new One());
Thread thread2 = new Thread(new Second());

thread1.start();
thread2.start();
}
}

© Bahria University Department of Computer Science | Bahria University 16


Discovering Knowledge

Synchronization
• The process of making only one thread access
the object when two or more threads are
accessing the same object is called
Synchronization.
• Keyword - ‘synchronized’.

© Bahria University Department of Computer Science | Bahria University 17


Discovering Knowledge

How to synchronize the object?


synchronized(object) Synchronized
{
//Statements block
}

synchronized returntype methodname()


Synchronized {
method //Statements
}

© Bahria University Department of Computer Science | Bahria University 18


Discovering Knowledge

Inter-Thread Communication
• The process of communication between multiple threads
is called Inter-thread communication.

• In this process of communication when one thread is


executing the other thread will wait for the thread to be
executed.

• The methods used for communication are:


wait()
notify()
notifyAll()

© Bahria University Department of Computer Science | Bahria University 19


Discovering Knowledge

Deadlock
Object
• Deadlock is a situation in 1
which a thread has got an waiting
object and waiting for an
object locked by another
thread which is waiting for
Thread2
the object locked by first Thread1
thread.

waiting
Object
2

© Bahria University Department of Computer Science | Bahria University 20


Discovering Knowledge

Deadlock Example
• Player1 has got a bat to play and waiting for a
ball.
• Player2 has got a ball to play and waiting for a
bat.
• Both are waiting for the resource which are
held by the other player. This leads to
deadlock.

© Bahria University Department of Computer Science | Bahria University 21

Das könnte Ihnen auch gefallen