Java Multithreading
Java Multithreading
Java Lecture-48
Topic: Chapter-8 Multithreading
Programs that can run more than one thread at once are said to be multi-
threaded program.
Main
Thread
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Creating Thread
Thread can be created by following two ways
1. By extending Thread class.
2. By implementing Runnable Interface
c) Create a Thread object and call the start() method to initiate thread
execution.
System.out.println("Exiting A Thread");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=30;j++)
System.out.println("B Thread : j = "+j);
System.out.println("Exiting B Thread");
}
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
System.out.println("Exiting C Thread");
}
}
class ThreadTest
{
public static void main(String [] args)
{
A a = new A();
B b = new B();
C c = new C();
a.start();
b.start();
c.start();
}
}
Output:
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-49
Topic: Thread Life Cycle OR Thread States
Thread states/Thread life cycle is very basic question, before going deep
into concepts we must understand Thread life cycle.
start()
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
1. New State
When you create object of Thread with the new operator that means it
is in the born state, it does not start executing code inside the thread. It
remains in this state until we call the start () method.
2. Runnable State
The thread is in runnable state after invocation of start() method but the
thread scheduler has not selected it to be the running thread.
3. Running State
The thread is in running state, if the thread scheduler has selected it for
running.
5. Dead State:
A thread is considered dead when its run() method completes.
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-50
Topic: Thread Constructors and Its Methods
Constructors
Thread()
Allocates a new Thread object.
Thread(Runnable target)
Allocates a new Thread object.
Thread(Runnable target, String name)
Allocates a new Thread object with specified
name.
Thread(String name)
Allocates a new Thread object with specified
name.
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Java Lecture-51
Topic: Thread Synchronization
Key to synchronization is the concept of the monitor. Only one thread can own
a monitor at a given time. When a thread acquires a lock, it is said to have
entered the monitor. All other threads attempting to enter the locked monitor
will be suspended until the first thread exits the monitor. These other threads
are said to be waiting for the monitor.
class Table {
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Table t;
MyThread1(Table t) {
this.t=t;
}
Output:
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
There are two ways that you can synchronize your code.
1) Using synchronize Method:-
If you declare any method as synchronized, it is known as synchronized
method.
Synchronized method is used to lock an object for any shared resource.
When a thread invokes a synchronized method, it automatically acquires the
lock for that object and releases it when the thread completes its task.
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
class SysnTest {
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
Output:
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260
Imagine that you want to synchronize access to objects of a class that was
not designed for multithreaded access. That is the class does not use
synchronized methods. Further, this class was not created by you, but by a
third party and you do not have access to the source code. Thus it is not
possible for you to add synchronized to the appropriate methods within the
class. In this situation, you simply put calls to the methods defined by this
class inside a synchronized block.
Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260