Core Java

Debasish Pratihari

Multi-Threading :


A multithread program contains two or more
parts that can run concurrently. Each part of
such program is called a thread, and each
thread defines a separate path of execution.



Multithreading is a specialized form of
multitasking.



Multithreading enables you to write very
efficient programs that make maximum use of
the CPU, because idle time can be kept to a
minimum.

Methods of thread class :
getName()
getPriority()
getId()
getThreadGroup()
isAlive()
join()
run()
sleep()
start()

setName()
setPriority()
setDaemon()
yield ()
interrupt()
isDaemon()
isInterrupted()

Thread Priority :


Java assigns to each thread a priority that
determines how that thread should be treated with
respect to the others. Thread priorities are
integers that specify the relative priority of one
thread to another. A higher-priority thread does
not run faster than a low priority thread. Instead, a
thread priority is used to decide when to switch
from one running thread to the next. This is called
context switching. The constants to set priority
are:
MIN_PRIORITY=1
MAX_PRIORITY=10
NORM_PRIORITY=5

Lecture/core/thread2/21

Page #1

feel the Technology…
Core Java

Debasish Pratihari

Thread Synchronization :


When two or more threads need access to a
shared resources, they need some way to
ensure that the resource will be used by only
one thread at a time. The process by which this
is achieved is called synchronization.



When a thread invokes a synchronized method ,
it automatically acquires the intrinsic lock for
that method’s object and releases it when the
method return. The lock releases occurs even if
the return was caused by an un-caught
exception.

Synchronization Methods :
o Synchronized Methods
void fun(){
synchronized( expr ) {
// 'expr' must evaluate to an Object

}

}
o Synchronized Statement
public synchronized void fun() {
//method body here
}

More About Synchronize Key word:


The synchronized keyword is NOT considered
part of a method's signature.



It is not automatically inherited when
subclasses override super class methods



methods in Interfaces cannot be declared
synchronized



constructors cannot be declared synchronized
however they can contain synchronized blocks

Lecture/core/thread2/21

Page #2

feel the Technology…
Core Java

Debasish Pratihari

Thread Interrupts:


A thread can signal another thread that is should
stop executing by calling interrupt() method for that
thread object.

class MyThread extends Thread{
Thread2(){
start();
}
public void run(){
for(int i=1;i<=1000;i++){
System.out.println(i);
if(Thread.interrupted())
System.out.println("Thread Inrrupted..");
}
}

}

class ThreadTest{
public static void main(String args[]){
MyThread t= new MyThread ();
while(true){
while(!t.isInterrupted()){
t.interrupt();
System.out.println("Interrupted by
main");
}
}
}

}

Output :

Lecture/core/thread2/21

Page #3

feel the Technology…
Core Java

Debasish Pratihari

Thread Group:






Every Java thread is a member of a Thread
Group.
ThreadGroup facilitate to operate or manage
threads as group.
Java runtime system creates a Thread Group
named “main”.
If any thread is created without a thread group
specification , the runtime system put the
thread into main.
Once a thread became a member of a group it
can’t be removed from that group.

Creating a Thread Group :
25%

ThreadGroup debThreads = new ThreadGroup("debasish");

Putting Threads into ThreadGroup:
Thread debThread = new Thread(debThreads, this);

Getting a Thread’s group:
ThreadGroup group = debThread.getThreadGroup();

Example:
public class GroupTest{
public static void main(String ar[]){
ThreadGroup
group=Thread.currentThread().getThreadGroup();
System.out.println(group.getName());
System.out.println(group.activeCount());
}
}

Output :
main
1

Lecture/core/thread2/21

Page #4

feel the Technology…

Lecture 21

  • 1.
    Core Java Debasish Pratihari Multi-Threading:  A multithread program contains two or more parts that can run concurrently. Each part of such program is called a thread, and each thread defines a separate path of execution.  Multithreading is a specialized form of multitasking.  Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum. Methods of thread class : getName() getPriority() getId() getThreadGroup() isAlive() join() run() sleep() start() setName() setPriority() setDaemon() yield () interrupt() isDaemon() isInterrupted() Thread Priority :  Java assigns to each thread a priority that determines how that thread should be treated with respect to the others. Thread priorities are integers that specify the relative priority of one thread to another. A higher-priority thread does not run faster than a low priority thread. Instead, a thread priority is used to decide when to switch from one running thread to the next. This is called context switching. The constants to set priority are: MIN_PRIORITY=1 MAX_PRIORITY=10 NORM_PRIORITY=5 Lecture/core/thread2/21 Page #1 feel the Technology…
  • 2.
    Core Java Debasish Pratihari ThreadSynchronization :  When two or more threads need access to a shared resources, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization.  When a thread invokes a synchronized method , it automatically acquires the intrinsic lock for that method’s object and releases it when the method return. The lock releases occurs even if the return was caused by an un-caught exception. Synchronization Methods : o Synchronized Methods void fun(){ synchronized( expr ) { // 'expr' must evaluate to an Object } } o Synchronized Statement public synchronized void fun() { //method body here } More About Synchronize Key word:  The synchronized keyword is NOT considered part of a method's signature.  It is not automatically inherited when subclasses override super class methods  methods in Interfaces cannot be declared synchronized  constructors cannot be declared synchronized however they can contain synchronized blocks Lecture/core/thread2/21 Page #2 feel the Technology…
  • 3.
    Core Java Debasish Pratihari ThreadInterrupts:  A thread can signal another thread that is should stop executing by calling interrupt() method for that thread object. class MyThread extends Thread{ Thread2(){ start(); } public void run(){ for(int i=1;i<=1000;i++){ System.out.println(i); if(Thread.interrupted()) System.out.println("Thread Inrrupted.."); } } } class ThreadTest{ public static void main(String args[]){ MyThread t= new MyThread (); while(true){ while(!t.isInterrupted()){ t.interrupt(); System.out.println("Interrupted by main"); } } } } Output : Lecture/core/thread2/21 Page #3 feel the Technology…
  • 4.
    Core Java Debasish Pratihari ThreadGroup:      Every Java thread is a member of a Thread Group. ThreadGroup facilitate to operate or manage threads as group. Java runtime system creates a Thread Group named “main”. If any thread is created without a thread group specification , the runtime system put the thread into main. Once a thread became a member of a group it can’t be removed from that group. Creating a Thread Group : 25% ThreadGroup debThreads = new ThreadGroup("debasish"); Putting Threads into ThreadGroup: Thread debThread = new Thread(debThreads, this); Getting a Thread’s group: ThreadGroup group = debThread.getThreadGroup(); Example: public class GroupTest{ public static void main(String ar[]){ ThreadGroup group=Thread.currentThread().getThreadGroup(); System.out.println(group.getName()); System.out.println(group.activeCount()); } } Output : main 1 Lecture/core/thread2/21 Page #4 feel the Technology…