Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Core Java
Multithreading
Mr Jayant Dalvi
Multithreading : Summary
• A thread is a lightweight sub-process, the smallest
unit of processing. Multiprocessing and
multithreading, both are used to achieve
multitasking.
• Multithreading in Java is a process of executing
multiple threads simultaneously.
• we use multithreading than multiprocessing
because threads use a shared memory area. They
don't allocate separate memory area so saves
memory
• a thread is executed inside the process. There is
context-switching between the threads. There can
be multiple processes inside the OS, and one
process can have multiple threads.
• Note: At a time one thread is executed only.
Advantages of Multithreading
• It doesn't block the user because threads are independent and you
can perform multiple operations at the same time.
• You can perform many operations together, so it saves time.
• Threads are independent, so it doesn't affect other threads if an
exception occurs in a single thread.
program: Java Thread Example by extending
Thread class
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
Java Thread Example by implementing Runnable
interface
• If you are not extending the
Thread class,your class object
would not be treated as a thread
object.
• So you need to explicitely create
Thread class object.We are
passing the object of your class
that implements Runnable so
that your class run() method
may execute.
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}
Join() method
• The join() method waits for a thread
to die. In other words, it causes the
currently running threads to stop
executing until the thread it joins
with completes its task.
• Syntax:
• public void join()throws
InterruptedException
• public void join(long
milliseconds)throws
InterruptedException
class TestJoinMethod1 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod1 t1=new TestJoinMethod1();
TestJoinMethod1 t2=new TestJoinMethod1();
TestJoinMethod1 t3=new TestJoinMethod1();
t1.start();
try{
t1.join();
}catch(Exception e){System.out.println(e);}
t2.start();
t3.start();
}
}
Program of performing two tasks by two threads
class Simple1 extends Thread{
public void run(){
System.out.println("task one");
}
}
class Simple2 extends Thread{
public void run(){
System.out.println("task two");
}
}
class TestMultitasking3{
public static void main(String
args[]){
Simple1 t1=new Simple1();
Simple2 t2=new Simple2();
t1.start();
t2.start();
}
}
Synchronization
• Synchronization in java is the capability to control the access of
multiple threads to any shared resource.
• Java Synchronization is better option where we want to allow only
one thread to access the shared resource.
Inter Thread Communication
• Inter-thread communication or Co-operation is all about allowing synchronized threads
to communicate with each other.
• Cooperation (Inter-thread communication) is a mechanism in which a thread is paused
running in its critical section and another thread is allowed to enter (or lock) in the same
critical section to be executed.It is implemented by following methods of Object class:
• wait()
• notify()
• notifyAll()
1) wait() method
Causes current thread to release the lock and wait until either another thread invokes the notify()
method or the notifyAll() method for this object, or a specified amount of time has elapsed.
notify() method
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this
object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of
the implementation. Syntax:
public final void notify()
3) notifyAll() method
Wakes up all threads that are waiting on this object's monitor. Syntax:
public final void notifyAll()
Process of InterThread Communication
• Threads enter to acquire lock.
• Lock is acquired by on thread.
• Now thread goes to waiting state if you call
wait() method on the object. Otherwise it
releases the lock and exits.
• If you call notify() or notifyAll() method,
thread moves to the notified state (runnable
state).
• Now thread is available to acquire lock.
• After completion of the task, thread releases
the lock and exits the monitor state of the
object.
Multithreading Important Questions
• Write a program that creates two threads. Each thread is instantiated from the same class. It executes a loop with
10 iterations. Each iteration displays “Welcome” message, sleeps for 200 milliseconds.
• Explain life cycle of thread
• What are different ways of creating a thread?
• What is purpose of wait() method in multithreading?
• What is multithreading? How to create Thread using Thread class?
• What is thread synchronization? How is it achieved in Java?
• List and explain methods used in inter thread communication
• What do you mean by synchronising a thread explain in detail
• What is meant by multithreading? Explain how to create thread using Runnable interface
• Write a note on Thread class and methods of thread class
• WAP to show interleaving of 2 threads and display output ABABABABABA

More Related Content

Multithreading in Java

  • 2. Multithreading : Summary • A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. • Multithreading in Java is a process of executing multiple threads simultaneously. • we use multithreading than multiprocessing because threads use a shared memory area. They don't allocate separate memory area so saves memory • a thread is executed inside the process. There is context-switching between the threads. There can be multiple processes inside the OS, and one process can have multiple threads. • Note: At a time one thread is executed only.
  • 3. Advantages of Multithreading • It doesn't block the user because threads are independent and you can perform multiple operations at the same time. • You can perform many operations together, so it saves time. • Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread.
  • 4. program: Java Thread Example by extending Thread class class Multi extends Thread{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi t1=new Multi(); t1.start(); } }
  • 5. Java Thread Example by implementing Runnable interface • If you are not extending the Thread class,your class object would not be treated as a thread object. • So you need to explicitely create Thread class object.We are passing the object of your class that implements Runnable so that your class run() method may execute. class Multi3 implements Runnable{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi3 m1=new Multi3(); Thread t1 =new Thread(m1); t1.start(); } }
  • 6. Join() method • The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task. • Syntax: • public void join()throws InterruptedException • public void join(long milliseconds)throws InterruptedException class TestJoinMethod1 extends Thread{ public void run(){ for(int i=1;i<=5;i++){ try{ Thread.sleep(500); }catch(Exception e){System.out.println(e);} System.out.println(i); } } public static void main(String args[]){ TestJoinMethod1 t1=new TestJoinMethod1(); TestJoinMethod1 t2=new TestJoinMethod1(); TestJoinMethod1 t3=new TestJoinMethod1(); t1.start(); try{ t1.join(); }catch(Exception e){System.out.println(e);} t2.start(); t3.start(); } }
  • 7. Program of performing two tasks by two threads class Simple1 extends Thread{ public void run(){ System.out.println("task one"); } } class Simple2 extends Thread{ public void run(){ System.out.println("task two"); } } class TestMultitasking3{ public static void main(String args[]){ Simple1 t1=new Simple1(); Simple2 t2=new Simple2(); t1.start(); t2.start(); } }
  • 8. Synchronization • Synchronization in java is the capability to control the access of multiple threads to any shared resource. • Java Synchronization is better option where we want to allow only one thread to access the shared resource.
  • 9. Inter Thread Communication • Inter-thread communication or Co-operation is all about allowing synchronized threads to communicate with each other. • Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.It is implemented by following methods of Object class: • wait() • notify() • notifyAll()
  • 10. 1) wait() method Causes current thread to release the lock and wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. notify() method Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. Syntax: public final void notify() 3) notifyAll() method Wakes up all threads that are waiting on this object's monitor. Syntax: public final void notifyAll()
  • 11. Process of InterThread Communication • Threads enter to acquire lock. • Lock is acquired by on thread. • Now thread goes to waiting state if you call wait() method on the object. Otherwise it releases the lock and exits. • If you call notify() or notifyAll() method, thread moves to the notified state (runnable state). • Now thread is available to acquire lock. • After completion of the task, thread releases the lock and exits the monitor state of the object.
  • 12. Multithreading Important Questions • Write a program that creates two threads. Each thread is instantiated from the same class. It executes a loop with 10 iterations. Each iteration displays “Welcome” message, sleeps for 200 milliseconds. • Explain life cycle of thread • What are different ways of creating a thread? • What is purpose of wait() method in multithreading? • What is multithreading? How to create Thread using Thread class? • What is thread synchronization? How is it achieved in Java? • List and explain methods used in inter thread communication • What do you mean by synchronising a thread explain in detail • What is meant by multithreading? Explain how to create thread using Runnable interface • Write a note on Thread class and methods of thread class • WAP to show interleaving of 2 threads and display output ABABABABABA