M.SANDHIYA (MSC INFO TECH)
NADAR SARASWATHI COLLEGE OF ARTS
AND SCIENCE.
 Thread: single sequential flow of control
within a program
 Single-threaded program can handle one task
at any time.
 Multitasking allows single processor to run
several concurrent threads.
 Most modern operating systems support
multitasking.
 Reactive systems – constantly monitoring
 More responsive to user input – GUI application
can interrupt a time-consuming task
 Server can handle multiple clients simultaneously
 Can take advantage of parallel processing
 Different processes do not share memory space.
 A thread can execute concurrently with other
threads within a single process.
 All threads managed by the JVM share memory
space and can communicate with each other.
Creating threads in Java:
 Extend java.lang.Thread class
◦ run() method must be overridden (similar to main
method of sequential program)
◦ run() is called when execution of the thread begins
◦ A thread terminates when run() returns
◦ start() method invokes run()
◦ Calling run() does not create a new thread
 Implement java.lang.Runnable interface
Creating threads in Java:
 Extend java.lang.Thread class
 Implement java.lang.Runnable interface
◦ If already inheriting another class (i.e., JApplet)
◦ Single method: public void run()
◦ Thread class implements Runnable
isAlive()
 method used to find out the state of a thread.
 returns true: thread is in the Ready, Blocked, or
Running state
 returns false: thread is new and has not started or
if it is finished.
interrupt()
f a thread is currently in the Ready or Running
state, its interrupted flag is set; if a thread is
currently blocked, it is awakened and enters the
Ready state, and an java.io.InterruptedException is
thrown.
The isInterrupt() method tests whether the thread is
interrupted.
• Each thread is assigned a default priority of
Thread.NORM_PRIORITY (constant of 5). You can
reset the priority using setPriority(int
priority).
• Some constants for priorities include
Thread.MIN_PRIORITY Thread.MAX_PRIORITY
Thread.NORM_PRIORITY
• By default, a thread has the priority level of the thread
that created it.
 Thread-based Multitasking (Multithreading)
 Threads share the same address space.
 A thread is lightweight.
 Cost of communication between the thread is
low.
 Multithreading is a Java feature that allows concurrent
execution of two or more parts of a program for
maximum utilization of CPU. Each part of such
program is called a thread. So, threads are light-
weight processes within a process.
Threads can be created by using two mechanisms :
1. Extending the Thread class
2. Implementing the Runnable Interface
Multithreading can be a
Multithreading can be a great
way to increase the performance
of some types of programs.
However, mulithreading is even
more challenging than
multitasking. The threads are
executing within the same
program and are hence reading
and writing the same memory
simultanously. This can result in
errors not seen in a
singlethreaded program. Some
of these errors may not be seen
on single CPU machines, because
two threads never really execute
"simultanously". Modern
computers, though, come with
multi core CPUs, and even with
multiple CPUs too.
 General concurrency and multithreading theory:
 Multithreading Benefits
 Multithreading Costs
 Concurrency Models
 Same-threading
 Concurrency vs. Parallelism
 The basics of Java concurrency:
 Creating and Starting Java Threads
 Race Conditions and Critical Sections
 Thread Safety and Shared Resources
 Thread Safety and Immutability
 Java Memory Model
 Java Synchronized Blocks
 Java Volatile Keyword
 Java ThreadLocal
 Java Thread Signaling

Mulitthread

  • 1.
    M.SANDHIYA (MSC INFOTECH) NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE.
  • 2.
     Thread: singlesequential flow of control within a program  Single-threaded program can handle one task at any time.  Multitasking allows single processor to run several concurrent threads.  Most modern operating systems support multitasking.
  • 3.
     Reactive systems– constantly monitoring  More responsive to user input – GUI application can interrupt a time-consuming task  Server can handle multiple clients simultaneously  Can take advantage of parallel processing  Different processes do not share memory space.  A thread can execute concurrently with other threads within a single process.  All threads managed by the JVM share memory space and can communicate with each other.
  • 4.
    Creating threads inJava:  Extend java.lang.Thread class ◦ run() method must be overridden (similar to main method of sequential program) ◦ run() is called when execution of the thread begins ◦ A thread terminates when run() returns ◦ start() method invokes run() ◦ Calling run() does not create a new thread  Implement java.lang.Runnable interface
  • 5.
    Creating threads inJava:  Extend java.lang.Thread class  Implement java.lang.Runnable interface ◦ If already inheriting another class (i.e., JApplet) ◦ Single method: public void run() ◦ Thread class implements Runnable
  • 6.
    isAlive()  method usedto find out the state of a thread.  returns true: thread is in the Ready, Blocked, or Running state  returns false: thread is new and has not started or if it is finished. interrupt() f a thread is currently in the Ready or Running state, its interrupted flag is set; if a thread is currently blocked, it is awakened and enters the Ready state, and an java.io.InterruptedException is thrown. The isInterrupt() method tests whether the thread is interrupted.
  • 7.
    • Each threadis assigned a default priority of Thread.NORM_PRIORITY (constant of 5). You can reset the priority using setPriority(int priority). • Some constants for priorities include Thread.MIN_PRIORITY Thread.MAX_PRIORITY Thread.NORM_PRIORITY • By default, a thread has the priority level of the thread that created it.
  • 8.
     Thread-based Multitasking(Multithreading)  Threads share the same address space.  A thread is lightweight.  Cost of communication between the thread is low.
  • 9.
     Multithreading isa Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such program is called a thread. So, threads are light- weight processes within a process. Threads can be created by using two mechanisms : 1. Extending the Thread class 2. Implementing the Runnable Interface
  • 10.
    Multithreading can bea Multithreading can be a great way to increase the performance of some types of programs. However, mulithreading is even more challenging than multitasking. The threads are executing within the same program and are hence reading and writing the same memory simultanously. This can result in errors not seen in a singlethreaded program. Some of these errors may not be seen on single CPU machines, because two threads never really execute "simultanously". Modern computers, though, come with multi core CPUs, and even with multiple CPUs too.
  • 11.
     General concurrencyand multithreading theory:  Multithreading Benefits  Multithreading Costs  Concurrency Models  Same-threading  Concurrency vs. Parallelism  The basics of Java concurrency:  Creating and Starting Java Threads  Race Conditions and Critical Sections  Thread Safety and Shared Resources  Thread Safety and Immutability  Java Memory Model  Java Synchronized Blocks  Java Volatile Keyword  Java ThreadLocal  Java Thread Signaling